I am upgrading to 5.2.5 from 4.2.6 and I'm seeing an error in all of my Criteria queries that set a max row count using setMaxResults().
I understand that the Criteria API has been deprecated, but I am doing this work on a product that has no developers assigned to it (for various business reasons). Re-writing the queries we do use would involve more time & effort than management would like us to spend.
I'm looking for suggestions for a simple workaround, as this does look like a bug in Hibernate. I've read many of the posts describing the JPA query API, but using that at this point would involve significant re-factoring.
The code is:
Code:
Criteria criteria = session.createCriteria(CertRequest.class);
List<CertRequest> certRequests = (List<CertRequest>)criteria
.setFirstResult(0)
.setMaxResults(BATCH_SIZE)
.addOrder(Order.asc("modifyTime"))
.list();
This generates the following SQL using Hibernate 5.2.5:
Quote:
/* criteria query */ select
TOP ? this_.id as id1_25_0_,
this_.revision as revision2_25_0_,
this_.cert as cert3_25_0_,
this_.csr as csr4_25_0_,
this_.expirationDate as expirati5_25_0_,
this_.modifyTime as modifyTi6_25_0_,
from
dbo.CertRequest this_
order by
this_.modifyTime asc
The same code generates the following query using Hibernate 4.2.6 (the max results argument is embedded in the SQL statement):
Quote:
Hibernate:
/* criteria query */ select
top 101 this_.id as id1_22_0_,
this_.revision as revision2_22_0_,
this_.cert as cert3_22_0_,
this_.csr as csr4_22_0_,
this_.expirationDate as expirati5_22_0_,
this_.modifyTime as modifyTi6_22_0_,
from
CertRequest this_
order by
this_.modifyTime asc
The error is: incorrect syntax near '@P0' - the stack trace is below:
Quote:
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2123)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1911)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1887)
at org.hibernate.loader.Loader.doQuery(Loader.java:932)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
at org.hibernate.loader.Loader.doList(Loader.java:2615)
at org.hibernate.loader.Loader.doList(Loader.java:2598)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2430)
at org.hibernate.loader.Loader.list(Loader.java:2425)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1878)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:365)
at com.XXX.mgmt.testing.TestPeD.testCriteria(TestPeD.java:113)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1655)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:440)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:385)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2444)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:166)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:297)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
... 15 more