Hibernate incorrectly binds named parameters when filter is enabled.
I have two classes: Issue and Comment. Both classes have the same filter defined in their mapping files:
Code:
<filter name="projectFilter" condition=":projectPK = PROJECT"/>
<filter-def name="projectFilter">
<filter-param name="projectPK" type="java.lang.Long"/>
</filter-def>
This HQL works fine:
Code:
select count(*) from Issue as i
where
exists (from Comment c where c.PK = (select max(c2.PK) from Comment c2 where c2.issue = i)) and
i.text like :textParam
But almost the same HQL (only order of predicates in where clause is changed) generates error:
Code:
select count(*) from Issue as i
where
i.text like :textParam and
exists (from Comment c where c.PK = (select max(c2.PK) from Comment c2 where c2.issue = i))
Code:
java.sql.SQLException
Error converting data type nvarchar to numeric.
SQLState: 37000
errorCode: 8114
Stack Trace:
* net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:364)
* net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2754)
* net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2195)
* net.sourceforge.jtds.jdbc.TdsCore.isDataInResultSet(TdsCore.java:784)
* net.sourceforge.jtds.jdbc.JtdsResultSet.<init>(JtdsResultSet.java:143)
* net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:378)
* net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:672)
* org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:92)
* org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:137)
* org.hibernate.loader.Loader.getResultSet(Loader.java:1676)
* org.hibernate.loader.Loader.doQuery(Loader.java:662)
* org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
* org.hibernate.loader.Loader.doList(Loader.java:2147)
* org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026)
* org.hibernate.loader.Loader.list(Loader.java:2021)
* org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)
* org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:296)
* org.hibernate.impl.SessionImpl.list(SessionImpl.java:985)
* org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
* org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:650)
Generated SQL from second HQL looks like this
Code:
select count(*) as col_0_0_ from ISSUE issue0_
where
:projectFilter.projectPK = issue0_.PROJECT and
(issue0_.TEXT like ?) and
(exists (select comment1_.PK from COMMENT comment1_ where :projectFilter.projectPK = comment1_.project and comment1_.PK=(select max(comment2_.PK) from COMMENT comment2_ where :projectFilter.projectPK = comment2_.project and comment2_.ISSUE=issue0_.PK)))
Something gets broken with binding phase where named parameters and parameters from multiple filters are set. I believe that named parameters and filter parameters are mixed and bound in wrong order.
Hibernate bug?
Hibernate version: 3.0.5
Name and version of the database you are using: MS SQL Server