setMaxResult在Hibernate

时间:2015-09-10 06:56:53

标签: java sql-server hibernate jpa jdbc

Query query = entityManager.createQuery("select distinct(id) From Table where plant=?1").setParameter(1, plant);
        query.setFirstResult((pageNo-1) * pageSize); 
        query.setMaxResults(pageSize);
        List<String> Ids = query.getResultList();

此代码适用于SQL Server 2008,但是当我将数据库从2008迁移到2014时,这给了我以下错误:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The requested operation is not supported on forward only result sets.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.throwNotScrollable(SQLServerResultSet.java:376)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyResultSetIsScrollable(SQLServerResultSet.java:399)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.absolute(SQLServerResultSet.java:1474)
    at org.jboss.jca.adapters.jdbc.WrappedResultSet.absolute(WrappedResultSet.java:131)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractResultSetProxyHandler.continueInvocation(AbstractResultSetProxyHandler.java:104)
    ... 170 more

我试过以下: 1.将jdbc驱动程序从sqljdbc4.0.jar更改为sqljdbc4.2.jar

  1. LIMIT在sql server
  2. 中不受支持

    String query =“从表限制中选择不同的ID”                      + offset +“,”+ noOfRecords;

    1. ROW_NUMBER()不是一个好的解决方案,因为我想要不同的记录

2 个答案:

答案 0 :(得分:2)

看看Wrong hibernate dialect for MSSQL 2014。我认为这可能与针对SQL Server 2014的Hibernate解决的错误方言有关

答案 1 :(得分:0)

在此处查看此解决方法:https://stackoverflow.com/a/37423126/1255493

这解决了我的问题。