亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

子句中的 Criteria API(過濾)

子句中的 Criteria API(過濾)

臨摹微笑 2022-07-27 21:24:32
嘿,我的查詢有一個小問題。我需要找到具有特定能力的用戶(如許可證和東西)??梢哉f我有能力的用戶:亞歷克斯:A、B、C約翰:A,B史蒂夫:B,C要求:workers?competences=A,B,C - 應該只返回 Alex目前返回 Alex 3 次,John 和 Steve 兩次workers?competences=A,C - 應該只返回 Alex目前返回亞歷克斯兩次,約翰和史蒂夫一次workers?competences=B,C - 應該返回 Alex 和 Steve目前返回 Alex 兩次,Steve 兩次,John 一次workers?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);        };    }
查看完整描述

1 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

由于 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')

在criteriaAPI 中似乎.distinct(true)


更新


IN是一個OR條件。如果您只想name擁有所有能力,您應該執行以下操作(假設能力不會有一個人的多個條目)


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

3是IN數組的長度


查看完整回答
反對 回復 2022-07-27
  • 1 回答
  • 0 關注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號