-->
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: SQLServer/PostgreSQL and HQL Boolean value '= true'
PostPosted: Thu Jul 28, 2005 1:12 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Hibernate version:3.0.5

I have run into a problem with trying to run Hibernate on both SQLServer and PostgreSQL databases, dealing with Boolean fields. I can't find anything in the forums or FAQs. I've probably missed something obvious, but I guess I need someone to point it out to me.

If I have a property MyFlag, type "java.lang.Boolean" in MyTable, I can't figure out how to write HQL using that field that will run on both SQLServer and PostgreSQL.

If I say

Code:
from MyTable mytable where mytable.MyFlag = true


it runs on PostgreSQL, but SQLServer complains "Invalid column name 'true'". If I say

Code:
from MyTable mytable where mytable.MyFlag = 1


it runs on SQLServer but PostgreSQL complains about converting from boolean to integer.

I understand that I can do something like:

Code:
<property name="hibernate.query.substitutions">true 1, false 0</property>


but I can only use that for SQLServer, and it will still break on PostgreSQL.

Our solution for now is to always use parameters for booleans:

Code:
from MyTable mytable where mytable.MyFlag = :myflag


and then in code do

Code:
query.setBoolean("myflag", Boolean.TRUE);


which works on both databases (uses 1 for SQLServer and true for PostgreSQL). But if using a parameter can get Hibernate to translate correctly for each database, it seems like just saying 'mytable.MyFlag = true' ought to get translated just the same.

What am I missing?? How can I get Hibernate to convert HQL using '= true' into '= 1' without breaking PostgreSQL??

Thanks in advance.


Top
 Profile  
 
 Post subject: SQLServer/PostgreSQL and HQL Boolean value '= true'
PostPosted: Thu Jul 28, 2005 1:34 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I suspect that in you case setting appropriate config property for session might help, something like this for postgres:
<hibernate.query.substitutions">true='1', false='0'</property>
and for SQLServer
<hibernate.query.substitutions">true=1, false=0</property>

Note lack of quots for SQLServer


Please also see my comments on JIRA issue
http://opensource.atlassian.com/project ... se/HHH-477

There I have proposed changes in Hibernate to address 'boolean' values handling.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 3:14 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Thanks for the tips. Actually, in order to be more database vendor agnostic, I was looking for a setting that would work for BOTH (actually for any db). But I just tried it out, and it looks like using a quoted '0' and '1' works on SQLServer as well as PostgreSQL. So this will work for now, since we will only be running on SQLServer or PostgreSQL in the near future.

My question for the Hibernate Team is why true and false aren't keywords in HQL? It seems that saying '<booleanfield> = true' in HQL ought to work for boolean properties on any database. Then I wouldn't be required to set hibernate.query.substitutions for different dbs.


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.