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.  [ 6 posts ] 
Author Message
 Post subject: fetch="join" n+1 select query
PostPosted: Mon Oct 20, 2008 11:11 am 
Beginner
Beginner

Joined: Wed Oct 18, 2006 4:41 am
Posts: 20
Location: West Yorkshire, UK
Hi there,

I have a very complicated object model/database - when I try to load the entity that's at the top of the tree (using session.load) I can see 4000+ lines of sql being generated. I have tried to tune this using fetch="join" on the main offending collection - this attempts to join but the query is then too complex.

When I set this back to "select" mode however, then change some of the descendant collections to use fetch="join", this seems to be ignored. I have tried to be more fine grained using the criteria objects and setting the FetchMode, more or less as follows:


Code:
ICriteria criteria = session.CreateCriteria(typeof (Parent));
criteria.Add(Expression.Eq("ParentID"), 12345));
criteria.CreateCriteria("ChildCollection1").SetFetchMode("ChildCollection2", FetchMode.EAGER);
criteria.SetResultTransformer(CriteriaUtil.DistinctRootEntity);
Parent result = (Parent) criteria.UniqueResult();


... but because I'm using 'createcriteria' on the first collection it automatically tries to use a join (I have also tried specifying the JoinType.None).... ChildCollection2 is mapped using fetch="join" but as described in the 1st scenario, this seems to be ignored...

Can anybody help please?

Thanks in anticipation, Carl


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2008 11:52 am 
Please,

Can you send us your mapping file?

Tks


Top
  
 
 Post subject:
PostPosted: Tue Oct 21, 2008 3:05 am 
Beginner
Beginner

Joined: Wed Oct 18, 2006 4:41 am
Posts: 20
Location: West Yorkshire, UK
Hi luiz,

I can't send you the entire thing I'm afraid but here's the mapped collection on the first parent object:

Code:
   <bag lazy="false" name="Stages" cascade="all" order-by="StageNo">
      <key column="ParentId" />
      <one-to-many class="Package.Stage, assembly" />
    </bag>


And this is a collection on a 'Stage':

Code:
  <bag name="Assumptions" cascade="all" fetch="join">
      <key column="StageID" />
      <one-to-many class="Package.Assumption, assembly" />
    </bag>


essentially I want the stages to be retrieved with a select, but the assumptions under that stage to be retrieved with a join....

Thanks, Carl


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2008 5:43 am 
Hi CarlHowarth,

I think your problem is due the lazy property.
Change the value of this property to TRUE (lazy="true").

When you execute a query, this property won't bring all these record once.

Hope it's help


Top
  
 
 Post subject:
PostPosted: Tue Oct 21, 2008 6:33 am 
Beginner
Beginner

Joined: Wed Oct 18, 2006 4:41 am
Posts: 20
Location: West Yorkshire, UK
Hi again - thanks, but the problem is more that I can get my first collection to load using a select, but I want the child collections to load using a join, and that's where the trouble lies.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2008 10:05 am 
Beginner
Beginner

Joined: Sun Oct 22, 2006 12:06 pm
Posts: 39
Hi! I don't know how to this with Criterias, but in HQL there is a key fetch

so try this query

select p from Parent p
join fetch p.ChildCollection c
where p.ParentID=12345

see the fetch argument? it will enable to load all entities along from childcollection without making n+1

recommend you to read a http://nhforge.org blog posts


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