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

Fix total count for paged specifications with GROUP BY #2376

Closed
wants to merge 1 commit into from
Closed

Fix total count for paged specifications with GROUP BY #2376

wants to merge 1 commit into from

Conversation

robin850
Copy link

@robin850 robin850 commented Dec 4, 2021

Hello,

This patch tries to fix using paginated results found through a custom Specification that relies on a GROUP BY clause.

I hope the test is clear enough, but just in case, the problem is that since the getCountQuery method of SimpleJpaRepository takes the exact same query produced by the specification but injects a COUNT in the SELECT, the COUNT is done on each group of records so we end up counting them and not the "root" ones.

Using DISTINCT does the trick here. Actually I can't think of a case where using DISTINCT would be problematic so in my mind the patch could be to totally remove the if/else statement and rely on countDistinct anyway but maybe I'm blinded with my application and that's a bit too rash.

For the record, the if/else statement has been added in a74fadd.

Closes #1296.
Closes #2361.
Closes #1858.

Have a nice day.


  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

To count the total of elements returned by a paged query expressed via
a `Specification`, the produced query is taken and its `SELECT` clause
is changed to perform a `COUNT`.

The problem is that using the `GROUP BY` clause changes the way the
`COUNT` behaves and returns the number of times the root entity is
associated with its joined entities.

Closes #1296
Closes #2361
Closes #1858
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 4, 2021
@schauder
Copy link
Contributor

schauder commented Dec 6, 2021

Sorry for being so late to reply to the original issue.
As explained there we will not consider this PR.

@schauder schauder closed this Dec 6, 2021
@robin850 robin850 deleted the page-spec-with-group-by branch December 7, 2021 08:46
@ankitpec72
Copy link

@robin850 @schauder We are facing the same issue in Spring-data-jpa-2.7.13 version. Is there any solution or this issue is already fixed in some other version? The count query with group by is returning wrong total count value.

@robin850
Copy link
Author

@ankitpec72 : No, as stated in @schauder's comment, the fact that it doesn't work is "normal" because you should only manage WHERE clauses through Specification.

To be honest, we currently have a copy of this PR's SimpleJpaRepository.java file in our application to be able to correctly use GROUP BY. It's far from ideal but I don't know if there is an "elegant" API currently to achieve the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
4 participants