Querying in JPA 2: Typesafe and Object Oriented
Query Projection
The result of a criteria query is the same as what is specified during the criteria query creation. The result can also be explicitly specified by passing a query root to the CriteriaQuery.select
. The criteria query also gives the developer the strength to project the result in various types, as discussed in the following sections.
Using the construct()
Using this approach, the results of a query can be made to be of a non entity type. In the following code snippet, a criteria query object is created for the EmployeeDetails
class, which is a non entity type. This criteria query object is necessary for the EmployeeDetails
class to have a constructor that accepts a String
and an Integer
type argument. The CriteriaBuilder.construct
method is used to create the instance of the EmployeeDetails
type.
CriteriaQuery<EmployeeDetails> criteriaQuery = criteriaBuilder.createQuery(EmployeeDetails.class);Root<Employee> employee = criteriaQuery.from(Employee.class);criteriaQuery.select(criteriaBuilder.construct(EmployeeDetails.class, employee.get(Employee_.name), employee.get(Employee_.age)));em.createQuery(criteriaQuery).getResultList();Corresponding SQL: SELECT name, age FROM employee
Query Returning Object[]
Criteria queries can also return the Object[]
type by setting the values to the CriteriaBuilder.array
method. In the following code snippet, the array size is 2 (composed of a String
and an Integer
value).
CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);Root<Employee> employee = criteriaQuery.from(Employee.class);criteriaQuery.select(criteriaBuilder.array(employee.get(Employee_.name), employee.get(Employee_.age)));em.createQuery(criteriaQuery).getResultList();Corresponding SQL: SELECT name, age FROM employee
Query Returning a Tuple
A row of data or a single record in the database is generally referred to as a tuple. Queries can be made on the tuple by invoking the CriteriaBuilder.createTupleQuery()
method. A CriteriaQuery.multiselect
method is passed with the parameters, which must be returned from the query in a single record of data.
CriteriaQuery<Tuple> criteriaQuery = criteriaBuilder.createTupleQuery();Root<Employee> employee = criteriaQuery.from(Employee.class);criteriaQuery.multiselect(employee.get(Employee_.name).alias("name"), employee.get(Employee_.age).alias("age"));em.createQuery(criteriaQuery).getResultList();Corresponding SQL: SELECT name, age FROM employee
Conclusion
Criteria queries are a means of querying the database in a more object-oriented fashion. In this article I discussed typesafe criteria queries in JPA 2 as well as the concept of metamodel, which is vital for understanding criteria queries. The various APIs for criteria queries were also discussed.
Acknowledgements
The author would like to sincerely thank Mr. Subrahmanya SV (VP, ECOM Research Group, E&R) for his constant encouragement and Ms. Sangeetha S for providing ideas, guidance and valuable comments, as well as kindly reviewing this article.
About the Author
Nitin KL works at the E-Commerce Research Labs at Infosys Technologies. He is involved in design and development of Java EE applications using Hibernate, iBATIS, and JPA.
Page 3 of 3
This article was originally published on September 10, 2010