spring-projects / spring-projects/spring-data-jpa
totalElements value is wrong when using group by with pageable of pageSize 1 [DATAJPA-1544]
@schauder is already working on this.
Since Dec 30, 2020.
- Dominant language
- Java
- Stars
- 3.3k
- Forks
- 1.6k
- PR merge metrics
- No merged PRs in 30d
Description
RohitJ23 opened DATAJPA-1544 and commented
Similar to https://jira.spring.io/browse/DATAJPA-1415
When group by is used with pageable of page size 1 and there is exactly 1 result row, totalElements (and totalPages) returned by pageable has the count before group by is applied.
This is because JpaQueryExecution assumes if totals.size() == 1 (which is true in this case) then group by is not applied and therefore totals.get(0) is the actual count:
private long count(AbstractJpaQuery repositoryQuery, Object[] values) {
List<?> totals = repositoryQuery.createCountQuery(values).getResultList();
return (totals.size() == 1 ? CONVERSION_SERVICE.convert(totals.get(0), Long.class) : totals.size());
}
And PageExecutionUtils.getPage does a greater than check instead of greater than equal to, resulting in totalElements being what totalSupplier gives instead of content size.
if (pageable.isUnpaged() || pageable.getPageSize() > content.size()) {
return new PageImpl<>(content, pageable, content.size());
}
return new PageImpl<>(content, pageable, totalSupplier.getAsLong());
Reproduced in https://github.com/RohitJ23/GroupBy-Repro - run GroupByBugTest.groupByBugTest
Affects: 2.1.8 (Lovelace SR8)
Referenced from: pull request https://github.com/spring-projects/spring-data-jpa/pull/388
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.