JPA Hibernate 选择不同的 @ElementCollection 并进行比较

Coc*_*oco 2 java spring hibernate jpa

我正在将 Hibernate 与 JpaRepositories 一起使用。

实体类的相关部分是:

@Entity
public class Person {
   //.. id and other attributes

   @Column(name = "function")
   @ElementCollection
   private Set<String> functions;

  // .. getter setter
}
Run Code Online (Sandbox Code Playgroud)

我需要将我的实体类从只有一个函数更改为能够处理多个函数。我的一个 DAO 中有一个搜索函数,可以将所有现有函数与一个字符串进行比较,希望能找到已定义的函数。

最初的 JPA 查询是:

select DISTINCT(p.function) from Person p where UPPER(p.function) like UPPER(:term) order by p.function
Run Code Online (Sandbox Code Playgroud)

如何使用新的@ElementCollection 存档相同的结果?

Tim*_*sen 5

您需要将集合加入到Person然后选择。试试这个查询:

select DISTINCT(f) from Person p join p.functions f where UPPER(f) like UPPER(:term) order by f
Run Code Online (Sandbox Code Playgroud)