Close

JPA Criteria API - Using ParameterExpression to supply query parameters at query execution time

[Updated: Aug 20, 2018, Created: Aug 20, 2018]

A ParameterExpression (extends Expression) represents a query parameter whose value is provided at the time when query is executed.

Following two methods are defined in CriteriaBuilder to create an instance of ParameterExpression:

ParameterExpression<T> parameter(Class<T> paramClass);

Quick example:

  CriteriaBuilder cb = entityManager.getCriteriaBuilder();
  //query creation
  CriteriaQuery<Employee> query = cb.createQuery(Employee.class);
  Root<Employee> employee = query.from(Employee.class);
  ParameterExpression<String> parameterExpression = cb.parameter(String.class);
  query.select(employee)
       .where(cb.equal(employee.get(Employee_.name), parameterExpression));
  //query execution 
  TypedQuery<Employee> typedQuery = entityManager.createQuery(query);
  typedQuery.setParameter(parameterExpression, "Diana");
  List<Employee> resultList = typedQuery.getResultList();

Following method is used to create a named parameter.

ParameterExpression<T> parameter(Class<T> paramClass, String name);

Quick example:

  //query execution
  CriteriaBuilder cb = entityManager.getCriteriaBuilder();
  CriteriaQuery<Employee> query = cb.createQuery(Employee.class);
  Root<Employee> employee = query.from(Employee.class);
  query.select(employee)
       .where(cb.equal(employee.get(Employee_.name), cb.parameter(String.class, "empName")));
  //query execution
  TypedQuery<Employee> typedQuery = entityManager.createQuery(query);
  typedQuery.setParameter("empName", "Diana");
  List<Employee> resultList = typedQuery.getResultList();

Example

Entity

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

Using ParameterExpression

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

  public static void main(String[] args) {
      try {
          persistEmployees();
          findAllEmployeeEntities();
          findEmployeeBySalaries();
          findEmployeeByDept();
      } finally {
          entityManagerFactory.close();
      }
  }

  public static void persistEmployees() {
      Employee employee1 = Employee.create("Diana", 2000, "IT");
      Employee employee2 = Employee.create("Rose", 4500, "Admin");
      Employee employee3 = Employee.create("Denise", 2000, "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();
  }

  private static void findAllEmployeeEntities() {
      System.out.println("-- finding Employee entities - entity selection --");
      EntityManager em = entityManagerFactory.createEntityManager();
      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery<Employee> query = cb.createQuery(Employee.class);
      Root<Employee> employee = query.from(Employee.class);
      query.select(employee);
      TypedQuery<Employee> typedQuery = em.createQuery(query);
      List<Employee> resultList = typedQuery.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }

  private static void findEmployeeBySalaries() {
      System.out.println("-- Employee with salaries greater than 2000 --");
      EntityManager em = entityManagerFactory.createEntityManager();
      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery<Employee> query = cb.createQuery(Employee.class);
      Root<Employee> employee = query.from(Employee.class);
      ParameterExpression<Double> parameterExpression = cb.parameter(Double.class);
      query.select(employee)
           .where(cb.gt(employee.get(Employee_.salary), parameterExpression));
      TypedQuery<Employee> typedQuery = em.createQuery(query);
      typedQuery.setParameter(parameterExpression, Double.valueOf(2000));
      List<Employee> resultList = typedQuery.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }

  private static void findEmployeeByDept() {
      System.out.println("-- Employee in IT dept --");
      EntityManager em = entityManagerFactory.createEntityManager();
      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery<Employee> query = cb.createQuery(Employee.class);
      Root<Employee> employee = query.from(Employee.class);
      ParameterExpression<String> parameterExpression = cb.parameter(String.class, "dept");
      query.select(employee)
           .where(cb.equal(employee.get(Employee_.dept), parameterExpression));
      TypedQuery<Employee> typedQuery = em.createQuery(query);
      typedQuery.setParameter("dept", "IT");
      List<Employee> resultList = typedQuery.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }
}
-- finding Employee entities - entity selection --
Employee{id=1, name='Diana', salary=2000.0, dept='IT'}
Employee{id=2, name='Rose', salary=4500.0, dept='Admin'}
Employee{id=3, name='Denise', salary=2000.0, dept='Admin'}
Employee{id=4, name='Mike', salary=4000.0, dept='IT'}
Employee{id=5, name='Linda', salary=4500.0, dept='Sales'}
-- Employee with salaries greater than 2000 --
Employee{id=2, name='Rose', salary=4500.0, dept='Admin'}
Employee{id=4, name='Mike', salary=4000.0, dept='IT'}
Employee{id=5, name='Linda', salary=4500.0, dept='Sales'}
-- Employee in IT dept --
Employee{id=1, name='Diana', salary=2000.0, dept='IT'}
Employee{id=4, name='Mike', salary=4000.0, dept='IT'}

Example Project

Dependencies and Technologies Used:

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

ParameterExpression Example Select All Download
  • jpa-criteria-api-parameter-expression
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources
            • META-INF

    See Also