Close

JPA Criteria API - Applying isNull() and isNotNull() Predicate

[Last Updated: Nov 23, 2018]

Following example shows how to use Expression.isNull() and Expression.isNotNull() methods.

These methods test whether the target entity field value is null or not null.

package javax.persistence.criteria;
 ...
public interface Expression<T> extends Selection<T> {

    Predicate isNull();
    Predicate isNotNull();
     ....
}

Example

@Entity
public class Employee {
  @Id
  @GeneratedValue
  private long id;
  private String name;
  private String dept;
  private long salary;
    .............
}

Using isNull() and isNotNull() methods

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

  public static void main(String[] args) {
      try {
          persistEmployees();
          findEmployeeByNullDept();
          findEmployeeByDeptNoNull();
      } finally {
          entityManagerFactory.close();
      }
  }

  private static void findEmployeeByNullDept() {
      System.out.println("-- Employees with dept = NULL --");
      EntityManager em = entityManagerFactory.createEntityManager();
      CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
      CriteriaQuery<Employee> query = criteriaBuilder.createQuery(Employee.class);
      Root<Employee> employee = query.from(Employee.class);
      query.select(employee)
           .where(employee.get(Employee_.dept).isNull());
      TypedQuery<Employee> typedQuery = em.createQuery(query);
      List<Employee> resultList = typedQuery.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }

  private static void findEmployeeByDeptNoNull() {
      System.out.println("-- Employees with dept not NULL --");
      EntityManager em = entityManagerFactory.createEntityManager();
      CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
      CriteriaQuery<Employee> query = criteriaBuilder.createQuery(Employee.class);
      Root<Employee> employee = query.from(Employee.class);
      query.select(employee)
           .where(employee.get(Employee_.dept).isNotNull());
      TypedQuery<Employee> typedQuery = em.createQuery(query);
      List<Employee> resultList = typedQuery.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }

  public static void persistEmployees() {
      Employee employee1 = Employee.create("Diana", "IT", 3000);
      Employee employee2 = Employee.create("Rose", "Sales", 2500);
      Employee employee3 = Employee.create("Denise", "Admin", 4000);
      Employee employee4 = Employee.create("Mike", null, 3500);
      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("-- employees persisted --");
      System.out.println(employee1);
      System.out.println(employee2);
      System.out.println(employee3);
      System.out.println(employee4);
  }
}
-- employees persisted --
Employee{id=1, name='Diana', dept='IT', salary=3000}
Employee{id=2, name='Rose', dept='Sales', salary=2500}
Employee{id=3, name='Denise', dept='Admin', salary=4000}
Employee{id=4, name='Mike', dept='null', salary=3500}
-- Employees with dept = NULL --
Employee{id=4, name='Mike', dept='null', salary=3500}
-- Employees with dept not NULL --
Employee{id=1, name='Diana', dept='IT', salary=3000}
Employee{id=2, name='Rose', dept='Sales', salary=2500}
Employee{id=3, name='Denise', dept='Admin', salary=4000}

Example Project

Dependencies and Technologies Used:

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

Applying isNull() and isNotNull() Predicate Select All Download
  • jpa-criteria-api-null-restriction
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources
            • META-INF

    See Also