I have a one-to-many relationship defined as below
@Cacheable
@Entity
@NamedEntityGraph(
name = "Parent.Child",
attributeNodes = {
@NamedAttributeNode("children"),
}
)
public class Parent {
private Set<Child> children;
}
Now in my DAL, i'm calling this method
@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 and entity is getting loaded from the cache instead of the database (i.e. it is already loaded from another transaction and saved in the cache). If i disable it from config, it works as expected. Also if i initialize collection explicitly after find, it works as expected. I'm using Hibernate 5.2.6.Final with JPA 2.1. Suitable Test Case can be found here: TestCase |