-->
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: IfNull (MySQL) and HQL
PostPosted: Wed Feb 16, 2005 6:20 am 
Newbie

Joined: Wed Feb 16, 2005 6:09 am
Posts: 3
Hi,

I'm translating an SQL query to HQL (for a MySQL DB). It uses a series of outer joins and the ifnull function to translate nulls to known values in order that every row is returned (not just those containing non-nulls):

Does hibernate support something like the ifnull function or do I have to uses a native SQL query in this instance:

The original SQL looks something like:

select
u.dsivId, ifnull(s.semaineId,"0000W0"), sc.saisieActivityId,
sum(ifnull(sc.charge,0))
from users u
left outer join
saisies s on s.userId = u.dsivId
left outer join
saisie_activities sa on sa.saisieId = s.id
left outer join
saisie_charges sc on sc.saisieActivityId = sa.id
group by
s.semaineId, u.dsivId, sc.saisieActivityId
order by
u.dsivId, s.semaineId


TIA

Tom


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 8:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can use native SQL functions in HQL.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 8:41 am 
Newbie

Joined: Wed Feb 16, 2005 6:09 am
Posts: 3
Thanks for your reply michael.

I know I can call native SQL but I would like to know if I can do an ifnull with "pure" HQL. Shouldn't we avoid using native SQL if at all possible ?

Tom


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 8:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can just use ifnull() in your HQL query, if your DB supports it


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 8:52 am 
Newbie

Joined: Wed Feb 16, 2005 6:09 am
Posts: 3
It doesn't seem to. Here's my HQL query with the ifnull in. This query works without the ifnull bit (except that I have a null value instead of 16 of course).

The error I get is :

Code:
undefined alias: ifnull [select  s.user, s.semaineId, sa.id, sum(ifnull(sc.charge, "16")) Saisie as s left outer join s.activities as sa left outer join sa.charges as sc group by s.semaineId, s.user, sa.id order by s.user, s.semaineId ]


Thanks for your help with this

Code:
select  s.user, s.semaineId, sa.id, sum(ifnull(sc.charge, "16"))
from com.sncf.vfe.app.gact.metier.Saisie as s
left outer join s.activities as sa
left outer join sa.charges as sc
group by s.semaineId, s.user, sa.id
order by s.user, s.semaineId


Top
 Profile  
 
 Post subject: Re: IfNull (MySQL) and HQL
PostPosted: Tue Nov 06, 2012 2:57 pm 
Newbie

Joined: Fri Feb 29, 2008 1:07 pm
Posts: 5
Solution is to either use COALESCE(...) - some say NULLIF(..)
see http://stackoverflow.com/questions/1657637/ifnull-equivalent-in-hibernate-query-language

or use CASE WHEN ... THEN ... ELSE ... END (worked in my case)
see http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/queryhql.html#queryhql-expressions


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.