Spring Data JPA - Get All Unique Values in Column

前端 未结 2 2070
耶瑟儿~
耶瑟儿~ 2021-01-05 00:03

I have a project using Spring Data JPA that consumes data from a table full of addresses. One of the columns of this table is the city. I would like to get a distinct list o

2条回答
  •  我在风中等你
    2021-01-05 00:16

    This can be achieved using the @Query annotation as:

    public interface AddressRepository extends CrudRepository {
        @Query("SELECT DISTINCT a.city FROM Address a")
        List findDistinctCity();
    }
    

    Then, a call to addressRepository.findDistinctCity() would return the distinct city names.

    A sample application is available on Github for review. Run integration test as mvn clean test to verify the approach.

提交回复
热议问题