-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to avoid N+1 select problem?
PostPosted: Tue May 15, 2007 12:59 pm 
Beginner
Beginner

Joined: Fri Apr 13, 2007 9:27 am
Posts: 33
I have 2 tables PARENT and CHILD. The PARENT tabel has a foriegn key to the CHILD table. I want to query for all children of parent with ID = 1

select p.child from Parent p where p.id = 1

The above query returns proxies for the children. How can write the same query with the collection been intitialized?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 1:22 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Sounds like you want your collections eagerly fetched.

You can try setting lazy=false in your mapping file.

I'm not sure if that will work with the query that you have though but if you have, for example

Code:
Parent p = (Parent)session.load(Parent.class, 1);


When you do p.getChildren() you should get an initialized collection.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 1:25 am 
Beginner
Beginner

Joined: Mon Nov 06, 2006 2:40 am
Posts: 29
Location: New Delhi, India
use "fetch" join to get childern using single select statement.


refer section 11.3 from http://www.hibernate.org/hib_docs/refer ... ryhql.html

_________________
Hope this helps... :)

Regards,

Rohit

--Don'f forget to rate...


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 8:13 am 
Beginner
Beginner

Joined: Fri Apr 13, 2007 9:27 am
Posts: 33
himawan wrote:
Sounds like you want your collections eagerly fetched.

You can try setting lazy=false in your mapping file.

I'm not sure if that will work with the query that you have though but if you have, for example

Code:
Parent p = (Parent)session.load(Parent.class, 1);


When you do p.getChildren() you should get an initialized collection.


I thought of that, however I want to keep the default setting - lazy true- and overrite the settings per query bases.

In addition: it is not one-to-many relationship. It is many-to-many. The child and the parent form a composite key.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 8:19 am 
Beginner
Beginner

Joined: Fri Apr 13, 2007 9:27 am
Posts: 33
Sagi wrote:
use "fetch" join to get childern using single select statement.


refer section 11.3 from http://www.hibernate.org/hib_docs/refer ... ryhql.html


It is not a one-to-many relationship, it is many-to-many. The child and the parent form a compoist key.

select p.child from Parent p left join fetch p.child where p.id = 2

the above query will throw an error.

however this query will not throw an error

select p from Parent p left join fetch p.child where p.id = 1

The above query will return a list of parent objects with its child object initialized. How can I return the child objects only?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 8:32 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
why don't you just query for the children.

from child c where c.parent.id=1

or subselect

from child where c.parent in (select parent where id=1)

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject: getChildren().size(), getChildren.iterator()
PostPosted: Wed May 16, 2007 9:41 am 
Newbie

Joined: Tue Dec 12, 2006 9:07 am
Posts: 13
I have observed that parent.getchildren() does not lazily load the children but any call to getChildren().size() or getChildren.iterator() initiates the lazy loading.

Can someone confirm if this is the expected behaviour or I can get the children loaded with a call to parent.getchildren() itself?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 9:51 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Code:
Hibernate.initialize(parent.getChildren());


is what you want to do with the session still open.





another way is to specify a join fetch on the children with an hql query.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


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

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.