Criteria Queries
Hibernate features an intuitive, extensible criteria query API.
Creating a Criteria instance
The interface org.hibernate.Criteria represents a query against
a particular persistent class. The Session is a factory for
Criteria instances.
Narrowing the result set
An individual query criterion is an instance of the interface
org.hibernate.criterion.Criterion. The class
org.hibernate.criterion.Restrictions defines
factory methods for obtaining certain built-in
Criterion types.
Restrictions may be grouped logically.
There are quite a range of built-in criterion types (Restrictions
subclasses), but one that is especially useful lets you specify SQL directly.
The {alias} placeholder with be replaced by the row alias
of the queried entity.
An alternative approach to obtaining a criterion is to get it from a
Property instance. You can create a Property
by calling Property.forName().
Ordering the results
You may order the results using org.hibernate.criterion.Order.
Associations
You may easily specify constraints upon related entities by navigating
associations using createCriteria().
note that the second createCriteria() returns a new
instance of Criteria, which refers to the elements of
the kittens collection.
The following, alternate form is useful in certain circumstances.
(createAlias() does not create a new instance of
Criteria.)
Note that the kittens collections held by the Cat instances
returned by the previous two queries are not pre-filtered
by the criteria! If you wish to retrieve just the kittens that match the
criteria, you must use returnMaps().
Dynamic association fetching
You may specify association fetching semantics at runtime using
setFetchMode().
This query will fetch both mate and kittens
by outer join. See for more information.
Example queries
The class org.hibernate.criterion.Example allows
you to construct a query criterion from a given instance.
Version properties, identifiers and associations are ignored. By default,
null valued properties are excluded.
You can adjust how the Example is applied.
You can even use examples to place criteria upon associated objects.
Projections, aggregation and grouping
The class org.hibernate.criterion.Projections is a
factory for Projection instances. We apply a
projection to a query by calling setProjection().
There is no explicit "group by" necessary in a criteria query. Certain
projection types are defined to be grouping projections,
which also appear in the SQL group by clause.
An alias may optionally be assigned to a projection, so that the projected value
may be referred to in restrictions or orderings. Here are two different ways to
do this:
The alias() and as() methods simply wrap a
projection instance in another, aliased, instance of Projection.
As a shortcut, you can assign an alias when you add the projection to a
projection list:
You can also use Property.forName() to express projections:
Detached queries and subqueries
The DetachedCriteria class lets you create a query outside the scope
of a session, and then later execute it using some arbitrary Session.
A DetachedCriteria may also be used to express a subquery. Criterion
instances involving subqueries may be obtained via Subqueries or
Property.
Even correlated subqueries are possible:
Queries by natural identifier
For most queries, including criteria queries, the query cache is not very efficient,
because query cache invalidation occurs too frequently. However, there is one special
kind of query where we can optimize the cache invalidation algorithm: lookups by a
constant natural key. In some applications, this kind of query occurs frequently.
The criteria API provides special provision for this use case.
First, you should map the natural key of your entity using
<natural-id>, and enable use of the second-level cache.
]]>
Note that this functionality is not intended for use with entities with
mutable natural keys.
Next, enable the Hibernate query cache.
Now, Restrictions.naturalId() allows us to make use of
the more efficient cache algorithm.