-->
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.  [ 7 posts ] 
Author Message
 Post subject: n+1 on a Query, but want eager loading
PostPosted: Fri Jul 15, 2005 10:09 am 
Newbie

Joined: Wed Jun 01, 2005 5:34 am
Posts: 6
Location: Karlsruhe (Germany)
I have a very simple pice of code:

Query query = session.createQuery("from Site as site");
query.setFetchSize( 10);
List list = query.list();
v.addAll(list);

My problem is that these lines lead in a n+1 queries to the database.

The mapping for the Site object is:
<hibernate-mapping>
<class name="com.lauerbach.jcms.beans.Site"
lazy="false"
batch-size="10">
...


How do I define eager loading directly for the entity that is selected?
Is there a similar mechanism as in the CMP-Engine on JBoss? There I can define load-groups on the finders to load a set of properties eagerly.

So how do I define a load-group for a single query?
Or at least, how do I load all fields directly with the finder?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 16, 2005 7:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Show your mappings as we cannot see the relationships.
In anycase, read about 'left join fetch' and use it on the fields you wish to eager load with the query, eg, Many-to-one rels etc.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 17, 2005 2:52 am 
Newbie

Joined: Wed Jun 01, 2005 5:34 am
Posts: 6
Location: Karlsruhe (Germany)
No my Object does not have any relations. Assume this object just stores properties.
In the documentation and in several how-tos i found dozens of load optimization on relations, but not on the load of the object itself.
I already tried to define lazy="false" of the object itself. But I still get the n+1 queries to the database.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 17, 2005 3:24 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
Code:
Query query = session.createQuery("from Site as site");
query.setFetchSize( 10);
List list = query.list();
v.addAll(list);


What is the type of v, and what does the addAll() method do? The query should be initializing everything for you, so this is the only point where I could see extra fetches happening. (Note, this isn't n+1, but just n -- besides, n + 1 usually refers to when you have a one-to-many relationship. You get one select for the "one" side of the relationship and "n" selects as you access and lazy load each member of the associated "many" side of the relationship.)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 6:33 am 
Newbie

Joined: Wed Jun 01, 2005 5:34 am
Posts: 6
Location: Karlsruhe (Germany)
Variable v is a simple java.util.Vector and does not matter at this point (sorry for inserting here).


Here is the relevant part of may console-log, you can clearly see the n+1 queries:

12:24:04,231 INFO [STDOUT] Hibernate: select this_.siteID as siteID0_,this_.name as name10_0_ from Site this_

12:24:04,387 INFO [STDOUT] Hibernate: select rubrics0_.site as site__, rubrics0_.rubricID as rubricID__, rubrics0_.rubricID as rubricID0_, rubrics0_.description as descript2_9_0_, rubrics0_.title as title9_0_, rubrics0_.site as site9_0_, r
ubrics0_.parentRubric as parentRu5_9_0_ from Rubric rubrics0_ where rubrics0_.site=?

12:24:04,403 INFO [STDOUT] Hibernate: select rubrics0_.site as site__, rubrics0_.rubricID as rubricID__, rubrics0_.rubricID as rubricID0_, rubrics0_.description as descript2_9_0_, rubrics0_.title as title9_0_, rubrics0_.site as site9_0_, rubrics0_.parentRubric as parentRu5_9_0_ from Rubric rubrics0_ where rubrics0_.site=?

12:24:04,403 INFO [STDOUT] Hibernate: select rubrics0_.site as site__, rubrics0_.rubricID as rubricID__, rubrics0_.rubricID as rubricID0_, rubrics0_.description as descript2_9_0_, rubrics0_.title as title9_0_, rubrics0_.site as site9_0_, rubrics0_.parentRubric as parentRu5_9_0_ from Rubric rubrics0_ where rubrics0_.site=?

12:24:04,403 INFO [STDOUT] Hibernate: select rubrics0_.site as site__, rubrics0_.rubricID as rubricID__, rubrics0_.rubricID as rubricID0_, rubrics0_.description as descript2_9_0_, rubrics0_.title as title9_0_, rubrics0_.site as site9_0_, rubrics0_.parentRubric as parentRu5_9_0_ from Rubric rubrics0_ where rubrics0_.site=?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 6:35 am 
Newbie

Joined: Wed Jun 01, 2005 5:34 am
Posts: 6
Location: Karlsruhe (Germany)
sh..., it IS the relation that is loaded +x.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 8:32 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
thl-mot wrote:
sh..., it IS the relation that is loaded +x.


Yeah, looks like its the relationship with Rubric (I'm assuming that's the classname). Can you post your mappings for Site and Rubric? I'm a bit confused as to why you're loading Rubric, since the query didn't ask for it.


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