Close

JPQL IN Expression Examples

[Updated: May 16, 2018, Created: May 16, 2018]

Following example shows how to use JPQL IN keyword to apply a set of values in WHERE clause.

Example

Entity

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

Using IN expression

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

  public static void main(String[] args) {
      try {
          persistEmployees();
          findEmployeeBySalary();
          findEmployeeByName();
          //using list parameter
          findEmployeeByDept();
          //not in statement
          findEmployeeByDept2();
      } finally {
          entityManagerFactory.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();
  }

  private static void findEmployeeBySalary() {
      System.out.println("-- Employees with salary IN 2000, 3000 and 4000 --");
      EntityManager em = entityManagerFactory.createEntityManager();
      Query query = em.createQuery(
              "SELECT e FROM Employee e WHERE e.salary IN (2000L, 3000, 4000L) order by e.salary");
      List<Employee> resultList = query.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }

  private static void findEmployeeByName() {
      System.out.println("-- Employees name IN Diana, Mike and Rose --");
      EntityManager em = entityManagerFactory.createEntityManager();
      Query query = em.createQuery(
              "SELECT e FROM Employee e WHERE e.name IN ('Diana', 'Mike', 'Rose')");
      List<Employee> resultList = query.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }

  private static void findEmployeeByDept() {
      System.out.println("-- Employees name IN IT, Sales and HR --");
      EntityManager em = entityManagerFactory.createEntityManager();
      Query query = em.createQuery(
              "SELECT e FROM Employee e WHERE e.dept IN :deptNames");
      query.setParameter("deptNames", Arrays.asList("IT", "Sales", "HR"));
      List<Employee> resultList = query.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }

  private static void findEmployeeByDept2() {
      System.out.println("-- Employees name NOT IN IT, Sales and HR --");
      EntityManager em = entityManagerFactory.createEntityManager();
      Query query = em.createQuery(
              "SELECT e FROM Employee e WHERE e.dept NOT IN :deptNames");
      query.setParameter("deptNames", Arrays.asList("IT", "Sales", "HR"));
      List<Employee> resultList = query.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }
}
-- Employees with salary IN 2000, 3000 and 4000 --
Employee{id=2, name='Rose', dept='Sales', salary=2000}
Employee{id=1, name='Diana', dept='IT', salary=3000}
Employee{id=3, name='Denise', dept='Admin', salary=4000}
-- Employees name IN Diana, Mike and Rose --
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}
-- Employees name 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}
-- Employees name NOT IN IT, Sales and HR --
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.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 IN Expression Examples Select All Download
  • jpql-in-expression
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources
            • META-INF

    See Also