Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] (bug?) counting results gives a cartesian product in 2.3.3

Hello,

After upgrading to 2.3.3, I get a cartesian product. It could have been the same in 2.3.2, but I didn't notice it then.

Here is a method which counts the total results of query (for pagination):

public <T> Long countCriteriaQueryResults(CriteriaQuery<T> selectQuery) {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();

        CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
        Class<T> resultType = selectQuery.getResultType();
        Root<T> entityRoot = countQuery.from(resultType);
Expression<Long> countExpression = builder.countDistinct(entityRoot);
        countQuery.select(countExpression);
        countQuery.where(selectQuery.getRestriction());

        return entityManager.createQuery(countQuery).getSingleResult();
    }

It results in this, for example:

SELECT COUNT(DISTINCT(t0.ID)) FROM client t0, client t1 WHERE ((t1.FIDUCIAIRE_ID = ?) = ?)

I'd rather want this:

SELECT COUNT(DISTINCT(t1.ID)) FROM client t1 WHERE ((t1.FIDUCIAIRE_ID = ?) = ?)

Is it a bug in Eclipselink or is my code wrong?

Is it ok to use the previous predicates with query.getRestriction()?

Best regards,

Yannick Majoros


Back to the top