Hi, I want to query some entities with some deep eager fetching and I want to avoid both the N+1 query problem and N^k sized result sets due to NxN joins.
For example, assume I have this structure:
class Person {
@Id
UUID id;
String name;
@ManyToMany(fetch = FetchType.LAZY)
Set<Person> friends;
}
and I want to run this HQL query:
from Person p
join fetch p.friends f1
join fetch f1.friends f2
join fetch f2.friends f3
where p.lastName = :lastName
The result set will be very large, with many Person
s needlessly duplicated several times.
I would think that, if I were to fetch the entire Person
table (it might not be that big), I would have enough data to follow references locally. Is Hibernate able to do this? If yes, how can I do it?
Also, is there any way to “download” intermediate tables for NxN relationships without running an actual join
for Hibernate to use? If not, is a join
query the only choice or is there a way to use something similar to “subselect” fetching?
The above is a silly example: in my actual case, this situation spans a few distinct tables; also, since I use multi-tenancy in one DB, while the tables are large, I can select relatively small parts of each table and have all the data I need.