Close

JPA Criteria API - Applying 'Is Empty' and 'Is Not Empty' Predicate

[Updated: Oct 22, 2018, Created: Oct 21, 2018]

Following example shows how to use CriteriaBuilder.isEmpty() and CriteriaBuilder.isNotEmpty() methods.

IsEmpty() method creates a predicate that tests whether a collection is empty.

<C extends Collection<?>> Predicate isEmpty(Expression<C> collection);
<C extends Collection<?>> Predicate isNotEmpty(Expression<C> collection);

Quick Example:

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
  CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
  Root<Employee> employee = criteriaQuery.from(Employee.class);
  criteriaQuery.select(employee)
               .where(criteriaBuilder.isEmpty(employee.get(Employee_.tasks)));
  TypedQuery<Employee> typedQuery = entityManager.createQuery(criteriaQuery);
  List<Employee> list = typedQuery.getResultList();

Complete Example

Entities

@Entity
public class Employee {
  @Id
  @GeneratedValue
  private long id;
  private String name;
  @ManyToMany(cascade = CascadeType.ALL)
  private List<Task> tasks;
    .............
}
@Entity
public class Task {
  @Id
  @GeneratedValue
  private long id;
  private String description;
  private String supervisor;
    .............
}

Applying isEmpty() and isNotEmpty() predicates

public class ExampleMain {
  private static EntityManagerFactory entityManagerFactory =
          Persistence.createEntityManagerFactory("example-unit");

  public static void main(String[] args) {
      try {
          persistEmployees();
          findEmployeeWithNoTask();
          findEmployeeWithTasks();
          findEmployeeWithNoTaskSupervisor();
      } finally {
          entityManagerFactory.close();
      }
  }

  private static void findEmployeeWithNoTask() {
      System.out.println("-- Employees with no tasks --");
      EntityManager em = entityManagerFactory.createEntityManager();
      CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
      CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
      Root<Employee> employee = criteriaQuery.from(Employee.class);
      criteriaQuery.select(employee)
                   .where(criteriaBuilder.isEmpty(employee.get(Employee_.tasks)));
      TypedQuery<Employee> typedQuery = em.createQuery(criteriaQuery);
      typedQuery.getResultList().forEach(System.out::println);
      em.close();
  }

  private static void findEmployeeWithTasks() {
      System.out.println("-- Employees with tasks --");
      EntityManager em = entityManagerFactory.createEntityManager();
      CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
      CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
      Root<Employee> employee = criteriaQuery.from(Employee.class);
      criteriaQuery.select(employee)
                   .where(criteriaBuilder.isNotEmpty(employee.get(Employee_.tasks)));
      TypedQuery<Employee> typedQuery = em.createQuery(criteriaQuery);
      typedQuery.getResultList().forEach(System.out::println);
      em.close();
 }

  private static void findEmployeeWithNoTaskSupervisor() {
      System.out.println("-- Employees with no task supervisor --");
      EntityManager em = entityManagerFactory.createEntityManager();
      CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
      CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
      Root<Employee> employee = criteriaQuery.from(Employee.class);
      ListJoin<Employee, Task> tasks = employee.join(Employee_.tasks);
      criteriaQuery.select(employee)
                   .distinct(true)
                   .where(criteriaBuilder.isNull(tasks.get(Task_.supervisor)));
      TypedQuery<Employee> typedQuery = em.createQuery(criteriaQuery);
      typedQuery.getResultList().forEach(System.out::println);
      em.close();
  }

  public static void persistEmployees() {
      Task task1 = new Task("Coding", "Denise");
      Task task2 = new Task("Refactoring", "Rose");
      Task task3 = new Task("Designing", "Denise");
      Task task4 = new Task("Documentation", null);

      Employee employee1 = Employee.create("Diana", task1, task3);
      Employee employee2 = Employee.create("Mike");
      Employee employee3 = Employee.create("Tim", task2, task4);
      Employee employee4 = Employee.create("Jack");

      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      em.persist(employee1);
      em.persist(employee2);
      em.persist(employee3);
      em.persist(employee4);
      em.getTransaction().commit();
      em.close();
      System.out.println("-- Employee persisted --");
      System.out.println(employee1);
      System.out.println(employee2);
      System.out.println(employee3);
      System.out.println(employee4);
  }
}
-- Employee persisted --
Employee{id=1, name='Diana', tasks=[Task{id=2, description='Coding', supervisor='Denise'}, Task{id=3, description='Designing', supervisor='Denise'}]}
Employee{id=4, name='Mike', tasks=[]}
Employee{id=5, name='Tim', tasks=[Task{id=6, description='Refactoring', supervisor='Rose'}, Task{id=7, description='Documentation', supervisor='null'}]}
Employee{id=8, name='Jack', tasks=[]}
-- Employees with no tasks --
Employee{id=4, name='Mike', tasks=[]}
Employee{id=8, name='Jack', tasks=[]}
-- Employees with tasks --
Employee{id=1, name='Diana', tasks=[Task{id=2, description='Coding', supervisor='Denise'}, Task{id=3, description='Designing', supervisor='Denise'}]}
Employee{id=5, name='Tim', tasks=[Task{id=6, description='Refactoring', supervisor='Rose'}, Task{id=7, description='Documentation', supervisor='null'}]}
-- Employees with no task supervisor --
Employee{id=5, name='Tim', tasks=[Task{id=6, description='Refactoring', supervisor='Rose'}, Task{id=7, description='Documentation', supervisor='null'}]}

Example Project

Dependencies and Technologies Used:

  • h2 1.4.197: H2 Database Engine.
  • hibernate-core 5.3.6.Final: Hibernate's core ORM functionality.
    Implements javax.persistence:javax.persistence-api version 2.2
  • hibernate-jpamodelgen 5.3.6.Final: Annotation Processor to generate JPA 2 static metamodel classes.
  • JDK 1.8
  • Maven 3.5.4

isEmpty() and isNotEmpty() Examples Select All Download
  • jpa-criteria-api-is-empty-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources
            • META-INF

    See Also