Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BuildableCriteria#list can not cast results to org.grails.orm.hibernate.query.PagedResultList #876

Open
HaroldHormaechea opened this issue Jun 5, 2024 · 0 comments

Comments

@HaroldHormaechea
Copy link

HaroldHormaechea commented Jun 5, 2024

Tested both in 7.2.1& 8.1.0

grails.orm.PagedResultList is a deprecated class which points to using org.grails.orm.hibernate.query.PagedResultList instead.
When calling BuildableCriteria#list, the method grails.orm.HibernateCriteriaBuilder#createPagedResultList builds a grails.orm.PagedResultList which can not be casted to the new org.grails.orm.hibernate.query.PagedResultList.

/**
 * A result list for Criteria list calls, which is aware of the totalCount for
 * the paged result.
 *
 * @author Siegfried Puchbauer
 * @since 1.0
 * @deprecated Use {@link org.grails.orm.hibernate.query.PagedResultList} instead.
 */
@SuppressWarnings({"unchecked","rawtypes"})
@Deprecated
public class PagedResultList extends grails.gorm.PagedResultList 

Casting to the parent of both classes grails.gorm.PagedResultList causes another bug where if a caller asks for a page beyond the last, which has 0 elements in it, the method grails.gorm.PagedResultList#initialize finds a null query and defaults to 0 total results, because all the constructors of grails.orm.PagedResultList push a null query to the super constructor:

Constructors of grails.orm.PagedResultList:

  public PagedResultList(GrailsHibernateTemplate template, Criteria crit) {
        super(null);
        ...
    }

    public PagedResultList(GrailsHibernateTemplate template, HibernateQuery query) {
        super(null);
        ...
    }

Affected method:

  protected void initialize() {
        if (totalCount == Integer.MIN_VALUE) {
            if (query == null) {
                totalCount = 0;
            } else {
                Query newQuery = (Query)query.clone();
                newQuery.projections().count();
                Number result = (Number) newQuery.singleResult();
                totalCount = result == null ? 0 : result.intValue();
            }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant