0

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?

Jordan Mackie
  • 2,264
  • 4
  • 25
  • 45
  • Hi! I stumbled upon a case like yours, did you find a solution? – pleft Apr 04 '19 at 08:12
  • Unfortunately not, I think my conclusion was that entity graphs only worked to two layers deep. There are workarounds that involve starting queries from the deepest child I think though – Jordan Mackie Apr 04 '19 at 14:01

1 Answers1

0

Try this one it worked for me.

@NamedEntityGraph(name = "Book.details", attributeNodes = @NamedAttributeNode("reviews"))
@Table(name = "Book")
public class Book  implements Serializable
{
    @OneToMany(cascade = CascadeType.ALL , mappedBy = "book" , fetch = FetchType.EAGER)
    //@OrderBy("no ASC")
    private List<Review> reviews ; 
}

2) Get Record (internally use left outer join).

@EntityGraph(value = "Book.details", type = EntityGraphType.FETCH)
  List<Book> getBooksByAuthName(String aname); 
Angad Bansode
  • 825
  • 6
  • 15