Close

JPQL Case Expressions

[Updated: Jul 2, 2018, Created: Jul 2, 2018]

JPQL CASE expression is similar to Java 'switch' statement which selects one of the multiple choices based on a provided runtime value. Following is the general syntax of JPQL CASE expression:

CASE property 
   WHEN property-value-1 THEN scalar-expression-1
   WHEN property-value-2 THEN scalar-expression-2
   .....
   ELSE scalar-expression-n

The 'property' (same or different) can also be used individually within 'WHEN' expression:

CASE 
   WHEN property=property-value-1 THEN scalar-expression-1
   WHEN property=property-value-2 THEN scalar-expression-2
   .....
   ELSE scalar-expression-n

Example

Entities

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

Using CASE expressions

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

  public static void main(String[] args) {
      try {
          persistEmployees();
          findAllEmployees();
      } finally {
          entityManagerFactory.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();
  }

  private static void findAllEmployees() {
      EntityManager em = entityManagerFactory.createEntityManager();
      System.out.println("-- All employees --");
      Query query = em.createQuery(
              "SELECT e.name, e.salary, "
                      + "CASE e.dept "
                      + "WHEN 'IT' THEN 'Information Technology' "
                      + "WHEN 'Admin' THEN 'Administration' "
                      + " ELSE e.dept "
                      + "END "
                      + "FROM Employee e");
      List<Object[]> resultList = query.getResultList();
      resultList.forEach(o -> System.out.println(Arrays.toString(o)));
      em.close();
  }
}
-- All employees --
[Diana, 2000.0, Information Technology]
[Rose, 3500.0, Administration]
[Denise, 2500.0, Administration]
[Mike, 4000.0, Information Technology]
[Linda, 4500.0, Sales]

CASE Expression with polymorphic types

'CASE' can also be used in polymorphic type selection. Check out an example here.

Example Project

Dependencies and Technologies Used:

  • h2 1.4.197: H2 Database Engine.
  • hibernate-core 5.3.1.Final: Hibernate's core ORM functionality.
    Implements javax.persistence:javax.persistence-api version 2.2
  • JDK 1.8
  • Maven 3.5.4

Case expression example Select All Download
  • jpql-case-expressions
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources
            • META-INF

    See Also