Reputation: 16127
I am using Spring Data JPA.
I an entity like this
public class A {
@CollectionTable(name = "B_ITEMS", joinColumns = @JoinColumn(name = "B_ID"))
@ElementCollection
private List<B> bs;
}
And an Embedded class
@Embeddable
public class B {
private String prop1
private String prop2
private String prop3
}
How do I search entity A using the contents @ElementCollection B?
And here's my repository Spring Data JPA Repositry
public interface ARepo extends PagingAndSortingRepository<Clinic, Long> {
}
What Query method name is applicable for my use case?
Upvotes: 15
Views: 7298
Reputation: 3166
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);
}
Upvotes: 1
Reputation: 937
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:
Upvotes: 2
Reputation: 4532
@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)
}
Upvotes: 4
Reputation: 192
You seem to be asking for a basic IN query in Spring Data - findBybsIn(List<B> bs)
Upvotes: 4