I would like to get all distinct values in a particular column. Normally this would be done using the @Query
annotation by writing a select with the particular column name.
@Query(value = "SELECT DISTINCT t.columnName FROM #{#entityName} t")
Set<Object> findDistinctValues();
However the column name is dynamic. I need to search for distinct values using the column name argument. Also the repository is generic.
@Query(value = "SELECT DISTINCT t.:columnName FROM #{#entityName} t")
Set<Object> findDistinctValues(@Param("columnName") String columnName);
This doesn't work as the syntax is correct.
I've tried using CriteriaBuilder
instead but it always returns the enter row instead of one column. This would mean I would need to use reflection to get the data I need out of the row as I only have the column name.
Is it possible to use Spring JPA to do this? If not, what is the best alternative method?