'Find Distinct results in spring data jpa
Im trying to fetch Distinct records, SQL query looks as follows
select DISTINCT id from Table A where UserId = 'XXXX';
Currently, I have implemented this using spring data jpa @Query
@Query(value = "select DISTINCT id from Table A where UserId = :userId")
Can this be achived through spring data jpa query method ?
Solution 1:[1]
Yes, it can. You can use the Spring boot query builder (see here) to get a simple query with distinct. In your case, the query method will look like:
List<Long> findDistinctIdByUserId(String userId);
The use of Long is just in case you're using a number ID in the DB - it can of course be String or any other datatype, as applicable. Same for my choice to type userId as String. "findDistinct" is a prefix to select all distinct results from the table, the next word is the column name, and "byColumnName" is a suffix to add the relevant Where clause (and it should correlate with the input argument to the method).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Assafs |
