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.  [ 5 posts ] 
Author Message
 Post subject: HQL: Aggregate function on single column, list returned
PostPosted: Wed Mar 12, 2008 7:44 am 
Newbie

Joined: Wed Sep 13, 2006 4:12 am
Posts: 10
Hello everyone,

i'm trying to retrieve a single datetime value using an hql query:

Code:
select min(Answers.Created) from Answers aw where aw.Contactid.Id = 10 and aw.Formid.Id = 1002


executed via ISession.CreateQuery().UniqueResult() this query throws an NonUniqueResultException. When executed by ISession.CreateQuery().List() i get a collection of Answers-Objects (all objects that fit into the where-clause). Shouldn't i just get a single object of type DateTime when selecting a single property? The selected Property is mapped as a simple Property of type DateTime, not nullable.

Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 8:14 am 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
Lets se the CreateQuery and UniqueResult parts of the query, code like below should work fine:

Code:
DateTime dt = s.CreateQuery("select min(a.Created) from Answer a").UniqueResult<DateTime>();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 8:53 am 
Newbie

Joined: Wed Sep 13, 2006 4:12 am
Posts: 10
sorry, this still leads to an NHibernate.NonUniqueResultException. I think i'm going to make a short workaround for this and investigate my problem within a small sample project. Maybe there is something about my mapping or class definitions that causes this behavior.


Top
 Profile  
 
 Post subject: Re: HQL: Aggregate function on single column, list returned
PostPosted: Wed Mar 12, 2008 10:03 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try it with aw.Created instead of Answers.Created. I ran into a similar problem a few days ago and found out that you have to use the alias in the select if you specify one in the from clause. Otherwise you get the object instead of the selected properties.

Code:
select min(aw.Created) from Answers aw where aw.Contactid.Id = 10 and aw.Formid.Id = 1002


I can't remember reading that explicitly in the documentation, but that my rather be due to my reading ... ;-)

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 10:17 am 
Newbie

Joined: Wed Sep 13, 2006 4:12 am
Posts: 10
thats it, using the alias within the aggregate function works. I wonder if this behavior is intended.

Thank you!


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