所以我的查询工作正常但是当我想访问结果的第二页时,我得到了这个例外:
org.hibernate.exception.GenericJDBCException:The requested operation is not supported on forward only result sets
Caused by : com.microsoft.sqlserver.jdbc.SQLServerException: The requested operation is not supported on forward only result sets
这是我的代码
HibernatePage sp = new HibernatePage(pageNumber, pageSize);
sp.scrollableResults = query.scroll(ScrollMode.FORWARD_ONLY);
sp.totalElements = sp.calculateTotalElementsByList(query);
sp.determineElements(query);
with determineElement(query)
private void determineElements(Query query) throws HibernateException {
if (Integer.MAX_VALUE == this.pageNumber)
this.pageNumber = (getTotalNumberOfElements() / this.pageSize);
elements = query.setFirstResult((this.pageNumber * this.pageSize)-this.pageSize).setMaxResults(this.pageSize + 1).list();
}
我正在使用hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect
她是堆栈跟踪
org.hibernate.exception.GenericJDBCException: The requested operation is not supported on forward only result sets
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:52)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractResultSetProxyHandler.continueInvocation(AbstractResultSetProxyHandler.java:108)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at com.sun.proxy.$Proxy48.absolute(Unknown Source)
at org.hibernate.loader.Loader.advance(Loader.java:1651)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1982)
at org.hibernate.loader.Loader.doQuery(Loader.java:829)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
at org.hibernate.loader.Loader.doList(Loader.java:2463)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2279)
at org.hibernate.loader.Loader.list(Loader.java:2274)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1115)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at ma.softwork.commons.dao.impl.HibernatePage.determineElements(HibernatePage.java:205)
at ma.softwork.commons.dao.impl.HibernatePage.getScrollPageInstanceWithTotalByList(HibernatePage.java:180)
at ma.softwork.commons.dao.impl.HibernatePage.getHibernatePageInstance(HibernatePage.java:127)
at ma.softwork.commons.dao.impl.HibernatePage.getHibernatePageInstance(HibernatePage.java:116)
at ma.softwork.dao.impl.QueryBuilderFactory.rechercheContratsPagination(QueryBuilderFactory.java:332)
at ma.per.service.contrat.ContratService.rechercheContratsPagination(ContratService.java:250)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
我错过了什么吗?
答案 0 :(得分:0)
如果下面的表达式的值是负数,你可以得到这个错误,比如说 this.pageNumber是0 ,那么下面的表达式的值可以是负数
(this.pageNumber * this.pageSize)-this.pageSize
答案 1 :(得分:0)
似乎并非所有的Hibernate SQL Server方言版本都单独使用可滚动结果集来使分页工作。
如果使用标准方言,则使用的是2000版本。
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
使用较新的版本可以解决该问题。
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect" />