How to find distinct rows with field in list using JPA and Spring?

前端 未结 5 1701
陌清茗
陌清茗 2020-12-01 17:45

I am using Spring to connect to the db. I have an interface extending CrudRepository Here is the query I want execute on the db: SELEC

相关标签:
5条回答
  • 2020-12-01 18:12

    I finally was able to figure out a simple solution without the @Query annotation.

    List<People> findDistinctByNameNotIn(List<String> names);
    

    Of course, I got the people object instead of only Strings. I can then do the change in java.

    0 讨论(0)
  • 2020-12-01 18:17

    Can you not use like this?

    @Query("SELECT DISTINCT name FROM people p (nolock) WHERE p.name NOT IN (:myparam)")
    List<String> findNonReferencedNames(@Param("myparam")List<String> names);
    

    P.S. I write queries in SQL Server 2012 a lot and using nolock in server is a good practice, you can ignore nolock if a local db is used.

    Seems like your db name is not being mapped correctly (after you've updated your question)

    0 讨论(0)
  • 2020-12-01 18:22
    @Query("SELECT distinct new com.model.referential.Asset(firefCode,firefDescription) FROM AssetClass ")
    List<AssetClass> findDistinctAsset();
    
    0 讨论(0)
  • 2020-12-01 18:27
    @Query("SELECT DISTINCT name FROM people WHERE name NOT IN (:names)")
    List<String> findNonReferencedNames(@Param("names") List<String> names);
    
    0 讨论(0)
  • 2020-12-01 18:28

    Have you tried rewording your query like this?

    @Query("SELECT DISTINCT p.name FROM People p WHERE p.name NOT IN ?1")
    List<String> findNonReferencedNames(List<String> names);
    

    Note, I'm assuming your entity class is named People, and not people.

    0 讨论(0)
提交回复
热议问题