JPA Queries
Queries are represented in JPA by the Query
and TypedQuery
interfaces:
The JPA Query API section (in chapter 4 of the ObjectDB manual) provides detailed explanation of how exactly to use these interfaces to build and run JPQL queries.
The TypedQuery
javax.persistence.TypedQueryQuery
javax.persistence.Query - JPA InterfaceInterface used to control query execution.javax.jdo.Query - JDO InterfaceThe Query
interface allows applications to obtain persistent instances, values, and aggregate data from the data store. interface) is the only neccessary interface for defining and running string based JPQL queries (e.g. "SELECT c FROM Country"
).
Criteria Query API
Building dynamic queries with a structure that is known only at runtime (e.g. depending on which fields are filled by a user in a form) can be done by concatenating JPQL strings into a valid complete JPQL query. JPA 2 introduced the JPA Criteria Query API, as a cleaner alternative that enables building a dynamic query by using instances of special types, representing query elements. Running criteria queries, however, still requires a TypedQuery
javax.persistence.TypedQueryQuery
javax.persistence.Query - JPA InterfaceInterface used to control query execution.javax.jdo.Query - JDO InterfaceThe Query
interface allows applications to obtain persistent instances, values, and aggregate data from the data store. instance.
Building a dynamic query using the criteria API is managed by the CriteriaQuery
interface:
As noted above, eventually every criteria query is managed by an ordinary TypedQuery
javax.persistence.TypedQueryCriteriaQuery
javax.persistence.criteria.CriteriaQueryCriteriaQuery
interface defines functionality that is specific to top-level queries. instance merely represents the query during its building, and is equivalent to a JPQL query string (i.e. to a String
instance containing JPQL).
An essential interface for using the criteria API is the CriteriaBuilder
interface:
A CriteriaBuilder
javax.persistence.criteria.CriteriaBuilder - JPA InterfaceUsed to construct criteria queries, compound selections, expressions, predicates, orderings. instance is the factory of criteria queries as well as of criteria query elements. It can be obtained either by the EntityManagerFactory
javax.persistence.EntityManagerFactory - JPA InterfaceInterface used to interact with the entity manager factory for the persistence unit.'s getCriteriaBuilder
EntityManagerFactory.getCriteriaBuilder() - JPA MethodReturn an instance of CriteriaBuilder
for the creation of CriteriaQuery
objects. method or by the EntityManager
javax.persistence.EntityManager - JPA InterfaceInterface used to interact with the persistence context.'s getCriteriaBuilder
EntityManager.getCriteriaBuilder() - JPA MethodReturn an instance of CriteriaBuilder
for the creation of CriteriaQuery
objects. method (both methods are equivalent).
Criteria Query Elements
Criteria query elements are organized in this reference into three groups.
SELECT and ORDER BY elements (including tuples):
FROM clause elements (representing range variables, join and fetch):
Other criteria query expressions (for all the query clauses):