Spring Data JPA - Get All Unique Values in Column

前端 未结 2 2051
耶瑟儿~
耶瑟儿~ 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:38

    Manish's comment should probably be bumped up to an answer (which i'll try to capture here since it ultimately solved my problem...although projections didn't seem to work with select distinct). The selected answer works in spring-data-jpa, but fails in spring-data-rest. One possible workaround for the spring-data-rest scenario is to create a separate @RestController for the select distinct results

    @RestController
    public class AddressRepoAdditionals {
         @Autowired
         private AddressRepository repo;
    
         @RequestMapping("/additional/address/distictCities")
         public List findDistinctCity() {
              return repo.findDistinctCity();
         }
     }
    

    perhaps there's a similar but more elegant variation based on @RepositoryRestController

提交回复
热议问题