During upgrade from Hibernate 4 to 5 I found out weird loading bug by entity graph.
Structure is simple. I have Activity which have one ActivityPerson as manager and collection of same entities (ActivityPerson) as participants. ActivityPerson has only one field Person. All associations are lazy fetch.
Now if I want load only manager
EntityGraph graph = em.createEntityGraph(Activity.class);
graph.addSubgraph("manager").addSubgraph("person");
or only participants
EntityGraph graph = em.createEntityGraph(Activity.class);
graph.addSubgraph("participant").addSubgraph("person");
Then everything works and printing some field from person is no problem.
System.out.println(act1.getManager().toString());
// or
act2.getParticipant().forEach((p) -> System.out.println(p.toString()));
But if I want load manager and participants then problem occurs. In this simplified version manager was loaded but participants were not loaded and throw could not initialize proxy
when is object accessed. (in my project opposite behavior occur but this is not important)
EntityGraph graph = em.createEntityGraph(Activity.class);
graph.addSubgraph("manager").addSubgraph("person");
graph.addSubgraph("participant").addSubgraph("person");
// ...
System.out.println(act3.getManager().toString()); // properly loaded
act3.getParticipant().forEach((p) -> System.out.println(p.toString())); // throws error
I am attaching a simplified project where the bug is reproducible: