I have an arbitrary JPA criteria query. I would like to know how many results that query would produce without transferring all the results from the database.
So basically I would like to create a query
select count(*) from (<the arbitrary criteria query>)
The problem is that either I don't know how to create a SubQuery
from the original CriteriaQuery
when implementing this like:
CriteriaQuery<Integer> countQuery = criteriaBuilder.createQuery(Integer.class);
criteriaBuilder.count(criteriaQuery);
Or I'm missing another way to implement it.
How can I do this using JPA?
Edit:
This is not a duplicate of How do I count the number of rows returned by subquery?, because I already have a CriteriaQuery instance at hand, that I would like to incorporate into a new query or modify. The other question is about crafting a query from scratch. The point this question is about is how to use the CriteriaQuery
instance I already have at hand.