java 在hibernate中实现结果分页(获取总行数)

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

Implementing result paging in hibernate (getting total number of rows)

javadatabasehibernatepaging

提问by flybywire

How do I implement paging in Hibernate? The Queryobjects has methods called setMaxResultsand setFirstResultwhich are certainly helpful. But where can I get the total number of results, so that I can show link to last page of results, and print things such as results 200 to 250 of xxx?

如何在 Hibernate 中实现分页?这些Query对象有被调用的方法setMaxResultssetFirstResult这些方法肯定是有帮助的。但是我在哪里可以获得结果总数,以便我可以显示结果最后一页的链接,并打印诸如xxx 的结果 200 到 250 之类的东西?

回答by sinuhepop

You can use Query.setMaxResults(int results) and Query.setFirstResult(int offset).

您可以使用 Query.setMaxResults(int results) 和 Query.setFirstResult(int offset)。

Editing too: There's no way to know how many results you'll get. So, first you must query with "select count(*)...". A little ugly, IMHO.

编辑太:没有办法知道你会得到多少结果。因此,首先您必须使用“select count(*)...”进行查询。有点丑,恕我直言。

回答by non sequitor

You must do a separate query to get the max results...and in the case where between time A of the first time the client issues a paging request to time B when another request is issued, if new records are added or some records now fit the criteria then you have to query the max again to reflect such. I usually do this in HQL like this

您必须执行单独的查询才能获得最大结果......并且在客户端第一次发出分页请求的时间 A 到发出另一个请求时的时间 B 之间的情况下,如果添加了新记录或现在有一些记录符合条件,然后您必须再次查询最大值以反映这一点。我通常像这样在 HQL 中这样做

Integer count = (Integer) session.createQuery("select count(*) from ....").uniqueResult();

for Criteriaqueries I usually push my data into a DTO like this

对于Criteria查询,我通常将我的数据推送到这样的 DTO

ScrollableResults scrollable = criteria.scroll(ScrollMode.SCROLL_INSENSITIVE);
if(scrollable.last()){//returns true if there is a resultset
    genericDTO.setTotalCount(scrollable.getRowNumber() + 1);
    criteria.setFirstResult(command.getStart())
            .setMaxResults(command.getLimit());
    genericDTO.setLineItems(Collections.unmodifiableList(criteria.list()));
}
scrollable.close();
return genericDTO;

回答by Chii

you could perform two queries - a count(*) type query, which should be cheap if you are not joining too many tables together, and a second query that has the limits set. Then you know how many items exists but only grab the ones being viewed.

您可以执行两个查询 - 一个 count(*) 类型的查询,如果您没有将太多表连接在一起,这应该很便宜,以及第二个设置了限制的查询。然后你知道有多少项目存在,但只抓取正在查看的项目。

回答by Bill

You can just setMaxResults to the maximum number of rows you want returned. There is no harm in setting this value greater than the number of actual rows available. The problem the other solutions is they assume the ordering of records remains the same each repeat of the query, and there are no changes going on between commands.

您可以将MaxResults 设置为您想要返回的最大行数。将此值设置为大于实际可用行数没有任何害处。其他解决方案的问题是他们假设记录的顺序在每次重复查询时保持不变,并且命令之间没有变化。

To avoid that if you really want to scroll through results, it is best to use the ScrollableResults. Don't throw this object away between paging, but use it to keep the records in the same order. To find out the number of records from the ScrollableResults, you can simply move to the last() position, and then get the row number. Remember to add 1 to this value, since row numbers start counting at 0.

为了避免这种情况,如果您真的想滚动结果,最好使用 ScrollableResults。不要在分页之间扔掉这个对象,而是用它来保持记录的顺序相同。要从 ScrollableResults 中找出记录数,您可以简单地移动到 last() 位置,然后获取行号。请记住将此值加 1,因为行号从 0 开始计数。

回答by NickDK

I personally think you should handle the paging in the front-end. I know this isn't that effici?nt but at least it would be less error prone.

我个人认为你应该在前端处理分页。我知道这不是那么有效,但至少它不会那么容易出错。

If you would use the count(*) thing what would happen if records get deleted from the table in between requests for a certain page? Lots of things could go wrong this way.

如果您使用 count(*) 东西,如果在对某个页面的请求之间从表中删除记录会发生什么?很多事情都可能以这种方式出错。