Oracle jdbc 驱动程序的 Hibernate 的 setFirstResult() 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/520869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:45:53  来源:igfitidea点击:

Hibernate's setFirstResult() issue with Oracle jdbc driver

javaoraclehibernatejdbc

提问by Bernardo

I'm trying to do pagination with Hibernate using setFirstResult()and setMaxResults()but I'm not getting the expected results when setting the first result to 0.

我试图做分页与Hibernate使用setFirstResult()setMaxResults(),但在第一个结果设置为0时,我没有得到预期的结果。

When doing the following:

执行以下操作时:

Query query = session.createQuery(queryString);  
query.setFirstResult(0);  
query.setMaxResults(30);  
List list = query.list();  //list.size() returns 10  

but if I set the first result to 1 (or anything different than 0 for that matter):

但是如果我将第一个结果设置为 1(或者任何与 0 不同的值):

query.setFirstResult(1);  
query.setMaxResults(30);  
List list = query.list();  //list.size() returns 30  

I read this is a known bug in the jdbc driver, but I searched for a solution and I can't seem to find it. Has anyone run across anything similar and found a fix for it?

我读到这是 jdbc 驱动程序中的一个已知错误,但我搜索了一个解决方案,但似乎找不到它。有没有人遇到过类似的事情并找到了解决方法?

回答by Bernardo

Apparently adding setFetchSize()does the trick. So something like this now works perfectly:

显然添加setFetchSize()可以解决问题。所以像这样的事情现在完美地工作:

query.setFirstResult(0);  
query.setMaxResults(30);  
query.setFetchSize(30);  
List list = query.list(); //list.size() now returns... wait for it... 30

回答by brabenetz

Another Solution is to implement your own Oracle Dialect:

另一个解决方案是实现您自己的 Oracle 方言:

public class Oracle10gDialectLimitBugfix extends Oracle10gDialect {
    @Override
    public boolean forceLimitUsage() {
        return true;
    }
}

See https://forum.hibernate.org/viewtopic.php?p=2379096

https://forum.hibernate.org/viewtopic.php?p=2379096

UPDATE: It seems to be fixed in Oracle 11.2.0.1.0

更新:它似乎已在 Oracle 11.2.0.1.0 中修复