簡體   English   中英

子句中的Criteria API(過濾)

[英]Criteria API in clause (filtering)

嘿,我的查詢有一個小問題。

我需要找到具有特定能力(如許可證和資料)的用戶。

可以說我的用戶具有能力:

  • 亞歷克斯:A,B,C
  • 約翰:A,B
  • 史蒂夫:B,C

要求:

  • worker?competences = A,B,C-應該只返回Alex
    • 目前返回Alex 3次,John和Steve 2次
  • worker?competences = A,C-應該只返回Alex
    • 目前返回兩次Alex,John和Steve一次
  • worker?competences = B,C-應該返回Alex和Steve
    • 目前返回兩次Alex,Steve兩次,John一次
  • worker?competences = B-應該返回所有用戶
    • 目前返回每個人一次

當前,它查找具有能力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

INOR條件。 如果您只想使用所有能力來name ,則應執行以下操作(假設一個人不會有多個條目)

select name from competences where competence in ('A', 'B', 'C') group by name having count(name) = 3;

3IN數組的長度

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM