When I try to compare boolean field in a query with a boolean constant, Hibernate first substitutes 1 for true and 0 for false and the whole query goes incorrect, as "Data types must be compatible".
As a fix, I explicitly specified the substitution rule in the hibernate configuration file, it looks like
Code:
<attribute name="QuerySubstitutions">true true, false false</attribute>
(now commented)
Imho it's an ugly trick. Isn't there another way to forbid the substitution?
I've pasted an excerpt from server log and from config file. Note what happens to "where info.active=true".
Hibernate version:
3.1
Name and version of the database:
MaxDB 7.5.00
Mapping documents:
<server>
<mbean code="org.hibernate.jmx.HibernateService" name="jboss.jca:service=ipub/PortalKernelService">
<depends>jboss.jca:service=RARDeployer</depends>
<attribute name="CacheProviderClass">org.hibernate.cache.EhCacheProvider</attribute>
<attribute name="JndiName">java:/ipub/hibernate/KernelHibernateFactory</attribute>
<attribute name="Datasource">java:/jdbc/ipub</attribute>
<attribute name="Dialect">org.hibernate.dialect.SAPDBDialect</attribute>
<attribute name="ShowSqlEnabled">false</attribute>
<attribute name="UserTransactionName">UserTransaction</attribute>
<attribute name="TransactionStrategy">org.hibernate.transaction.JTATransactionFactory</attribute>
<attribute name="FlushBeforeCompletionEnabled">true</attribute>
<attribute name="AutoCloseSessionEnabled">true</attribute>
<attribute name="TransactionManagerLookupStrategy">org.hibernate.transaction.JBossTransactionManagerLookup</attribute>
<!--
I had to add this in order to fix the strange Hibernate behaviour: for some reason, it does
substitute 'true' with 1 and 'false' with 0, which is incorrect
-->
<!--<attribute name="QuerySubstitutions">true true, false false</attribute>-->
<attribute name="MapResources">
........
</attribute>
</mbean>
</server>
Debug level Hibernate log excerpt:
2006-03-29 19:07:33,642 DEBUG [org.hibernate.engine.query.QueryPlanCache] located HQL query plan in cache (select count(info) from com.smartphonelabs.portal.values.UserInfo as info where
info.active=true and info.uid != 'UIDREQUEST')
2006-03-29 19:07:33,642 DEBUG [org.hibernate.engine.query.HQLQueryPlan] find: select count(info) from com.smartphonelabs.portal.values.UserInfo as info where info.active=true and info.uid != 'UIDREQUEST'
2006-03-29 19:07:33,642 DEBUG [org.hibernate.engine.QueryParameters] named parameters: {}
2006-03-29 19:07:33,642 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 46)
2006-03-29 19:07:33,642 DEBUG [org.hibernate.jdbc.ConnectionManager] opening JDBC connection
2006-03-29 19:07:33,642 DEBUG [org.hibernate.SQL] select count(userinfo0_.p_id)
as col_0_0_ from user_info userinfo0_ where
userinfo0_.active=1 and userinfo0_.user_id<>'UIDREQUEST'
2006-03-29 19:07:33,642 DEBUG [org.hibernate.jdbc.AbstractBatcher] preparing statement
2006-03-29 19:07:33,643 DEBUG [org.hibernate.util.JDBCExceptionReporter] could not execute query [select count(userinfo0_.p_id) as col_0_0_ from user_info userinfo0_ where userinfo0_.active=1 and userinfo0_.user_id<>'UIDREQUEST']
com.sap.dbtech.jdbc.exceptions.DatabaseException: [-8006] (at 93): Data types must be compatible
19:07:33,646 INFO [STDOUT] org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
.........