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.  [ 4 posts ] 
Author Message
 Post subject: Enumerable() causes hundreds of queries
PostPosted: Sun Mar 02, 2008 2:16 pm 
Beginner
Beginner

Joined: Wed Aug 01, 2007 4:28 pm
Posts: 23
I'm have a State class the represents states of the unions. It is generally a many-to-one to other classes. When i use Enumerable() for some reason, it runs a query per each record in the database. I'm just trying to get all the states, but sql profiler shows it getting just the ID for all the states, then running one query per state to get all the other information, resulting in 50+ queries.

My code is:

Session..CreateQuery("FROM State ORDER BY Abbreviation ").Enermable()

Here's my mapping:

<class name="State" table="State">
<!-- Primary key -->
<id column="StateId" type="int" name="ID" access="field.camelcase-underscore">
<generator class="native">
</generator>
</id>

<!-- One-To-Many relationships -->
<bag name="RegistrantList" inverse="true" lazy="true" cascade="all-delete-orphan" >
<key column="StateId" />
<one-to-many class="Registrant" />
</bag>
<bag name="StateSandlotprofilelist" inverse="true" lazy="true" cascade="all" >
<key column="StateID" />
<one-to-many class="SandlotProfile" />
</bag>
<bag name="IssuingstateSandlotprofilelist" inverse="true" lazy="true" cascade="all" >
<key column="IssuingStateID" />
<one-to-many class="SandlotProfile" />
</bag>

<!-- Standard Properties/Fields -->
<property column="Name" type="System.String" name="Name" not-null="true" access="field.camelcase-underscore" />
<property column="Abbreviation" type="System.String" name="Abbreviation" not-null="false" access="field.camelcase-underscore" />
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 9:01 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
That is exactly what Enumerable() is designed to do. See docs here:
http://www.hibernate.org/hib_docs/nhibe ... a-querying

Use List() instead.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 1:18 pm 
Beginner
Beginner

Joined: Wed Aug 01, 2007 4:28 pm
Posts: 23
You're my hero lately!

So I was using enmerable() because I thought it would eliminate duplicate objects from my result set. For instance, if I have this:

SELECT c
FROM Customer
JOIN FETCH c.Orders

I thought that would give me a bunch of duplicate customers, because i'm asking it to preload the Orders collection. A normal SQL join would do that--it would return repeating customer fields, and then the order fields.

Does this make sense?

Thanks,
Craig


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 1:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
If my guess is correct, the HQL you need is this:
Code:
SELECT DISTINCT c
FROM Customer
LEFT JOIN FETCH c.Orders

It should fetch the Orders via a left join and also eliminates the duplication of Customers.

_________________
Karl Chu


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