规范OR子句,in和isEmpty / isNull

问题描述 投票:0回答:1

我有能力(驾驶执照等)的工人,然后有机制需要一定的能力。有时机制根本不需要任何能力。

目前我有一个带有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]

Aaditi:

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];
spring-boot criteria-api
1个回答
1
投票

你有2个错误:

  1. 匹配空集合的标准是cb.isEmpty(root.get("competences"))
  2. 您需要指定左连接。 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)
);

qazxsw poi不会在qazxsw poi(qazxsw poi的结果)上工作 - 看点上面1点

尝试

isEmpty
© www.soinside.com 2019 - 2024. All rights reserved.