Java HQL - 用于分页的行标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/489360/
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-08-11 15:17:14  来源:igfitidea点击:

HQL - row identifier for pagination

javahibernatepaginationhql

提问by user57701

Does anyone know if HQL has a keyword to identify rows such as ROWID or ROWNUM?

有谁知道 HQL 是否有一个关键字来标识行,例如 ROWID 或 ROWNUM?

I would like to implement pagination with HQL but I am not able to use .setMaxResult() or .setFirstResult() because I don't work with the session object directly and therefore don't use the Query object but simply create my query as a string and use the .find() method.

我想使用 HQL 实现分页,但我无法使用 .setMaxResult() 或 .setFirstResult() 因为我不直接使用会话对象,因此不使用 Query 对象,而只是将我的查询创建为一个字符串并使用 .find() 方法。

I tried using LIMIT and OFFSET in my query, but HQL seems to be ignoring these keywords and is returning the whole result to me no matter what.

我尝试在查询中使用 LIMIT 和 OFFSET,但 HQL 似乎忽略了这些关键字,无论如何都会将整个结果返回给我。

I'm also not able to use Hibernate criteria because it does not have support for the "HAVING" clause that appears in my query.

我也无法使用 Hibernate 标准,因为它不支持出现在我的查询中的“HAVING”子句。

My last resort is to restrict the result set using the ROWNUM/ROWID keyword. Does anyone else have any other suggestions?

我最后的手段是使用 ROWNUM/ROWID 关键字来限制结果集。还有其他人有其他建议吗?

回答by sleske

Well, you can in principle access ROWNUM/ROWID from HSQL (though I've never used it). See e.g. Ron's Weblog. That should work.

好吧,原则上您可以从 HSQL 访问 ROWNUM/ROWID(尽管我从未使用过它)。参见例如Ron 的博客。那应该工作。

But I'd like to point out that you're really working against Hibernate and HSQL if you do it like that. The proper way is to use setMaxResult() & friends. If you cannot do that b/c of your architecture, I'd at least reconsider my architectural decisions. I know these are always tough changes to make, but it might be worth it.

但是我想指出,如果您这样做,您实际上是在反对 Hibernate 和 HSQL。正确的方法是使用 setMaxResult() 和朋友。如果你不能对你的架构做那个 b/c,我至少会重新考虑我的架构决定。我知道这些总是很难做出改变,但这可能是值得的。

回答by Andreas Petersson

this is one situation where hibernate shines:

这是休眠的一种情况:

typical solution with hql query.

hql 查询的典型解决方案。

int elementsPerBlock = 10;
int page = 2;

return  getSession().createQuery("from SomeItems order by id asc")
            .setFirstResult(elementsPerBlock * (page-1) + 1 )
            .setMaxResults(elementsPerBlock)
            .list();

hibernate will translate this to a pattern that is understood by the database according to its sql dialect. on oracle it will create a subselect with ROWNUM < X. on postgresql it will issue a LIMIT / OFFSET on msSQL server it will issue a TOP..

hibernate 会将其转换为数据库根据其 sql 方言理解的模式。在 oracle 上,它将创建一个 ROWNUM < X 的子选择。在 postgresql 上,它将在 msSQL 服务器上发出一个 LIMIT/OFFSET,它将发出一个 TOP..

to my knowledge it is not possible to achieve this with "pure" hql.

据我所知,使用“纯”hql 是不可能实现这一点的。

回答by Ranga Reddy

Pagination using Query Interface:

使用查询接口进行分页:

There are two methods of the Query interface for pagination.

Query接口有两种分页方法。

1. Query setFirstResult(int startPosition):This method takes an integer that represents the first row in your result set, starting with row 0.

1. Query setFirstResult(int startPosition):此方法采用一个整数,表示结果集中的第一行,从第 0 行开始。

2. Query setMaxResults(int maxResult):This method tells Hibernate to retrieve a fixed number maxResults of objects. Using above two methods together, we can construct a paging component in our web or Swing application.

2. Query setMaxResults(int maxResult):这个方法告诉Hibernate检索固定数量的maxResults对象。结合使用以上两种方法,我们可以在我们的 web 或 Swing 应用程序中构建一个分页组件。

Example:

例子:

Query query = session.createQuery("FROM Employee");
query.setFirstResult(5);
query.setMaxResults(10);
List<Employee> list = query.list();
for(Employee emp: list) {            
   System.out.println(emp);
}

Pagination using Criteria Interface:There are two methods of the Criteria interface for pagination.

使用 Criteria 接口进行分页: Criteria 接口有两种用于分页的方法。

1. Criteria setFirstResult(int firstResult):

1. 条件 setFirstResult(int firstResult):

Set the first result to be retrieved.

设置要检索的第一个结果。

2. List item Criteria setMaxResults(int maxResults):

2. 列表项条件 setMaxResults(int maxResults):

Set a limit upon the number of objects to be retrieved.

对要检索的对象数量设置限制。

Example:

例子:

Criteria criteria = session.createCriteria(Employee.class);
criteria.setFirstResult(5);
criteria.setMaxResults(10);            
List<Employee> list = criteria.list();
for(Employee emp: list) {            
    System.out.println(emp);
}