Is there a way to get the count size for a JPA Named Query with a result set?

前端 未结 3 1267
余生分开走
余生分开走 2020-12-30 02:46

I like the idea of Named Queries in JPA for static queries I\'m going to do, but I often want to get the count result for the query as well as a result list from some subset

3条回答
  •  鱼传尺愫
    2020-12-30 03:32

    Using setFirstResult/setMaxResults do not return a subset of a result set, the query hasn't even been run when you call these methods, they affect the generated SELECT query that will be executed when calling getResultList. If you want to get the total records count, you'll have to SELECT COUNT your entities in a separate query (typically before to paginate).

    For a complete example, check out Pagination of Data Sets in a Sample Application using JSF, Catalog Facade Stateless Session, and Java Persistence APIs.

提交回复
热议问题