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

醉酒当歌 提交于 2019-11-27 02:53:32

问题


How do I implement paging in Hibernate? The Query objects has methods called setMaxResults and setFirstResult which 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?


回答1:


You can use Query.setMaxResults(int results) and 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.




回答2:


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

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

for Criteria queries I usually push my data into a DTO like this

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;



回答3:


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.




回答4:


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.

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.




回答5:


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.



来源:https://stackoverflow.com/questions/1600215/implementing-result-paging-in-hibernate-getting-total-number-of-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!