spring-projects / spring-projects/spring-data-jpa

Paged queries with GROUP BY clauses return the incorrect total count when there is only 1 result [DATAJPA-945]

Open
#1,296 3 comments 3 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 30, 2020.

in: query-parser type: bug
Dominant language
Java
Stars
3.3k
Forks
1.6k
PR merge metrics
No merged PRs in 30d

Description

David Linfield opened DATAJPA-945 and commented

For example, if we have a data model in which one company has many products, and we wish to get pages of results listing the company name and the count of the number of products each company has as follows:

@Query(value = "select new uk.co.example.models.CompanyWithProductCount(c.id, c.name, cast(count(p) as integer)) " +
    "from Company c join c.products as p " +
    "group by c.id ")
Page<CompanyWithProductCount> getCompaniesWithProductCount(Pageable pageable);

The query performed to count the total number of results simply performs a count query with a GROUP BY clause, hence effectively returning a list of COUNT(p) for each Company. Where there are multiple companies there seems to be some special case handling where the total returned the the PageImpl object is actually the number of rows returned by this query (the correct answer), however when there is only one company returned, the total returned to the PageImpl object is actually the value returned by the COUNT query (i.e. the number of products that company has).

This bug is often concealed, because PageImpl uses the number of items in the Content of the Page if the supplied total is less than the page size provided. So to recreate in the case above you there must be more than pageSize Products associated with the Company


Affects: 1.10.1 (Hopper SR1)

0 votes, 6 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.