[英]Criteria API in clause (filtering)
嘿,我的查詢有一個小問題。
我需要找到具有特定能力(如許可證和資料)的用戶。
可以說我的用戶具有能力:
要求:
當前,它查找具有能力A或B或C的所有用戶。
我需要它返回具有所有已插入能力的用戶,因此competence A AND competence B AND competence C
這是我目前的規格:
public static Specification<WorkDetail> hasCompetences(String searchTerm) {
return (root, query, criteriaBuilder) -> {
List<String> list = new ArrayList<>(Arrays.asList(searchTerm.split(",")));
Join join = root.join("competences");
return join.get("name").in(list);
};
}
由於Alex有3個結果。 我認為您的表結構如下所示
name | competence
-----------------
Alex | A
Alex | B
Alex | C
John | A
John | B
您當前在sql中的查詢可能看起來像
select name from competences where competence in ('A', 'B', 'C')
您可能要添加distinct
select distinct name from competences where competence in ('A', 'B', 'C')
在criteria
API中似乎是.distinct(true)
UPDATE
IN
是OR
條件。 如果您只想使用所有能力來name
,則應執行以下操作(假設一個人不會有多個條目)
select name from competences where competence in ('A', 'B', 'C') group by name having count(name) = 3;
3
是IN
數組的長度
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.