Close

JPA Criteria API - Applying in() Predicate

[Updated: Nov 23, 2018, Created: Nov 23, 2018]

Following example shows how to use Expression.in() methods.

These methods test whether the expression is a member of the provided argument list.

package javax.persistence.criteria;
 ....
public interface Expression<T> extends Selection<T> {
     ....
    Predicate in(Object... values);
    Predicate in(Expression<?>... values);
    Predicate in(Collection<?> values);
    Predicate in(Expression<Collection<?>> values);
     ....
}

Example

Entity

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

Using in() methods

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

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

  private static void findEmployeeBySalary() {
      System.out.println("-- Employees with salary IN 2000, 3000 and 4000 --");
      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_.salary).in(2000, 3000, 4000));
      TypedQuery<Employee> typedQuery = em.createQuery(query);
      List<Employee> resultList = typedQuery.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }

  //negating IN predicate
  private static void findEmployeeByName() {
      System.out.println("-- Employees name NOT IN Diana and Rose --");
      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_.name).in("Diana", "Rose").not());
      TypedQuery<Employee> typedQuery = em.createQuery(query);
      List<Employee> resultList = typedQuery.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }

  //using  Predicate in(Expression<?>... values);
  private static void findEmployeeByDept() {
      System.out.println("-- Employees dept IN IT, Sales and HR --");
      EntityManager em = entityManagerFactory.createEntityManager();
      CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
      CriteriaQuery<Employee> query = criteriaBuilder.createQuery(Employee.class);
      Root<Employee> employee = query.from(Employee.class);
      ParameterExpression<Collection> deptParamExpression = criteriaBuilder.parameter(Collection.class);
      query.select(employee)
           .where(employee.get(Employee_.dept).in(deptParamExpression));
      TypedQuery<Employee> typedQuery = em.createQuery(query);
      typedQuery.setParameter(deptParamExpression, Arrays.asList("IT", "Sales", "HR"));
      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", 2000);
      Employee employee3 = Employee.create("Denise", "Admin", 4000);
      Employee employee4 = Employee.create("Mike", "HR", 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=2000}
Employee{id=3, name='Denise', dept='Admin', salary=4000}
Employee{id=4, name='Mike', dept='HR', salary=3500}
-- Employees with salary IN 2000, 3000 and 4000 --
Employee{id=1, name='Diana', dept='IT', salary=3000}
Employee{id=2, name='Rose', dept='Sales', salary=2000}
Employee{id=3, name='Denise', dept='Admin', salary=4000}
-- Employees name NOT IN Diana and Rose --
Employee{id=3, name='Denise', dept='Admin', salary=4000}
Employee{id=4, name='Mike', dept='HR', salary=3500}
-- Employees dept IN IT, Sales and HR --
Employee{id=1, name='Diana', dept='IT', salary=3000}
Employee{id=2, name='Rose', dept='Sales', salary=2000}
Employee{id=4, name='Mike', dept='HR', salary=3500}

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

JPA Criteria API - Expression.in() Examples Select All Download
  • jpa-criteria-api-in-predicate
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources
            • META-INF

    See Also