question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using native Query with distinct is returning wrong total count

See original GitHub issue

Hello, 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
DiegoKrupitzacommented, Apr 28, 2022

I hope with this PR (#2514 ) it should not be a problem anymore. @ruslanys

1reaction
DiegoKrupitzacommented, Apr 28, 2022

I found out that this commit has bug: https://github.com/spring-projects/spring-data-jpa/commit/3e64d9ad9b7d45fcf1231dbaf207be49cc481e7a

Ok I will take a look at this. I think I have a fix for this. PR will come soon. @ruslanys

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found