简体   繁体   English

createNativeQuery中的分页

[英]Pagination in createNativeQuery

I want to implement Pageable in my custom query.Below is the code I am using.The sql is dummy query.Could someone please tell me how can I calculate total rows,lastelement from following? 我想在我的自定义查询中实现Pageable.Below是我正在使用的代码.sql是虚拟查询。有人请告诉我如何计算总行数,来自下面的元素?

EntityManager em = OpenJPAUtils.openEntityManager();
String sql="select DISTINCT student.studentId from student where ... LIMIT  :limit OFFSET  :offSet ; "  
query = em.createNativeQuery(sql);
query.setParameter("limit", pageable.getPageSize());
query.setParameter("offSet", pageable.getPageNumber());
query.getResultList()

I want total counts but the limit I have used will only give that much only. 我想要总数,但我使用的限制只会给予这么多。 I could use the same query without limit to count the total rows but the query is very big and I don't want to run the same query twice to get total counts and calculate the rows. 我可以无限制地使用相同的查询来计算总行数,但查询非常大,我不想运行相同的查询两次以获得总计数并计算行数。 I would really appreciate the help. 我真的很感激帮助。

Why not use pagination provided by JPA? 为什么不使用JPA提供的分页?

query.setFirstResult(firstIndex);
query.setMaxResults(firstIndex + pageSize);

Then no need for LIMIT clause in sql: 然后在sql中不需要LIMIT子句:

String sql="select DISTINCT student.studentId from student where ..."  

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

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