271 words

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 TypedQueryjavax.persistence.TypedQueryJPA interfaceInterface used to control the execution of typed queries.
See JavaDoc Reference Page...
interface (or alternatively the older Queryjavax.persistence.QueryJPA interfaceInterface used to control query execution.
See JavaDoc Reference Page...
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 whose structure is only known 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 TypedQueryjavax.persistence.TypedQueryJPA interfaceInterface used to control the execution of typed queries.
See JavaDoc Reference Page...
or a Queryjavax.persistence.QueryJPA interfaceInterface used to control query execution.
See JavaDoc Reference Page...
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 TypedQueryjavax.persistence.TypedQueryJPA interfaceInterface used to control the execution of typed queries.
See JavaDoc Reference Page...
instance. The CriteriaQueryjavax.persistence.criteria.CriteriaQueryJPA interfaceThe CriteriaQuery interface defines functionality that is specific to top-level queries.
See JavaDoc Reference Page...
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 CriteriaBuilderjavax.persistence.criteria.CriteriaBuilderJPA interfaceUsed to construct criteria queries, compound selections, expressions, predicates, orderings.
See JavaDoc Reference Page...
instance is the factory of criteria queries as well as of criteria query elements. It can be obtained either by the EntityManagerFactoryjavax.persistence.EntityManagerFactoryJPA interfaceInterface used to interact with the entity manager factory for the persistence unit.
See JavaDoc Reference Page...
's getCriteriaBuildergetCriteriaBuilder()EntityManagerFactory's methodReturn an instance of CriteriaBuilder for the creation of CriteriaQuery objects.
See JavaDoc Reference Page...
method or by the EntityManagerjavax.persistence.EntityManagerJPA interfaceInterface used to interact with the persistence context.
See JavaDoc Reference Page...
's getCriteriaBuildergetCriteriaBuilder()EntityManager's methodReturn an instance of CriteriaBuilder for the creation of CriteriaQuery objects.
See JavaDoc Reference Page...
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):


This documentation page is derived (with some adjustments) from the open source JPA 2 RI (EclipseLink)
and is available under the terms of the Eclipse Public License, v. 1.0 and Eclipse Distribution License, v. 1.0.
Object Relational Mapping (ORM) JPA 2 providers include Hibernate, EclipseLink, TopLink, OpenJPA and DataNucleus.
Object DB is not an ORM JPA implementation but an Object Database for Java with built in JPA 2 support.