How do I do a distinct counting with Spring Data JPA? I do not want to use the @Query
annotation. The equivalent sql query would be:
SELECT COUNT(DISTINCT rack) FROM store WHERE id = 10001;
I tried by writing the following query, but it isn't working:
int countDistinctRackById();
This should work :
Integer countDistinctRackById(Long id);
I just tried this out and none of the existing answers work.
The following query:
Integer countDistinctRackById(String id);
Will render something like this:
select distinct count(table0_.rack) as col_0_0_ from table table0_ where table0_.id=?
As I am currently stuck with Spring Boot 1.2.3, idk if there were any changes later, but to achieve a count distinct I had to define a custom query:
@Query("Select count(distinct rack) from Table t where t.id = ?1")
Try this according to Spring Documentation:
Integer countDistinctRackById(String id);
Replace the word Rack
with the actual name of rack field
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With