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.