Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JPA Specifications Not In Query

Here the IN criteria query using Spring JPA Specification is working fine. but I don't know how to use "NOT IN"..

So How can i use the NOT IN criteria query using Spring JPA Specifications.

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;
}
like image 234
Vishnu Moorthy Kanagaraj Avatar asked Nov 01 '16 08:11

Vishnu Moorthy Kanagaraj


People also ask

What is Spring JPA specification?

SPring Data Jpa Specifications helps us to create dynamic queries based on the requirement at run time. Spring Data Jpa Specifications allows a combination of the attributes or properties of a domain or entity class and creates a query.

Is Spring data JPA an implementation of the JPA specification?

Spring Data JPA is not an implementation or JPA provider, it's just an abstraction used to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores.

What is JpaSpecificationExecutor?

The JpaSpecificationExecutor interface adds methods which will allow us to execute Specification s, for example, these: List<T> findAll(Specification<T> spec); Page<T> findAll(Specification<T> spec, Pageable pageable); List<T> findAll(Specification<T> spec, Sort sort);


2 Answers

Use the Predicate.not() method; the code is the same as in the IN case, just add .not():

case NOT_IN:
    if (searchCriteria.getValue() instanceof List<?>) {
        predicate = getFieldPath(searchCriteria.getKey(), root)
                .in(((List<?>) searchCriteria.getValue()).toArray())
                .not();
    }
    break;
like image 125
Nikos Paraskevopoulos Avatar answered Sep 22 '22 16:09

Nikos Paraskevopoulos


This functionality also works fine..

case NOT_IN:
if (searchCriteria.getValue() instanceof List<?>) {
    predicate = criteriaBuilder.not(getFieldPath(searchCriteria.getKey(), root).in(((List<?>) searchCriteria.getValue()).toArray()));
}
break;
like image 28
Vishnu Moorthy Kanagaraj Avatar answered Sep 19 '22 16:09

Vishnu Moorthy Kanagaraj