本文介绍了从@ElementCollection搜索对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Data JPA.

I am using Spring Data JPA.

我是这样的实体

public class A {

    @CollectionTable(name = "B_ITEMS", joinColumns = @JoinColumn(name = "B_ID"))
    @ElementCollection
    private List<B> bs;

}

还有一个嵌入式类

@Embeddable
public class B { 

private String prop1


private String prop2


private String prop3

}

如何使用@ElementCollection B内容搜索实体A?

How do I search entity A using the contents @ElementCollection B?

这是我的存储库Spring Data JPA存储库

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?

推荐答案

@ElementCollection只是映射@OneToMany关系的简单方法.

@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)
}

这篇关于从@ElementCollection搜索对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 08:40