I seem to have the 5N+1 selects problem. I've read through the "Java Persistence with Hibernate" book, but I haven't found an answer.
I have the following entity mappings in my entities (where A has a many-to-one relationship with B):
Code:
A many-to-one B
A one-to-one C
B one-to-one D
B one-to-one E
D many-to-one F
D one-to-one G
F one-to-one H
The entities are all mapped using outer-join=true.
When I execute a query of the following form:
Code:
from A a
join fetch a.d b
join fetch b.d d
join fetch d.f f
where d.someValue = :param1
and d.someOtherValue in (:param2)
Hibernate actually performs the following queries in SQL:
Code:
select A
select C
select E
select G
select B
select H
select C
select E
select G
select B
select H
etc. - the last five entities are fetched for each results in A
My query is fetching much more information than I need. I found that I can get rid of most, but not all, of the extra selects by using "join fetch" to eagerly load the objects, but that, in turn, causes additional selects.
Do you know why this might be happening? I'd like to reduce the number of selects to 1.