on 05-27-2013 4:42 PM
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.
Could it be that you are mixing critiera objects from two calls to the criteria builder? Where is the "criteria" coming from?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, Andreas.
We try to do only one criteria with the restrictions (where) and reuses these restrictions in another criteria. The first one is the paged query ant the another is the "count".
The criteria are differents, but the restriccions are the same.
With Hibernate it works, but in EclipseLink dosen't. Eclipselink evaluates the root of the restrictions and the query different ( USUARIOS t0, USUARIOS t1).
We want to implement a pagination mechanism, retrieving only a group of rows, but the total number of rows that matches with the restrictions.
Hi David,
Reusing the WHERE condition is exactly what causes the problem with your query. With EclipseLink, all query elements - in this case the WHERE condition (aka predicate condition) - are dependent on the particular CriteriaQuery and must not be reused or shared between different instances.
So instead of writing
countCriteria.where(criteria.getRestriction());
you should rather use the criteria builder to create a new predicate.
Reusing query elements may work in other JPA providers like Hibernate, but it's beyond the JPA standard and not supported by EclipseLink.
Best regards,
Sabine
User | Count |
---|---|
84 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.