Close

JPQL - Using TypedQuery

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

Untyped queries

Consider the following method of EntityManager:

public Query createQuery(String qlString);

Which can be used to get the query result by using following methods defined in Query class:

List getResultList();
Object getSingleResult();

As seen above both of the methods do not return the exact type of result (we have to do unsafe assignment/casting).

Typed Queries

To get typed result in JPQL, we can use following method of EntityManager

public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass);

and then we can use following methods of TypedQuery<X>:

List<X> getResultList();
X getSingleResult();

TypedQuery is a subclass of Query.

Example

@Entity
public class Employee {
  @Id
  @GeneratedValue
  private long id;
  private String name;
  private long salary;
  @ManyToOne(cascade = CascadeType.ALL)
  private Department dept;
    .............
  public static Employee create(String name, int salary, Department dept) {
      Employee e = new Employee();
      e.setName(name);
      e.setSalary(salary);
      e.setDept(dept);
      dept.addEmployee(e);
      return e;
  }
    .............
}
@Entity
public class Department {
  @Id
  @GeneratedValue
  private long id;
  private String name;
  @OneToMany(mappedBy = "dept")
  private List<Employee> employees;
    .............
  public void addEmployee(Employee e) {
      if (this.employees == null) {
          employees = new ArrayList<>();
      }
      employees.add(e);
  }
    .............
}

Using TypedQuery

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

  public static void main(String[] args) {
      try {
          persistEmployees();
          findEmployees();
          findEmployeeSalaries();
          findDepartments();
          findHighestSalaryDept();
      } finally {
          entityManagerFactory.close();
      }
  }

  public static void persistEmployees() {
      Department deptIt = new Department();
      deptIt.setName("IT");

      Department deptAdmin = new Department();
      deptAdmin.setName("Admin");

      Employee employee1 = Employee.create("Diana", 3000, deptIt);
      Employee employee2 = Employee.create("Rose", 4000, deptAdmin);
      Employee employee3 = Employee.create("Denise", 1500, deptIt);
      Employee employee4 = Employee.create("Mike", 2000, deptAdmin);
      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 findEmployees() {
      System.out.println("-- all Employees--");
      EntityManager em = entityManagerFactory.createEntityManager();
      TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e", Employee.class);
      List<Employee> resultList = query.getResultList();
      resultList.forEach(System.out::println);
  }

  private static void findEmployeeSalaries() {
      System.out.println("-- Employee salaries --");
      EntityManager em = entityManagerFactory.createEntityManager();
      TypedQuery<Long> query = em.createQuery("SELECT e.salary FROM Employee e", Long.class);
      List<Long> resultList = query.getResultList();
      resultList.forEach(System.out::println);
  }

  private static void findDepartments() {
      System.out.println("-- All dept departments --");
      EntityManager em = entityManagerFactory.createEntityManager();
      TypedQuery<Department> query = em.createQuery("SELECT d FROM Department d", Department.class);
      List<Department> resultList = query.getResultList();
      resultList.forEach(System.out::println);
  }

  private static void findHighestSalaryDept() {
      System.out.println("-- Dept with max salary --");
      EntityManager em = entityManagerFactory.createEntityManager();
      TypedQuery<String> query = em.createQuery("SELECT d.name FROM Department d "
                      + " JOIN d.employees e where e.salary = (SELECT MAX(e2.salary) FROM Employee e2) ",
              String.class);
      String dept = query.getSingleResult();
      System.out.println(dept);
  }
}
-- all Employees--
Employee{id=1, name='Diana', salary=3000, dept='IT'}
Employee{id=3, name='Rose', salary=4000, dept='Admin'}
Employee{id=5, name='Denise', salary=1500, dept='IT'}
Employee{id=6, name='Mike', salary=2000, dept='Admin'}
-- Employee salaries --
3000
4000
1500
2000
-- All dept departments --
Department{id=2, name='IT', employees=[Employee{id=1, name='Diana', salary=3000, dept='IT'}, Employee{id=5, name='Denise', salary=1500, dept='IT'}]}
Department{id=4, name='Admin', employees=[Employee{id=3, name='Rose', salary=4000, dept='Admin'}, Employee{id=6, name='Mike', salary=2000, dept='Admin'}]}
-- Dept with max salary --
Admin

Example Project

Dependencies and Technologies Used:

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

Using TypedQuery Select All Download
  • jpql-typed-query-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources
            • META-INF

    See Also