-->
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: ICriteria List multiple results
PostPosted: Fri Jun 12, 2009 3:28 pm 
Newbie

Joined: Tue Jun 09, 2009 1:43 pm
Posts: 10
Hello,

I am using an ICriteria object to query for a list of objects, and they are returned using the List(). However, I noticed that n+1 queries are run (one to get id's and one for every object). Is there a way to optimize this, so just a single query is run that gets all objects at once.


Top
 Profile  
 
 Post subject: Re: ICriteria List multiple results
PostPosted: Mon Jun 15, 2009 1:22 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Are the main objects loaded this way ? In that case they probably are lazy-loaded. If it's about some child objects, try criteria.SetFetchMode("path", FetchMode.Join) for that associatione.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: ICriteria List multiple results
PostPosted: Mon Jun 15, 2009 12:28 pm 
Newbie

Joined: Tue Jun 09, 2009 1:43 pm
Posts: 10
Thanks for the reply. I am actually already using joins of the object's mapping sets. I am not using lazy loading, because it would cause issues with my application.

So, I guess what I would like to know if there is a way to do something similar for the top level objects


Top
 Profile  
 
 Post subject: Re: ICriteria List multiple results
PostPosted: Mon Jun 15, 2009 12:30 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post the code ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: ICriteria List multiple results
PostPosted: Mon Jun 15, 2009 1:38 pm 
Newbie

Joined: Tue Jun 09, 2009 1:43 pm
Posts: 10
I don't know if this helps, but I am running the query like this:

IList<RuleBase> rules = crt.Add(NHibernate.Criterion.Expression.In("SegmentId", domainList))
.AddOrder(NHibernate.Criterion.Order.Asc("RuleOrder"))
.SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer())
.List<RuleBase>();

xml:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="RuleBase" table="rules">

<id name="ID" column="id" type="System.Int64" unsaved-value="null">
<generator class="assigned"/>
</id>

<discriminator column="type" type="System.String"></discriminator>
<property name="Name" column="obj_name" type="System.String" length="256" />
<property name="RuleOrder" column="rule_order" type="System.Double"/>
<property name="SegmentId" column="segment_id" type="System.Int64" />

<subclass name="MyRule" discriminator-value="rule">

<!-- mappings -->
<set name="Mapping1" table="map1" fetch="join">
<key column="rule_id"/>
<element column="map_id" type="System.Int64"/>
</set>


Top
 Profile  
 
 Post subject: Re: ICriteria List multiple results
PostPosted: Mon Jun 15, 2009 3:45 pm 
Newbie

Joined: Tue Jun 09, 2009 1:43 pm
Posts: 10
OK, i think i figured this one out...

My Rules object has two sets in it that were mapped to the same table. The sets where using a where clause. It looked like it needed to do a second query per object to get the second set.


Top
 Profile  
 
 Post subject: Re: ICriteria List multiple results
PostPosted: Tue Jun 16, 2009 12:49 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
As far as I can remember, if you have a second association to the same table, you will always get single selects instead of a join, regardless if you have a where clause or not.

_________________
--Wolfgang


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.