最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Pagination with Hibernate Criteria - Stack Overflow

programmeradmin5浏览0评论

I am facing problem in pagination. I am using criteria. Display length is 10. While clicking pagination it shows different results like list is 9 and 8 like that. How to avoid this kind of pagination problems. I need list 10 every time. How to control this kind of inconsistancy.

I am facing problem in pagination. I am using criteria. Display length is 10. While clicking pagination it shows different results like list is 9 and 8 like that. How to avoid this kind of pagination problems. I need list 10 every time. How to control this kind of inconsistancy.

Share Improve this question edited May 21, 2013 at 16:07 Muhammad Bekette 1,4342 gold badges28 silver badges63 bronze badges asked May 21, 2013 at 14:47 SangeethaSangeetha 371 silver badge7 bronze badges 2
  • Please post the code that is causing the problem. – Bhashit Parikh Commented May 21, 2013 at 14:51
  • why not you use sessions to store?? – Rajendra_Prasad Commented May 21, 2013 at 14:52
Add a ment  | 

2 Answers 2

Reset to default 5

User setFirstResult() and setMaxResults() for this. Here is a short code example:

EntityManager em = .....
....................
TypedQuery<T> query = em.createQuery(criteria);
query.setFirstResult(pageNumber * pageSize);
query.setMaxResults(pageSize);

If you're using firstResult and maxResults then that implies that the result set is potentially very large and you want to re-execute the query for each page. If that is the case, then the data may change between queries, resulting in inconsistent results. The only way to try to control this would be to sort on some unique value or group of values (primary key or unique index) and add a restriction to the criteria query that requires results to have values or groups of values greater than the last result in the previous page. Even then, inserts, updates, and deletes between pages will still causes inconsistencies in the results.

If the maximum number of results is something reasonable (say 500-1000 results) then it may be easier to simply pull all of the matching results, cache them in memory, and handle paging in the UI layer to ensure consistency. A mon strategy is to limit the query to some reasonable number of results (e.g. 1000) and set the maxResults to that plus 1 (e.g. 1001). If the number of results returned is greater than your arbitrary limit (i.e. 1001 vice 1000) then display an indication to the user that more than 1000 results were found and the query should be made more specific (or invite them to fetch the next 1000 results).

发布评论

评论列表(0)

  1. 暂无评论