apache / apache/grails-core

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

Open
#14,339 0 comments 0 reactions 0 assignees View on GitHub
relates-to: grails-data-hibernate5
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

_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`.

```java
/**
* 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`:
```java
public PagedResultList(GrailsHibernateTemplate template, Criteria crit) {
super(null);
...
}

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

```

Affected method:
```java
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();
}
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start with grails.orm.HibernateCriteriaBuilder#createPagedResultList and the grails.orm.PagedResultList constructors, then inspect org.grails.orm.hibernate.query.PagedResultList#initialize. Reproduce BuildableCriteria#list with a page beyond the last; done means the returned result is compatible with the newer PagedResultList type and still reports the total count for an empty page.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.