I'm new in JPA and want to implement a generic JPA DAO and need to find number of rows f a query to implement pagination. after many search over internet I can't find a practical way to do that. here is the code suggested in many articles :
Code:
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();
}
but that code doesn't work when using join. Is there any way to count query in JPA Criteria API ?