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: DistinctRootEntityResultTransformer and HQL
PostPosted: Tue Oct 23, 2007 7:11 pm 
Newbie

Joined: Thu Oct 26, 2006 4:02 pm
Posts: 12
Is DistinctRootEntityResultTransformer supported with HQL? The following is a sample Northwind system where Customer has a set of Orders. Most of the time I want to lazy load the Orders collection, but for this specific case I want to be able to load all of the Customer objects with their Orders collection already initialized.

Code:
IQuery query = Session.CreateQuery("select c from Customer c join fetch c.Orders");
query.SetResultTransformer(new DistinctRootEntityResultTransformer());
IList results = query.List();

When I run this code, I get the following exception:

Code:
Unable to cast object of type 'Northwind.Customer' to type 'System.Object[]'.

I've been able to achieve the same behavior by using a criteria query.

Code:
ICriteria criteria = Session.CreateCriteria(typeof(Customer));
criteria.SetFetchMode("Orders", FetchMode.Join);
criteria.SetResultTransformer(new DistinctRootEntityResultTransformer());
IList results = criteria.List();

However, I really would like to use HQL so that I can take advantage of some other features in more complex queries that require the same principle. Is DistinctRootEntityResultTransformer not supported with HQL?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 2:31 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
HQL do support distinct keyword. Try something like

select distinct c from Customer c join fetch c.Orders

_________________
Please rate this post if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 11:23 am 
Newbie

Joined: Thu Oct 26, 2006 4:02 pm
Posts: 12
The distinct keyword doesn't do what I want. When I tried using it, it added the distinct keyword to the SQL that was generated but since I'm doing a join on the Orders table I ended up with with a bunch of duplicate customers that each had 1 distinct order. I'm pretty sure that I'm supposed to use the DistinctRootEntityResultTransformer as I've used it lots before when doing a Criteria query, but I'm just wading into HQL for the first time and want the same behavior.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 1:47 pm 
Newbie

Joined: Thu Oct 26, 2006 4:02 pm
Posts: 12
I found the answer. It's a bug in 1.2.0 but is fixed in the development trunk. http://jira.nhibernate.org/browse/NH-1018


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.