-->
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.  [ 3 posts ] 
Author Message
 Post subject: Query with sum(..) returns null instead of expected value
PostPosted: Mon Mar 13, 2006 2:29 am 
Newbie

Joined: Mon Feb 27, 2006 6:47 pm
Posts: 8
Why is the query returning null when I execute the following code:

Query query = session.createQuery("select sum(wl.hours) from WorkLog wl where wl.owner.id = ?");
query.setString(0,"myId");
Object result = query.uniqueResult();

------------------

The query is just trying to get me the sum of hours. I'm expecting this to return a Double which is the type for my hours property. I see in the logs that hibernate generates the right SQL and if I execute it in an SQL editor it returns the expected results. But when runed within hibernate it returns null,
Any ideas?


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 12:20 pm 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
Hello,
Of what type is the wl.owner.id in the database? Is it of type char?
If so, make sure, that the length of the string is equal to the size in the database (--> padding).

Hope this helps!
Urs


Top
 Profile  
 
 Post subject: Re: Query with sum(..) returns null instead of expected value
PostPosted: Thu Sep 03, 2009 12:11 am 
Beginner
Beginner

Joined: Thu Jun 21, 2007 1:47 pm
Posts: 46
I seem to be experiencing the same issue, a few years later. Is this expected behavior? Here's an example SQL output:

Code:
   select
        sum(this_.amount) as y0_
    from
        public.Journal this_
    where
        this_.amount<>?
        and this_.removed=?
        and this_.draft=?
        and this_.account_id=?
        and this_.date>=?
        and this_.currency_id=?
        and this_.date<=?


My projection is quite simple: setProjection(sum("amount")); I'm not setting the ResultsTransformer.

When I call uniqueResult() I get a null value, which is strange - I know there are zero rows but I think hibernate should return 0 in that case, shouldn't it?


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