Close

JPA Criteria API - Modifying and Reusing Query Objects

[Updated: Apr 18, 2019, Created: Apr 18, 2019]

In Criteria API, a CriteriaQuery, CriteriaUpdate, or CriteriaDelete object may be modified, either before or after Query or TypedQuery objects have been created and executed from it.

Quick Example

     .........
     CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
     Root<Employee> root = criteriaQuery.from(Employee.class);

     //Employees name and salary
     criteriaQuery.multiselect(root.get(Employee_.NAME), root.get(Employee_.SALARY));
     List<Object[]> resultList = entityManager.createQuery(criteriaQuery).getResultList();
     resultList.forEach(arr -> System.out.println(Arrays.toString(arr)));

    
     //Employees name and dept
     criteriaQuery.multiselect(root.get(Employee_.NAME), root.get(Employee_.DEPT));
     List<Object[]> resultList2 = entityManager.createQuery(criteriaQuery).getResultList();
     resultList2.forEach(arr -> System.out.println(Arrays.toString(arr)));
     .....

Complete Example

Entity

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

Reusing Query Object

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

  public static void main(String[] args) {
      try {
          persistEmployees();
          findEmployees();
          findEmployees2();

      } finally {
          entityManagerFactory.close();
      }
  }

  private static void findEmployees() {
      EntityManager entityManager = entityManagerFactory.createEntityManager();
      CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
      CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
      Root<Employee> root = criteriaQuery.from(Employee.class);
      criteriaQuery.select(root);

      System.out.println("-- All employees --");
      List<Employee> resultList = entityManager.createQuery(criteriaQuery).getResultList();
      resultList.forEach(System.out::println);


      System.out.println("-- All employees from IT dept --");
      criteriaQuery.where(criteriaBuilder.equal(root.get(Employee_.DEPT), "IT"));
      List<Employee> resultList2 = entityManager.createQuery(criteriaQuery).getResultList();
      resultList2.forEach(System.out::println);

      entityManager.close();
  }

  private static void findEmployees2() {
      EntityManager entityManager = entityManagerFactory.createEntityManager();
      CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
      CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
      Root<Employee> root = criteriaQuery.from(Employee.class);

      System.out.println("-- Employees name and salary --");
      criteriaQuery.multiselect(root.get(Employee_.NAME), root.get(Employee_.SALARY));
      List<Object[]> resultList = entityManager.createQuery(criteriaQuery).getResultList();
      resultList.forEach(arr -> System.out.println(Arrays.toString(arr)));

      System.out.println("-- Employees name and dept --");
      criteriaQuery.multiselect(root.get(Employee_.NAME), root.get(Employee_.DEPT));
      List<Object[]> resultList2 = entityManager.createQuery(criteriaQuery).getResultList();
      resultList2.forEach(arr -> System.out.println(Arrays.toString(arr)));

      entityManager.close();
  }


  public static void persistEmployees() {
      Employee employee1 = Employee.create("Diana", 2000, "IT");
      Employee employee2 = Employee.create("Rose", 3500, "Admin");
      Employee employee3 = Employee.create("Denise", 2500, "Admin");
      Employee employee4 = Employee.create("Mike", 4000, "IT");
      Employee employee5 = Employee.create("Linda", 4500, "Sales");
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      em.persist(employee1);
      em.persist(employee2);
      em.persist(employee3);
      em.persist(employee4);
      em.persist(employee5);
      em.getTransaction().commit();
      em.close();
  }
}
-- All employees --
Employee{id=1, name='Diana', salary=2000.0, dept='IT'}
Employee{id=2, name='Rose', salary=3500.0, dept='Admin'}
Employee{id=3, name='Denise', salary=2500.0, dept='Admin'}
Employee{id=4, name='Mike', salary=4000.0, dept='IT'}
Employee{id=5, name='Linda', salary=4500.0, dept='Sales'}
-- All employees from IT dept --
Employee{id=1, name='Diana', salary=2000.0, dept='IT'}
Employee{id=4, name='Mike', salary=4000.0, dept='IT'}
-- Employees name and salary --
[Diana, 2000.0]
[Rose, 3500.0]
[Denise, 2500.0]
[Mike, 4000.0]
[Linda, 4500.0]
-- Employees name and dept --
[Diana, IT]
[Rose, Admin]
[Denise, Admin]
[Mike, IT]
[Linda, Sales]

Example Project

Dependencies and Technologies Used:

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

Modifying and Reusing Query Objects Select All Download
  • jpa-criteria-api-reuseable-objects
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources
            • META-INF

    See Also