We create a jpa 2.0 criteria in order to count the number of register that matches a where claupsule:
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
CriteriaQuery<Long> countCriteria = cb.createQuery(Long.class);
countCriteria.select(cb.count(countCriteria.from(this.clazz)));
if(criteria.getRestriction()!=null) {
countCriteria.where(criteria.getRestriction());
}
int total = this.entityManager.createQuery(countCriteria).getSingleResult().intValue();
In this case, thre is only one restriction (instalacion_Ins_Identificador = 1), but the query generated is:
SELECT COUNT(t0.instalacion_Ins_Identificador) FROM USUARIOS t0, USUARIOS t1 WHERE ((t1.instalacion_Ins_Identificador = 1) = 1)")
and this doen't work.