Skip to main content

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

Hello,

I solved it like this, but it's somewhat hacky. It works, but is there a better way?

        Set<Root<?>> rootSet = selectQuery.getRoots();
        if (rootSet.isEmpty()) {
            throw new RuntimeException("Expecting a query root");
        }
        if (rootSet.size() > 1) {
            throw new RuntimeException("Cartesian product");
        }
        List<Root<T>> roots = new ArrayList(rootSet);
        Root<T> firstRoot = roots.get(0);
        Expression<Long> countExpression = builder.count(firstRoot);

Best regards,

Yannick Majoros

Le 2/05/2012 15:56, James Sutherland a écrit :
You cannot use the restriction from one query in another.  You restriction
will be based on a different entityRoot , so you end up with two entityRoots
as expected.

You have the build the where using the correct entityRoot from the query
that you are building.


ymajoros-2 wrote:
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
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users



-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland
http://www.eclipse.org/eclipselink/
  EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink ,
http://wiki.oracle.com/page/TopLink TopLink
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink ,
http://www.eclipse.org/forums/index.php?t=thread&frm_id=111&S=1b00bfd151289b297688823a00683aca
EclipseLink
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence
Blog:  http://java-persistence-performance.blogspot.com/ Java Persistence
Performance



Back to the top