Spring JPA 规范未在查询中

Spring JPA Specifications Not In Query

此处使用 Spring JPA 规范的 IN 标准查询工作正常。但我不知道如何使用 "NOT IN"..

那么我如何使用 Spring JPA 规范的 NOT IN 条件查询。

SearchSpecification<User> spec = new CommonSpecification<Case>(new SearchCriteria("client.id", Operator.NOT_IN, clientIdList));

@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
    Predicate predicate = null;
    switch (searchCriteria.getOperator()) {

    case IN:
        if (searchCriteria.getValue() instanceof List<?>) {
            predicate = getFieldPath(searchCriteria.getKey(), root)
                    .in(((List<?>) searchCriteria.getValue()).toArray());
        }
        break;
    case NOT_IN:
        //What to do???
        break;
    default:
        break;

    }
    return predicate;
}


private Path<Object> getFieldPath(String key, Root<T> root) {
    Path<Object> fieldPath = null;
    if (key.contains(".")) {
        String[] fields = key.split("\.");
        for (String field : fields) {
            if (fieldPath == null) {
                fieldPath = root.get(field);
            } else {
                fieldPath = fieldPath.get(field);
            }
        }
    } else {
        fieldPath = root.get(key);
    }
    return fieldPath;
}

使用Predicate.not()方法;代码与 IN 的情况相同,只需添加 .not():

case NOT_IN:
    if (searchCriteria.getValue() instanceof List<?>) {
        predicate = getFieldPath(searchCriteria.getKey(), root)
                .in(((List<?>) searchCriteria.getValue()).toArray())
                .not();
    }
    break;

此功能也可以正常工作..

case NOT_IN:
if (searchCriteria.getValue() instanceof List<?>) {
    predicate = criteriaBuilder.not(getFieldPath(searchCriteria.getKey(), root).in(((List<?>) searchCriteria.getValue()).toArray()));
}
break;