Grails

Database searching with HibernateCriteriaBuilder has pagination and distinct results coded to be mutually exclusive

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Blocker Blocker
  • Resolution: Fixed
  • Affects Version/s: 1.0.3
  • Fix Version/s: 1.4-M1
  • Component/s: Persistence
  • Labels:
    None
  • Environment:
    OS X 10.5.3, J2SE 1.5.0_13-b05-237, Oracle 10.g Release 2

Description

The code snippet below means that Criteria queries through the HibernateCriteriaBuilder unable to support requests for distinct records to be returned in the same list() query as requests for pagination. This is at least as much an issue with Criteria as it is with the builder, however this seems like an area where the builder could (should?) be easier to use than directly using Criteria.

Code snippet from HibernateCriteriaBuilder:

HibernateCriteriaBuilder.java
} else if(paginationEnabledList) {
    // Calculate how many results there are in total. This has been
    // moved to before the 'list()' invocation to avoid any "ORDER
    // BY" clause added by 'populateArgumentsForCriteria()', otherwise
    // an exception is thrown for non-string sort fields (GRAILS-2690).
    this.criteria.setFirstResult(0);
    this.criteria.setMaxResults(Integer.MAX_VALUE);
    this.criteria.setProjection(Projections.rowCount());
    int totalCount = ((Integer)this.criteria.uniqueResult()).intValue();

    // Drop the projection, add settings for the pagination parameters,
    // and then execute the query.
    this.criteria.setProjection(null);
    this.criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    GrailsHibernateUtil.populateArgumentsForCriteria(this.criteria, (Map)args[0]);
    PagedResultList pagedRes = new PagedResultList(this.criteria.list());

    // Updated the paged results with the total number of records
    // calculated previously.
    pagedRes.setTotalCount(totalCount);
    result = pagedRes;
  }

The following link has a suggested solution:

http://floledermann.blogspot.com/2007/10/solving-hibernate-criterias-distinct.html

The following mailing list posting has examples of the problem,

http://www.nabble.com/Criteria-Query-How-To-to17352317.html#a17516093

Issue Links

Activity

Hide
Jeff Brown added a comment -

added code tags to code sample to make it easier to look at

Show
Jeff Brown added a comment - added code tags to code sample to make it easier to look at
Hide
Graeme Rocher added a comment -

There is no scope / time to resolve these remaining lower priority issues for 1.2 so moving to 1.3

for 1.2 final only issues considered blocking will now be fixed

Show
Graeme Rocher added a comment - There is no scope / time to resolve these remaining lower priority issues for 1.2 so moving to 1.3 for 1.2 final only issues considered blocking will now be fixed
Hide
Luke Daley added a comment -

Be aware that distinctness and pagination do not play well together.

http://floledermann.blogspot.com/2007/10/solving-hibernate-criterias-distinct.html

Show
Luke Daley added a comment - Be aware that distinctness and pagination do not play well together. http://floledermann.blogspot.com/2007/10/solving-hibernate-criterias-distinct.html
Hide
Graeme Rocher added a comment -

I'm closing this because the error report described the problem being related to the code that handles paginationEnabledList however the examples given in linked thread never activate this code. (The link to the thread is currently broken but it can be found in the archive here http://archive.grails.codehaus.org/lists/org.codehaus.grails.user/msg/17516093.post@talk.nabble.com).

I believe what you're really seeing is the behavior which is fixed by GRAILS-7087

I have added an additional test that verifies the example linked in this issue at:

https://github.com/grails/grails-core/commit/508fea170aba3e1d677965d836c76ac766407b77

I believe the true problem is that we were using LEFT instead of INNER join by default. If this is still an issue for people and someone believes my assertions are incorrect then reopen the issue and attach an example that reproduces the problem.

Show
Graeme Rocher added a comment - I'm closing this because the error report described the problem being related to the code that handles paginationEnabledList however the examples given in linked thread never activate this code. (The link to the thread is currently broken but it can be found in the archive here http://archive.grails.codehaus.org/lists/org.codehaus.grails.user/msg/17516093.post@talk.nabble.com). I believe what you're really seeing is the behavior which is fixed by GRAILS-7087 I have added an additional test that verifies the example linked in this issue at: https://github.com/grails/grails-core/commit/508fea170aba3e1d677965d836c76ac766407b77 I believe the true problem is that we were using LEFT instead of INNER join by default. If this is still an issue for people and someone believes my assertions are incorrect then reopen the issue and attach an example that reproduces the problem.
Hide
James Abley added a comment -

I don't appear to have permissions to re-open this issue. I think this is either the same thing, or it's related:

http://stackoverflow.com/questions/6760151/grails-gorm-how-to-get-a-pagedresultlist-when-using-aggregation-functions

I'm not using full-blow Grails, but would be happy to try to provide a test case if someone could point me at a skeleton that I can use / fork.

Show
James Abley added a comment - I don't appear to have permissions to re-open this issue. I think this is either the same thing, or it's related: http://stackoverflow.com/questions/6760151/grails-gorm-how-to-get-a-pagedresultlist-when-using-aggregation-functions I'm not using full-blow Grails, but would be happy to try to provide a test case if someone could point me at a skeleton that I can use / fork.
Hide
Graeme Rocher added a comment -

create a separate issue at is a different (but related) problem

Show
Graeme Rocher added a comment - create a separate issue at is a different (but related) problem

People

Vote (24)
Watch (21)

Dates

  • Created:
    Updated:
    Resolved: