Close

JPQL - Constructor Expressions in the SELECT Clause

[Updated: Jul 30, 2018, Created: Jul 29, 2018]

In JPQL, a constructor can be used in the SELECT clause to return an instance of a non-entity Java object. For example:

  Query query = em.createQuery("SELECT NEW com.example.MyClass(e.attribute1, e.attribute2...) FROM MyEntity e");
  List<MyClass> resultList = query.getResultList();

Example

Entity

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

Object to be selected

public class EmployeeNameSalaryPair {
  private String name;
  private long salary;

  public EmployeeNameSalaryPair(String name, long salary) {
      this.name = name;
      this.salary = salary;
  }
    .............
}

Executing Query with Constructor Expression

Following example retrieves employee name-salary pairs both as Object[] and as EmployeeNameSalaryPair:

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

  public static void main(String[] args) {
      try {
          persistEmployees();
          getNameSalaryPairsAsObjectArray();
          getNameSalaryPairsViaConstructorExpression();

      } 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();

      System.out.println("-- employee persisted --");
      em.createQuery("Select e from Employee e")
        .getResultList()
        .forEach(System.out::println);
      em.close();
  }

  private static void getNameSalaryPairsAsObjectArray() {
      System.out.println("-- Employee name salary pair as Object[] --");
      EntityManager em = entityManagerFactory.createEntityManager();
      Query query = em.createQuery("SELECT e.name, e.salary FROM Employee e");
      List<Object[]> resultList = query.getResultList();
      resultList.forEach(arr -> System.out.println(Arrays.toString(arr)));
      em.close();
  }

  private static void getNameSalaryPairsViaConstructorExpression() {
      System.out.println("-- Employee name salary pair as EmployeeNameSalaryPair --");
      EntityManager em = entityManagerFactory.createEntityManager();
      Query query =
              em.createQuery("SELECT NEW com.logicbig.example.EmployeeNameSalaryPair(e.name, e.salary)"
              + " FROM Employee e");
      List<EmployeeNameSalaryPair> resultList = query.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }
}
-- employee persisted --
Employee{id=1, name='Diana', salary=2000, dept='IT'}
Employee{id=2, name='Rose', salary=3500, dept='Admin'}
Employee{id=3, name='Denise', salary=2500, dept='Admin'}
Employee{id=4, name='Mike', salary=4000, dept='IT'}
Employee{id=5, name='Linda', salary=4500, dept='Sales'}
-- Employee name salary pair as Object[] --
[Diana, 2000]
[Rose, 3500]
[Denise, 2500]
[Mike, 4000]
[Linda, 4500]
-- Employee name salary pair as EmployeeNameSalaryPair --
EmployeeNameSalaryPair{name='Diana', salary=2000}
EmployeeNameSalaryPair{name='Rose', salary=3500}
EmployeeNameSalaryPair{name='Denise', salary=2500}
EmployeeNameSalaryPair{name='Mike', salary=4000}
EmployeeNameSalaryPair{name='Linda', salary=4500}

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

Constructor Expressions in the SELECT Clause Select All Download
  • jpql-constructor-expression
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources
            • META-INF

    See Also