-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: 5N+1 selects problem
PostPosted: Wed Dec 09, 2009 5:06 pm 
Newbie

Joined: Wed Dec 09, 2009 5:01 pm
Posts: 1
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.