Are you looking for something like this?
/**
* Counts the number of results of a search.
*
* @param criteria The criteria for the query.
* @return The number of results of the query.
*/
public <T> Long findCountByCriteria(CriteriaQuery<?> criteria) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Long> countCriteria = builder.createQuery(Long.class);
Root<?> entityRoot = countCriteria.from(criteria.getResultType());
countCriteria.select(builder.count(entityRoot));
countCriteria.where(criteria.getRestriction());
return em.createQuery(countCriteria).getSingleResult();
}
That you could use like this:
// a search based on the Criteria API
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
Root<Person> personRoot = criteria.from(Person.class);
criteria.select(personRoot);
Predicate personRestriction = builder.and(
builder.equal(personRoot.get(Person_.gender), Gender.MALE),
builder.equal(personRoot.get(Person_.relationshipStatus), RelationshipStatus.SINGLE)
);
criteria.where(personRestriction);
//...
// and to get the result count of the above query
Long count = findCountByCriteria(criteria);
PS: I don't know if this is the right/best way to implement this, still learning the Criteria API...