-->
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.  [ 6 posts ] 
Author Message
 Post subject: HQL and null values in the select
PostPosted: Wed Mar 12, 2008 3:10 am 
Newbie

Joined: Wed Sep 12, 2007 3:13 pm
Posts: 4
I am using HQL to add a few fields from a table. Here is an example:

select (foo.valueA + foo.valueB + foo.valueC) from Foo foo where foo.bar.date = :date

I can then get the resulting value doing something like:

Code:
Double value = (Double)this.getSession().createQuery(queryString)
                  .setDate("date", bDate)
                  .uniqueResult();


If foo.valueA = 10 and foo.valueB = 15 and foo.valueC = 30 then this query returns 55 : excellent!

If foo.valueA = 10 and foo.valueB = 15 and foo.valueC = null then this query returns null and not 25: NOT excellent!

What I would hope is that in this second example the query would return the value '25' as that is the sum of those three fields as best can be determined. Is this type of behavior possible? Any help is appreciated.

Thanks,

Rob


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 3:40 am 
Beginner
Beginner

Joined: Fri Jun 25, 2004 11:47 am
Posts: 34
Hi,

Before using hibernate, I would recommend you to learn SQL otherwise you will get more serious problems...

NULL!=0. Therefore, the result you get is correct in term of sql.
Try (on oracle) SELECT 1 + 2 + NULL FROM DUAL; is not the same as SELECT 1 + 2 + 0 FROM DUAL;
Rgds


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 9:40 am 
Newbie

Joined: Wed Sep 12, 2007 3:13 pm
Posts: 4
knoll wrote:
Before using hibernate, I would recommend you to learn SQL otherwise you will get more serious problems...


Thanks for the tip.

I fully understand that NULL != 0, that is why I suggested that I would like the 2nd query to return the sum of the 3 values as best can be determined. That is also why I asked if this particular type of behavior is possible and why I didn't suggest that this type of behavior should necessarily be automatically expected.


Top
 Profile  
 
 Post subject: Possible solution
PostPosted: Wed Mar 12, 2008 11:10 am 
Newbie

Joined: Tue May 22, 2007 4:26 pm
Posts: 2
I'm not sure if this will work in HQL or not, but it seems to work in SQL on MySQL. If it doesn't work, maybe it will help lead you down the correct path.

select if(valueA is null, 0, valueA) + if (valueB is null, 0, valueB) + if (valueC is null, 0, valueC) ...

Good luck!


Top
 Profile  
 
 Post subject: Real Solution
PostPosted: Wed Mar 12, 2008 11:38 am 
Newbie

Joined: Tue May 22, 2007 4:26 pm
Posts: 2
I found a real solution to this problem that should work in HQL, the coalesce() function. It takes a list of parameters and returns the first non-null value.

select coalesce(valueA, 0) + coalesce(valueB, 0) + coalesce(valueC, 0) from ...


Top
 Profile  
 
 Post subject: Re: Real Solution
PostPosted: Wed Mar 12, 2008 11:48 am 
Newbie

Joined: Wed Sep 12, 2007 3:13 pm
Posts: 4
Thanks -- this solves the problem!


NateRedding wrote:
I found a real solution to this problem that should work in HQL, the coalesce() function. It takes a list of parameters and returns the first non-null value.

select coalesce(valueA, 0) + coalesce(valueB, 0) + coalesce(valueC, 0) from ...


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