cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with select count in EclipseLink + JPA + Hana DB

Former Member
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

former_member197208
Participant
0 Kudos

Could it be that you are mixing critiera objects from two calls to the criteria builder? Where is the "criteria" coming from?

Former Member
0 Kudos

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.

sabine_heider
Explorer
0 Kudos

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

Answers (0)