Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA CriteriaBuilder - not in a Collection

What is the difference between:

First test run on the Builder:

Predicate predicate = root.get(PersonEntity_.name).in(names);
criteriaBuilder.not(predicate);

Second test run on the Query:

Predicate predicate2 = root.get(PersonEntity_.name).in(names).not();
criteriaQuery.where(predicate2);

This seems to give the same results. Am I missing something? Should we choose the CriteriaBuilder above the CriteriaQuery?

Complete example:

List<String> names = new ArrayList<String>();
names.add("John");
names.add("Emma");

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<PersonEntity> criteriaQuery = criteriaBuilder.createQuery(PersonEntity.class);
Root<PersonEntity> root = criteriaQuery.from(PersonEntity.class);

// First test run on the Builder
Predicate predicate = root.get(PersonEntity_.name).in(names);
criteriaBuilder.not(predicate);

  // Second test run the query

// Predicate predicate2 = root.get(PersonEntity_.name).in(names).not(); // criteriaQuery.where(predicate2);

List<PersonEntity> list = entityManager.createQuery(cq).getResultList();
like image 765
Dimitri Dewaele Avatar asked Nov 11 '22 14:11

Dimitri Dewaele


1 Answers

There is not difference between these variants. Both statements result in the same.

like image 117
unwichtich Avatar answered Jan 04 '23 02:01

unwichtich