-->
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.  [ 3 posts ] 
Author Message
 Post subject: Does lazy loading always result in n+1 selects?
PostPosted: Mon Jul 06, 2009 4:04 pm 
Newbie

Joined: Sun Apr 12, 2009 4:43 am
Posts: 11
Hi
If I retrieve an object Parent with a lazy-loaded collection Children, then when I try to access the Children collection, I should only get one more SQL statement executed?
Is n+1 selects only an issue when I had retrieved several Parents, and then tried to get each of their Children collections?
If lazy loading always resulted in n+1 selects, then I think one would be better off using detached objects pre-loaded rather than Open session in view, as the Hibernate book says 'You always want to avoid n+1 selects'. However, if there are circumstances in which it doesn't then perhaps I should use Open session in view and lazy loading.
thanks, Baljeet.


Top
 Profile  
 
 Post subject: Re: Does lazy loading always result in n+1 selects?
PostPosted: Tue Jul 07, 2009 3:19 pm 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
Hi,

Another approach is to specify a batch-size as mentioned here: http://docs.jboss.org/hibernate/stable/core/reference/en/html/performance.html#performance-fetching-batch

It should result in Hibernate retrieving the Children entities from the database in batches of the size that you specify.

--
Stephen Souness



blog: http://elegant-solutions.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Does lazy loading always result in n+1 selects?
PostPosted: Wed Jul 08, 2009 11:18 am 
Newbie

Joined: Sun Apr 12, 2009 4:43 am
Posts: 11
Thanks for that, I had come across the descriptions of different fetch strategies. I was thinking about Open Session In View, which in theory should allow you to define everything as lazy-loaded, but which in practice, you may need to think about whether or not you'd be accessing collections for n entities (rather than 1), or whether your single entity's collections had collections also, which would also result in n+1 selects, I believe. Then you would need to use a query instead of lazy-loading even with Open Session In View ...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.