-->
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.  [ 11 posts ] 
Author Message
 Post subject: hql returns System.Object[]
PostPosted: Thu Oct 11, 2007 5:51 am 
Newbie

Joined: Thu Oct 11, 2007 5:40 am
Posts: 8
I am using the following hql statement to retrieve a list of BreedingProject objects:

string hql =
@"from BreedingProject as bp left join bp._crops cs
where bp._year = :requestYear and
(cs in (:cropList) or cs is null) ";

But instead of a list of BreedinProject objects it returns a list of System.Object[] objects with a BreedingProject object at index 0 and a Crop (=cs) object at index 1.
I more or less do understand that this is caused by the left join call, but what hql should I use in order to obtain a list of BreedingObjects instead, against the same where clause?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 12, 2007 10:15 am 
Senior
Senior

Joined: Sat May 14, 2005 8:40 am
Posts: 130
select bp from BreedingProject as bp left join bp._crops cs
where bp._year = :requestYear and
(cs in (:cropList) or cs is null)

_________________
Cuyahoga


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 15, 2007 9:49 am 
Newbie

Joined: Thu Oct 11, 2007 5:40 am
Posts: 8
Of all people in the NHibernate community.... Thnx Martijn!


Top
 Profile  
 
 Post subject: Next challenge...
PostPosted: Tue Oct 16, 2007 4:29 am 
Newbie

Joined: Thu Oct 11, 2007 5:40 am
Posts: 8
Modifying the hql as suggested works fine. I now get a list of bp objects returned. Howerver, this list contains multiple instances of the same object, where I expected a unique list of bp objects. Is there a way to get a unique list or do I have to filter doubles myself?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 10:29 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
You have to filter them yourself or you can use an ResultSetTransformer to get a unique list. Beware of using the ResultSetTransformer when setting a max limit on results, however as the max limit is on the results *before* the duplicates are removed.

To filter them yourself you can create a HashedSet and load it with the IList result of your query. The set will only contain the unique results.

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 10:47 am 
Newbie

Joined: Thu Oct 11, 2007 5:40 am
Posts: 8
Indeed, the Hashtable is what I use now to filter duplicates, although I need the database id as key, since all objects are different instances as well. Will leave it like it is now. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 4:44 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Actually, when you get duplicate results from a query as you are each of the duplicate entities are in fact the same object - it's just been added to the collection multiple times.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 4:50 pm 
Newbie

Joined: Tue Oct 16, 2007 4:45 pm
Posts: 6
hello, i have a question of this too.. :D

what if i want to get this..

select bp, cs from BreedingProject as bp left join bp._crops cs
where bp._year = :requestYear and
(cs in (:cropList) or cs is null)

can i get it someway???

thanks!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 2:28 am 
Newbie

Joined: Thu Oct 11, 2007 5:40 am
Posts: 8
Well, that is what I got the first time. If you omit select bp from the hql syntax, you will get a Systme.Object[] with bp at index 0 and cs at index 1


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 2:43 pm 
Newbie

Joined: Tue Oct 16, 2007 4:45 pm
Posts: 6
ok i know that... but i want a object[] that has an object only, not an array of object...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 18, 2007 2:55 am 
Newbie

Joined: Thu Oct 11, 2007 5:40 am
Posts: 8
I can only say that ultimately the IQuery.list() method will return a list of objects and depending on the hql statement this will either be a list of bp objects or a list of System.Object[] objects.
For the rest, I do not completely understand what you want to get returned from the IQuery


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