-->
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.  [ 8 posts ] 
Author Message
 Post subject: Why does Hibernate remove whitespaces from my query?
PostPosted: Mon Mar 10, 2008 4:33 pm 
Newbie

Joined: Fri Feb 16, 2007 7:13 pm
Posts: 6
Hello,

In my HQL query I have the following CASE statement inside an aggregate SUM function:

Code:
sum(case when airmonth = 1 and airyear = 2007 then cost else 0 end)



The problem is when hibernate generates the SQL code it removes all the white spaces from inside the case statement and generates the following:

Code:
sum(casewhenairmonth=1andairyear=2007thencostelse0end)



Which of course causes the query to fail. I am using Hibernate 2 and PostgreSQL Dialect. Any help will be greatly appreciated. Thanks.


Last edited by Bashar on Tue Mar 11, 2008 1:08 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 6:52 pm 
Newbie

Joined: Fri Feb 16, 2007 7:13 pm
Posts: 6
This is really bizarre, it looks like Hibernate (version 2 at least) will remove all white spaces from anything inside an aggregate function.

For example:

Code:
sum(THIS MAKES NO SENSE)


will output the SQL:

Code:
sum(THISMAKESNOSENSE)



Can someone please verify this and let me know why does Hibernate do this and if there is any workaround around it ?

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 1:02 pm 
Newbie

Joined: Fri Feb 16, 2007 7:13 pm
Posts: 6
Anybody ?

Sorry If I am being impatient here but this is a major blocker for me and I hit the wall with it so I will really appreciate any help.

Thanks,


Top
 Profile  
 
 Post subject: Re: Why does Hibernate remove whitespaces from my query?
PostPosted: Tue Mar 11, 2008 1:08 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Are you sure you can use case in an aggregate function? It doesn't make much sense to me.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 1:23 pm 
Newbie

Joined: Fri Feb 16, 2007 7:13 pm
Posts: 6
Yes you can. I run the SQL query against my database with no problems. As a matter of fact this is the preferred way of creating cross tab queries and pivot tables in SQL.

http://www.simple-talk.com/sql/t-sql-pr ... es-in-sql/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 1:27 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Bashar wrote:
Yes you can. I run the SQL query against my database with no problems. As a matter of fact this is the preferred way of creating cross tab queries and pivot tables in SQL.

http://www.simple-talk.com/sql/t-sql-pr ... es-in-sql/



Thanks for the reference. One test you can do is to test the same thing with a native query and see how it reacts. Let me know the results.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 2:19 pm 
Newbie

Joined: Fri Feb 16, 2007 7:13 pm
Posts: 6
I found the solution!

Its not the most elegant but it works.

Use a comment instead of whitespace to delimit your tokens. For example:

Code:
sum(case when airmonth = 1 and airyear = 2007 then cost else 0 end)


becomes

Code:
sum(case/**/when/**/airmonth=1/**/and/**/airyear=2007/**/then/**/cost/**/ else/**/0/**/end)


Its ugly but it works for now!


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 4:52 am 
Newbie

Joined: Wed May 28, 2008 7:18 am
Posts: 6
Bashar,

I have hit the wall too with crosstab queries! Would you be kind enough to explain how you achieved this?

Thanks
Samuel


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