Q:

Criteria API and Entity Graph

I have two entities with unidirectional lazy relations. And one of them has a named entity graph. Here are the entities (getters and setter are not listed due to readability):
    @Entity
    @NamedEntityGraph (
            name = "Sms.fetchSims",
            attributeNodes = {
                    @NamedAttributeNode("sim")
            }
    )
    public class Sms {
        @Id
        @GeneratedValue (strategy = GenerationType.AUTO)
        private Long id;
        @NotNull
        private Timestamp date;
        @NotNull
        private String sender;
        @NotNull
        private String content;

        @NotNull
        private Direction direction;

        @NotNull
        @ManyToOne (fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
        private Sim sim;
    }

    @Entity
    public class Sim {
        @Id
        @GeneratedValue (strategy = GenerationType.AUTO)
        private long id;

        @NotNull
        private long IMSI;

        private String MSISDN;
        private String provider;
        private Timestamp lockedDate;
    }

When I fetch any sms using the entity manager find() method with this entity graph, I get an sms with a sim.
But i want to be able to find smses by sim id. That's why i created a criteria api query for that purpose.
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();

    EntityGraph eg = entityManager.getEntityGraph("Sms.fetchSims");

    CriteriaQuery<Sms> cq = cb.createQuery(Sms.class);
    Root<Sms> smses = cq.from(Sms.class);
    Join<Sms, Sim> sims = smses.join(Sms_.sim);
    cq.where(cb.equal(sims.get(Sim_.id), simId));
    cq.orderBy(cb.desc(smses.get(Sms_.date)));

    return entityManager
        .createQuery(cq)
        .setHint("javax.persistence.fetchgraph", eg)
        .setMaxResults(1000)
        .getResultList();

As you see, i set the entity graph here using a setHint method of the typed query, but sim is never fetched, it's fields are not even listed in the result sql. What could i do to make entity graph work with criteria api?
I know I can typecast smses.fetch(Sms_.sim) to Join, but i get ugly warnings, and this method looks like a dirty hack to me.
I'm working with hibernate and wildfly-8.2.0.Final application server.

A:

Yeah, man, sorry. There was a bug in hibernate that was fixed after 4.3.9.Final.
Here s query generated by 4.3.9:
DEBUG - org.hibernate.SQL - 
select
    sms0_.id as id1_1_0_,
    sim1_.id as id1_0_1_,
    sms0_.content as content2_1_0_,
    sms0_.date as date3_1_0_,
    sms0_.direction as directio4_1_0_,
    sms0_.sender as sender5_1_0_,
    sms0_.sim_id as sim_id6_1_0_,
    sim1_.IMSI as IMSI2_0_1_,
    sim1_.MSISDN as MSISDN3_0_1_,
    sim1_.lockedDate as lockedDa4_0_1_,
    sim1_.provider as provider5_0_1_ 
from
    Sms sms0_ 
inner join
    Sim sim1_ 
        on sms0_.sim_id=sim1_.id 
where
    sim1_.id=1 
order by
    sms0_.date desc limit ?

and here s 4.3.8 query:
DEBUG - org.hibernate.SQL - 
select
    sms0_.id as id1_1_,
    sms0_.content as content2_1_,
    sms0_.date as date3_1_,
    sms0_.direction as directio4_1_,
    sms0_.sender as sender5_1_,
    sms0_.sim_id as sim_id6_1_ 
from
    Sms sms0_ 
inner join
    Sim sim1_ 
        on sms0_.sim_id=sim1_.id 
where
    sim1_.id=1 
order by
    sms0_.date desc limit ?

