I have a one-to-many relationship defined as below
Code:
@Cacheable
@Entity
@NamedEntityGraph(
name = "Parent.Child",
attributeNodes = {
@NamedAttributeNode("children"),
}
)
public class Parent {
private Set<Child> children;
// getter - setter
}
Now in my DAL, i'm calling this method
Code:
@Override
public Defect getParentWithChildren(int id) {
EntityGraph<?> graph = entityManager.getEntityGraph("Parent.Child");
Map<String, Object> props = new HashMap<>();
props.put("javax.persistence.fetchgraph", graph);
return entityManager.find(Parent.class, id, props);
}
Since i have loaded Parent with Children, i should be able to use children collection outside of the transaction. But i'm getting Lazyinitialization Exception. This happens only when hibernate level 2 cache - ehcache is enabled. If i disable it from config, it works as expected. Also if i initialize collection explicitly after find, it works as expected. So is it a bug?. I'm using Hibernate 5.2.6.Final with JPA 2.1.
EDIT: One more thing i noticed was that entity loads fine for the first time, so it must be sonethibg related with Entity Graph support.