Searching An object from @ElementCollection

前端 未结 4 2079
野性不改
野性不改 2021-02-19 06:16

I am using Spring Data JPA.

I an entity like this

public class A {

    @CollectionTable(name = \"B_ITEMS\", joinColumns = @JoinColumn(name = \"B_ID\"))
         


        
相关标签:
4条回答
  • 2021-02-19 06:37

    @ElementCollection is just a simple way to map a @OneToMany relation.

    As such you can join them as usual:

    public interface ARepo extends PagingAndSortingRepository<A, Long> {
        @Query("select a from A a join a.bs b where b.prop1 = :prop1 and ...")
        A findByProps(@Param("prop1") String prop1)
    }
    
    0 讨论(0)
  • 2021-02-19 06:48

    An approach could be to search a list of A objects for all the properties of B object. That means use filters for prop1, prop2 and prop3 together.

    public interface ARepo extends PagingAndSortingRepository<Clinic, Long> {
        List<A> findByBsProp1AndBsProp2AndBsProp3(String prop1, String prop2, String prop3); 
    }
    
    0 讨论(0)
  • 2021-02-19 06:55

    You seem to be asking for a basic IN query in Spring Data - findBybsIn(List<B> bs)

    0 讨论(0)
  • 2021-02-19 06:57

    Instead of using @Query like in @Zeromus answer you can also define it by method name (findByElementCollectionProperty_NestedProperty):

    public interface ARepo extends JPARepository<A,Long> {
    
        List<A> findByBs_Prop1(String prop1);
    }
    

    More information how SpringData resolves nested Properties can be found under:

    Query Property Expressions

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