232 words

Criteria Query Selection and Results

The JPA Criteria API interfaces in this group are in use for setting the SELECT and ORDER BY clauses and for representing query results as tuples.

SELECT Clause Elements

The content of the SELECT clause in a criteria query is represented by Selection:

Because Selection is a super interface of Expression, any criteria expressionCriteria Query ExpressionsReference (JavaDoc) of JPA query expression interfaces, including Expression, Predicate, Path, ParameterExpression and Subquery.
See JavaDoc Reference Page...
 can be used as the SELECT clause content. Multiple selection expressions can be represented by CompoundSelection, which is itself a subinterface of Selection:

See the SELECT in Criteria Queries section for more details and examples.

ORDER BY Clause Elements

Elements in the ORDER BY clause are represented by the Order interface:

The Order interface is a thin wrapper around Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
, which adds order direction - either ascending (ASC) or descending (DESC). The CriteriaBuilderjavax.persistence.criteria.CriteriaBuilderJPA interfaceUsed to construct criteria queries, compound selections, expressions, predicates, orderings.
See JavaDoc Reference Page...
's ascasc(x)CriteriaBuilder's methodCreate an ordering by the ascending value of the expression.
See JavaDoc Reference Page...
and descdesc(x)CriteriaBuilder's methodCreate an ordering by the descending value of the expression.
See JavaDoc Reference Page...
methods take an expression and return an ascending or descending Order instance (respectively).

See the ORDER BY in Criteria Queries section for more details and examples.

Query Results as Tuples

JPA 2 introduces a new way to represent multi selection results (i.e. results of a query with multiple expressions in the SELECT clause) using tuples:

The Tuplejavax.persistence.TupleJPA interfaceInterface for extracting the elements of a query result tuple.
See JavaDoc Reference Page...
interface can be used as a clean alternative to Object[]. See the SELECT in Criteria Queries for more details and examples.

These interfaces are defined outside the javax.persistence.criteria package, since they might be useful also in string based JPQL queries. Using tuples with non criteria queries is supported by ObjectDB and it may be supported in the future by all the JPA providers.


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.