Skip to content

Wrong calculation of total number of elements when using DISTINCT clause [DATAJPA-342] #750

New issue

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

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

Already on GitHub? # to your account

Closed
spring-projects-issues opened this issue May 6, 2013 · 2 comments
Assignees
Labels
in: core Issues in core support type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link

Alex opened DATAJPA-342 and commented

If I define a custom query of this type:

@Query("SELECT DISTINCT m.genre FROM Media m WHERE m.user = ?1 ORDER BY m.genre ASC")
Page<String> getJukeGenres(User user, Pageable pageable);

The resulting Page element has wrong Total Elements.
It seems like it gives the number of total records instead of the distinct ones.

I had to create another adhoc query just to have the correct count:

@Query("SELECT count(*) as num FROM Media m WHERE m.user = ?1 AND m.genre IS NOT NULL")
long countGenres(User user);

But this is tedious workaround that I would like to avoid


Affects: 1.3 GA

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

We probably don't discover the distinct correctly here. As a workaround, you can define the countQuery attribute of the @Query annotation of the paging method to define the query explicitly

@OctavioCega
Copy link

Any progress or solution on this?

It seems that when using a native query with "distinct" it is getting removed in the count.

Is this a bug or a feature?

using springboot version 2.3.7.BUILD-SNAPSHOT

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
in: core Issues in core support type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants