-->
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.  [ 2 posts ] 
Author Message
 Post subject: N+1 Problem Occurs only with Lazy Loading?
PostPosted: Wed Mar 28, 2012 3:50 am 
Newbie

Joined: Wed Jul 06, 2011 3:22 am
Posts: 5
Does N+1 problem occurs only with Lazy Loading in Hibernate?

I am assuming that objects (both parent and child) are loaded in the same way for lazy and eager loading.
But the difference is in case of Lazy Loading the query for child objects are executed only when we access the child object.
In case of eager Loading, all the queries are executed when the request for the parent object is done.

So, N+1 problem occurs in both Lazy and Eager Loading. Isn't it?

Kindly correct if i am wrong.

Thanks.

Regards
Senthil Kumar Sekar


Top
 Profile  
 
 Post subject: Re: N+1 Problem Occurs only with Lazy Loading?
PostPosted: Wed Mar 28, 2012 9:43 am 
Newbie

Joined: Wed Mar 24, 2010 10:32 am
Posts: 7
See the hibernate manual and this stackoverflow thread

I think you are mixing up the how and when, as described at both links. Eager fetching can still cause the N+1 issue for collections. To solve this, I think you want to try the join fetch. If you are using Hibernate and not JPA then you can try the @Fetch annotation with FetchMode.JOIN. If you are using JPA then I think your options are to add the Hibernate specific annotation, knowing that you then are stuck w/ Hibernate and may not be able to switch providers, or use JPQL and put the fetch mode in the query. Unfortunately I don't think that you can specify the fetch mode using JPA2's criteria API (or at least I haven't figured out how), which is why you'd have to use the JPQL instead.

BTW, you can turn on SQL output (hibernate.show_sql = true) and experiment with each of the fetching strategies to see exactly what is happening under the hood.


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