I have the following structure of entities:
Route: -one-to-many-> :Stop: <-many-to-many-> :Child: <-many-to-many-> :Contact
And so I've defined the following named entity graphs at the top of each entity class so I can optimize my queries for how far down this heirarchy I'll need to travel:
Route:
@NamedEntityGraphs({
@NamedEntityGraph(name = "Route.stop.child.contact",
attributeNodes = {
@NamedAttributeNode(value = "stops", subgraph = "Stop.child.contact")
}),
@NamedEntityGraph(name = "Route.stop.child",
attributeNodes = {
@NamedAttributeNode(value = "stops", subgraph = "Stop.child")
}),
@NamedEntityGraph(name = "Route.stop",
attributeNodes = {
@NamedAttributeNode(value = "stops", subgraph = "Stop")
})
})
public class Route {...}
Stop:
@NamedEntityGraphs({
@NamedEntityGraph(name = "Stop.child.contact",
attributeNodes = {
@NamedAttributeNode(value = "children", subgraph = "Child.contact")
}),
@NamedEntityGraph(name = "Stop.child",
attributeNodes = {
@NamedAttributeNode(value = "children", subgraph = "Child")
}),
@NamedEntityGraph(name = "Stop", attributeNodes = {})
})
public class Stop {...}
Child:
@NamedEntityGraphs({
@NamedEntityGraph(name = "Child.contact",
attributeNodes = {
@NamedAttributeNode(value = "contacts", subgraph = "Contact")
}),
@NamedEntityGraph(name = "Child", attributeNodes = {})
})
public class Child {...}
Contact:
@NamedEntityGraphs({@NamedEntityGraph(name = "Contact", attributeNodes = {})})
public class Contact {...}
And I use them in my Repository class as such:
@EntityGraph(value = "Route.stop.child.contact", type = EntityGraph.EntityGraphType.LOAD)
Optional<Route> findRouteStopsChildrenContactsByRouteId(short routeId);
However, I can see no join queries being made, and a lazyinit exception is thrown when I try to access children of stops. My understanding was that this is how entity graphs can be used to optimize queries (i.e. n+1 problem when I have to fetch children for each individual stop), but I'm having no luck getting it to work.
Any advice as to what I'm doing wrong?