Using native Query with distinct is returning wrong total count
See original GitHub issueHello, It seems that Hibernate is removing the “distinct” key word from the native Query when using the count to get the total elements.
Any Workaround for this?
Springboot 2.3.7
A fragment for the logs:
The log for the get entities
select distinct e.* from schema.entity e
(more statements...)
...
The log for the count query generated by hibernate (as you can see, for some reason, it is removing the “distinct” keyword)
select count(e) from schema.entity e
(more statements...)
...
This seems like bug for me. I saw this issue from a long ago time but without any solution (a part from generating my own count query) https://github.com/spring-projects/spring-data-jpa/issues/750
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Is it possible to use COUNT with a DISTINCT JPA projection?
This works fine with setFirstResult and setMaxResults to page data. However I need to count the total number of rows without fetching all...
Read more >How To Apply DISTINCT to Your JPQL But Not Your SQL Query
You need the DISTINCT keyword in your JPQL query if you use a JOIN FETCH clause. With this hint, you can prevent Hibernate...
Read more >COUNT (DISTINCT) and Other DISTINCT Aggregates - Vertica
This query selects each distinct product_key value and then counts the number of distinct date_key values for all records with the specific product_key...
Read more >SQL query for distinct count - Microsoft Q&A
My intention in using GROUP BY is to return the distinct APPKEY records based on the combination of ANUMBER, DEPTKEY in a UNIT...
Read more >Overview of the SQL Count Distinct Function - SQLShack
We use SQL Count aggregate function to get the number of rows in the output. Suppose we have a product table that holds...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I hope with this PR (#2514 ) it should not be a problem anymore. @ruslanys
Ok I will take a look at this. I think I have a fix for this. PR will come soon. @ruslanys