CriteriaQuery join fetch not executed for @Embeddable with @OneToMany in Hibernate
Issue
- A
@OneToMany
association exists from a "parent" to a "child" type through an@Embeddable
@Entity
public class ChildEntity implements Serializable {
...
}
@Embeddable
public class EmbeddedEntity implements Serializable {
@OneToMany( cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_GUID")
private Set<ChildEntity> children;
...
}
@Entity
public class ParentEntity implements Serializable {
@Id
private String guid;
@Embedded
private EmbeddedEntity embedded;
...
}
- A
CriteriaQuery
is constructed as follows (using hibernate-jpamodelgen properties)
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<ParentEntity> cq = cb.createQuery(ParentEntity.class);
Root<ParentEntity> root = cq.from(ParentEntity.class);
root.fetch(ParentEntity_.embedded).fetch(EmbeddedEntity_.children); // specify join fetch
cq.where(cb.equal(root.get(ParentEntity_.guid), "P1"));
TypedQuery<ParentEntity> typeQuery = entityManager.createQuery(cq);
- The generated query omits the join fetch (as evident in looking at the generated SQL)
- The association is not initialized and requires a subsequent query
Environment
- Red Hat JBoss Enterprise Application Platform (EAP) 6
- Hibernate 4
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.