简体   繁体   English

如何在JPA中对结果查询进行分页?

[英]How do I paginate my result query in JPA?

I am trying to implement a pagination functionality with JPA but I am having trouble. 我正在尝试通过JPA实现分页功能,但是遇到了麻烦。

I can get and set the first and last numbers on the link eg localhost:8000/?first=11&last=20. 我可以获取并设置链接上的第一个和最后一个数字,例如localhost:8000 /?first = 11&last = 20。

As you can see, I am trying to get the items starting at row 11 and ending at row 20, however, I am getting all the results starting from first but with a total amount of last eg starting at 10 but with 20 results as opposed to the desired 10. 如您所见,我正在尝试使项目从第11行开始到第20行结束,但是,我得到的所有结果都从第一个开始,但是最后一个的总数,例如从10开始,但是有20个结果到所需的10。

I am using: 我在用:

query.setFirstResult(firstRowNumber - 1).setMaxResults(lastRowNumber);

How do I therefore limit my results? 因此,我如何限制结果?

You want this: 你要这个:

query
  .setFirstResult(firstRowNumber - 1)
  .setMaxResults(lastRowNumber - firstRowNumber + 1);

Notice that the method is named setMaxResults() (total number), not setLastResult() . 请注意,该方法名为setMaxResults() (总数),而不是setLastResult() Have a look at awesome which handles paging very nicely. 看一下很棒的 ,它可以很好地处理分页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM