I have the following model: Order has multiple items and items have multiple other children. It's a tree several levels deep.
I have EAGER loading on all @OneToMany
together with @JoinFetch
and LAZY loading on all @ManyToOne
(to avoid additional queries in N+1 problem).
As per How do I do a "deep" fetch join in JPQL? I can use multiple query hints to presumably eager load the whole tree, however, I'd prefer to do this via annotations only.
Unfortunately, even with the whole tree annotated with @JoinFetch
, it seems that the joins are performed 1 level deep only, leading to multiple queries instead of a single query for the whole tree.
These are the queries I can see being executed:
Order - left outer join on all children.
OrderItem - left outer join on all children.
...etc for all levels.
I can see @JoinFetch
is working but it works only one level deep. What I'd like to achieve is a single query to load the whole tree.
While this one level deep load did help me speed up the calls more than 2x they are still suboptimal when loading a lot of entities at once since each entitiy means 5+ additional queries.
Is there a way to load the whole tree with annotations only or should I manually set multiple query hints to achieve this?
Answer by James Sutherland here does not seem to be true (about recursive working).