spring-boot - 规范 OR 子句,在 isEmpty/isNull 中
问题描述
我的员工有能力(驾驶执照等),还有一些机制需要一定的能力。有时,这些机制根本不需要能力。
目前,我有一个带有 in 子句的规范,可以正常工作,但我希望它也能发出不需要操作权限的机制。
public static Specification<Mechanism> hasCompetences(String searchTerm) {
return (root, query, criteriaBuilder) -> {
query.distinct(true);
List<String> list = new ArrayList<>(Arrays.asList(searchTerm.split(",")));
return root.join("competences").get("name").in(list);
};
}
如果我有 3 种具有以下能力的机制
汽车 | B类 |
范 | C类 |
自行车 |(这里没有数据) |
要求mechanisms?competences=B-Category
它按预期返回 Car 后,但我也想得到自行车。
或者有没有办法获得所有不需要能力的机制?我试过mechanisms?competences=
了,但回来了[]
。
编辑:
这就是我现在所处的位置:
public static Specification<Mechanism> hasCompetences(List<String> list) {
return (root, query, cb) -> {
query.distinct(true);
return cb.or(
cb.isEmpty(root.join("competences")),
root.join("competences").get("name").in(list)
);
};
}
但是 isEmpty 给了我这个错误:
java.lang.IllegalArgumentException: unknown collection expression type [org.hibernate.query.criteria.internal.path.SetAttributeJoin]
编辑2:
public static Specification<Mechanism> hasCompetences(List<String> list) {
return (root, query, cb) -> {
query.distinct(true);
Join<Mechanism, Set<Competence>> competences = root.join("competences", JoinType.LEFT);
return cb.or(
root.join("competences").get("name").in(list),
cb.isEmpty(competences)
);
};
}
错误:
unknown collection expression type [org.hibernate.query.criteria.internal.path.SetAttributeJoin];
解决方案
您有 2 个错误:
- 匹配空集合的条件是
cb.isEmpty(root.get("competences"))
- 您需要指定左连接。
root.join("competences", JoinType.LEFT)
如果没有第二个修改,您将进行内部连接,因此您将永远不会检索具有空权限的机制。
更新
你提议
Join<Mechanism, Set<Competence>> competences = root.join("competences", JoinType.LEFT);
return cb.or(
root.join("competences").get("name").in(list),
cb.isEmpty(competences)
);
isEmpty
不起作用SetAttributeJoin
(的结果root.join
) - 看上面的第 1 点
尝试
Join<Mechanism, Set<Competence>> competences = root.join("competences", JoinType.LEFT);
return cb.or(
competences.get("name").in(list),
cb.isEmpty(root.get("competences"))
);
推荐阅读
- export-to-csv - 在 excel 中打开时,角度导出到 csv 不会将 unixtimestamp 值转换为日期
- oracle - HIbernate 5、Oracle 和本机查询 - 从 H4 升级时的破坏行为?
- angular - 当它是一个对象数组时,如何在 Reactive Forms 中设置特定字段的值?
- angular - Angular原理图缓存在某处
- flutter - Flutter Firestore 集合组
- python - 如何从 Amazon S3 ls 命令对文件进行分类?
- swift - 从托管在 monorepo 中的 GitHub 发布 Cocoapod
- flutter - 在使用 Provider 构建期间调用的 setState() 或 markNeedsBuild()
- javascript - 几秒钟后播放声音时的 React js 和 Howler js 包问题
- visual-c++ - 从 DirectX12 调试层获取有用信息