Close

JPQL IS EMPTY Expression

[Last Updated: May 16, 2018]

This example shows how to use IS EMPTY comparison expression which tests whether a collection-valued relationship has been set.

Examples

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;
    .............
}

Using IS EMPTY

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();
        }
    }

    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);
    }

    private static void findEmployeeWithNoTask() {
        System.out.println("-- Employees with no tasks --");
        EntityManager em = entityManagerFactory.createEntityManager();
        Query query = em.createQuery("SELECT e FROM Employee e where e.tasks is EMPTY");
        List<Employee> resultList = query.getResultList();
        resultList.forEach(System.out::println);
        em.close();
    }

    private static void findEmployeeWithTasks() {
        System.out.println("-- Employees with tasks --");
        EntityManager em = entityManagerFactory.createEntityManager();
        Query query = em.createQuery("SELECT e FROM Employee e where e.tasks is NOT EMPTY");
        List<Employee> resultList = query.getResultList();
        resultList.forEach(System.out::println);
        em.close();
    }

    private static void findEmployeeWithNoTaskSupervisor() {
        System.out.println("-- Employees with no task supervisor --");
        EntityManager em = entityManagerFactory.createEntityManager();
        //IS EMPTY is used for collections only. To find employee with task having no supervisor use a join
        Query query = em.createQuery("SELECT DISTINCT e FROM Employee e INNER JOIN e.tasks t "
                + "where t.supervisor is NULL");
        List<Employee> resultList = query.getResultList();
        resultList.forEach(System.out::println);
        em.close();
    }
}
-- 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.2.13.Final: The core O/RM functionality as provided by Hibernate.
    Implements javax.persistence:javax.persistence-api version 2.1
  • JDK 1.8
  • Maven 3.3.9

JPQL IS EMPTY Expression Select All Download
  • jpql-is-empty-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources
            • META-INF

    See Also