diff --git a/documentation/src/main/docbook/userGuide/en-US/Hibernate_User_Guide.xml b/documentation/src/main/docbook/userGuide/en-US/Hibernate_User_Guide.xml
index 9fd4841518..3774f55dce 100644
--- a/documentation/src/main/docbook/userGuide/en-US/Hibernate_User_Guide.xml
+++ b/documentation/src/main/docbook/userGuide/en-US/Hibernate_User_Guide.xml
@@ -58,15 +58,18 @@
+
+
+
-
+
+
+
diff --git a/documentation/src/main/docbook/userGuide/en-US/appendices/Appendix_LegacyBootstrap.xml b/documentation/src/main/docbook/userGuide/en-US/appendices/Legacy_Bootstrap.xml
similarity index 100%
rename from documentation/src/main/docbook/userGuide/en-US/appendices/Appendix_LegacyBootstrap.xml
rename to documentation/src/main/docbook/userGuide/en-US/appendices/Legacy_Bootstrap.xml
diff --git a/documentation/src/main/docbook/userGuide/en-US/appendices/Legacy_Criteria.xml b/documentation/src/main/docbook/userGuide/en-US/appendices/Legacy_Criteria.xml
new file mode 100644
index 0000000000..a4e472401d
--- /dev/null
+++ b/documentation/src/main/docbook/userGuide/en-US/appendices/Legacy_Criteria.xml
@@ -0,0 +1,549 @@
+
+
+
+
+
+ Legacy Hibernate Criteria Queries
+
+
+
+
+ This appendix covers the legacy Hibernate org.hibernate.Criteria API, which
+ should be considered deprecated. New development should focus on the JPA
+ javax.persistence.criteria.CriteriaQuery API. Eventually,
+ Hibernate-specific criteria features will be ported as extensions to the JPA
+ javax.persistence.criteria.CriteriaQuery. For details on the JPA APIs, see
+ .
+
+
+ This information is copied as-is from the older Hibernate documentation.
+
+
+
+
+ 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 can be grouped logically.
+
+
+
+
+
+
+
+ There are a range of built-in criterion types (Restrictions
+ subclasses). One of the most useful allows you to specify SQL directly.
+
+
+
+
+
+ The {alias} placeholder will be replaced by the row alias
+ of the queried entity.
+
+
+
+ You can also obtain a criterion from a
+ Property instance. You can create a Property
+ by calling Property.forName():
+
+
+
+
+
+
+
+ Ordering the results
+
+
+ You can order the results using org.hibernate.criterion.Order.
+
+
+
+
+
+
+
+
+
+ Associations
+
+
+ By navigating
+ associations using createCriteria() you can specify constraints upon related entities:
+
+
+
+
+
+ The second createCriteria() returns a new
+ instance of Criteria that refers to the elements of
+ the kittens collection.
+
+
+
+ There is also an alternate form that is useful in certain circumstances:
+
+
+
+
+
+ (createAlias() does not create a new instance of
+ Criteria.)
+
+
+
+ The kittens collections held by the Cat instances
+ returned by the previous two queries are not pre-filtered
+ by the criteria. If you want to retrieve just the kittens that match the
+ criteria, you must use a ResultTransformer.
+
+
+
+
+
+ Additionally you may manipulate the result set using a left outer join:
+
+
+
+
+ This will return all of the Cats with a mate whose name starts with "good"
+ ordered by their mate's age, and all cats who do not have a mate.
+ This is useful when there is a need to order or limit in the database
+ prior to returning complex/large result sets, and removes many instances where
+ multiple queries would have to be performed and the results unioned
+ by java in memory.
+
+
+ Without this feature, first all of the cats without a mate would need to be loaded in one query.
+
+
+ A second query would need to retreive the cats with mates who's name started with "good" sorted by the mates age.
+
+
+ Thirdly, in memory; the lists would need to be joined manually.
+
+
+
+
+ Dynamic association fetching
+
+
+ You can specify association fetching semantics at runtime using
+ setFetchMode().
+
+
+
+
+
+ This query will fetch both mate and kittens
+ by outer join.
+
+
+
+
+
+ Components
+
+
+ To add a restriction against a property of an embedded component, the component property
+ name should be prepended to the property name when creating the Restriction.
+ The criteria object should be created on the owning entity, and cannot be created on the component
+ itself. For example, suppose the Cat has a component property fullName
+ with sub-properties firstName and lastName:
+
+
+
+
+
+
+ Note: this does not apply when querying collections of components, for that see below
+
+
+
+
+
+
+ Collections
+
+ When using criteria against collections, there are two distinct cases. One is if
+ the collection contains entities (eg. <one-to-many/>
+ or <many-to-many/>) or components
+ (<composite-element/> ),
+ and the second is if the collection contains scalar values
+ (<element/>).
+ In the first case, the syntax is as given above in the section
+ where we restrict the kittens
+ collection. Essentially we create a Criteria object against the collection
+ property and restrict the entity or component properties using that instance.
+
+
+ For queryng a collection of basic values, we still create the Criteria
+ object against the collection, but to reference the value, we use the special property
+ "elements". For an indexed collection, we can also reference the index property using
+ the special property "indices".
+
+
+
+
+
+
+ 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. You can 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 can be assigned to a projection so that the projected value
+ can 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 allows you to create a query outside the scope
+ of a session and then execute it using an arbitrary Session.
+
+
+
+
+
+ A DetachedCriteria can also be used to express a subquery. Criterion
+ instances involving subqueries can be obtained via Subqueries or
+ Property.
+
+
+
+
+
+
+
+ Correlated subqueries are also possible:
+
+
+
+
+
+ Example of multi-column restriction based on a subquery:
+
+
+
+
+
+
+
+
+
+ Queries by natural identifier
+
+
+ For most queries, including criteria queries, the query cache is not efficient
+ because query cache invalidation occurs too frequently. However, there is a special
+ kind of query where you 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, map the natural key of your entity using
+ <natural-id> and enable use of the second-level cache.
+
+
+
+
+
+
+
+
+
+
+
+
+]]>
+
+
+ This functionality is not intended for use with entities with
+ mutable natural keys.
+
+
+
+ Once you have enabled the Hibernate query cache,
+ the Restrictions.naturalId() allows you to make use of
+ the more efficient cache algorithm.
+
+
+
+
+
+
+
diff --git a/documentation/src/main/docbook/userGuide/en-US/chapters/fetching/Fetching.xml b/documentation/src/main/docbook/userGuide/en-US/chapters/fetching/Fetching.xml
index 868312037a..c32a2157ce 100644
--- a/documentation/src/main/docbook/userGuide/en-US/chapters/fetching/Fetching.xml
+++ b/documentation/src/main/docbook/userGuide/en-US/chapters/fetching/Fetching.xml
@@ -11,6 +11,10 @@
xmlns:xi="http://www.w3.org/2001/XInclude">
Fetching
+
+
+
+
Fetching, essentially, is the process of grabbing data from the database and making it available to the
application. Tuning how an application does fetching is one of the biggest factors in determining how an
@@ -76,6 +80,11 @@
+
+
+ Starting in Hibernate 4.2 (JPA 2.1) you can also use JPA EntityGraphs.
+
+
@@ -143,7 +152,7 @@
The Hibernate recommendation is to statically mark all associations lazy and to use dynamic fetching
strategies for eagerness. This is unfortunately at odds with the JPA specification which defines that
all one-to-one and many-to-one associations should be eagerly fetched by default. Hibernate, as a JPA
- provider honors that default.
+ provider, honors that default.
@@ -195,8 +204,8 @@
In this example we have an Employee and their Projects loaded in a single query shown both as an HQL
- query and a JPA Criteria query. In both cases, this resolves to exactly one database query to get
- all that information.
+ query and a JPA Criteria query. In both cases, this resolves to exactly one database query to get all
+ that information.
@@ -226,7 +235,4 @@
-
-
-
\ No newline at end of file
diff --git a/documentation/src/main/docbook/userGuide/en-US/chapters/query-criteria/Criteria.xml b/documentation/src/main/docbook/userGuide/en-US/chapters/query-criteria/Criteria.xml
new file mode 100644
index 0000000000..184c73d2be
--- /dev/null
+++ b/documentation/src/main/docbook/userGuide/en-US/chapters/query-criteria/Criteria.xml
@@ -0,0 +1,413 @@
+
+
+
+
+
+ Criteria
+
+
+ Criteria queries offer a type-safe alternative to HQL, JPQL and native-sql queries.
+
+
+
+
+ Hibernate offers an older, legacy org.hibernate.Criteria API which should be
+ considered deprecated. No feature development will target those APIs. Eventually, Hibernate-specific
+ criteria features will be ported as extensions to the JPA
+ javax.persistence.criteria.CriteriaQuery. For details on the
+ org.hibernate.Criteria API, see .
+
+
+ This chapter will focus on the JPA APIs for declaring type-safe criteria queries.
+
+
+
+
+
+ Criteria queries are a programmatic, type-safe way to express a query. They are type-safe in terms of
+ using interfaces and classes to represent various structural parts of a query such as the query itself,
+ or the select clause, or an order-by, etc. They can also be type-safe in terms of referencing attributes
+ as we will see in a bit. Users of the older Hibernate org.hibernate.Criteria
+ query API will recognize the general approach, though we believe the JPA API to be superior
+ as it represents a clean look at the lessons learned from that API.
+
+
+
+ Criteria queries are essentially an object graph, where each part of the graph represents an increasing
+ (as we navigate down this graph) more atomic part of query. The first step in performing a criteria query
+ is building this graph. The javax.persistence.criteria.CriteriaBuilder
+ interface is the first thing with which you need to become acquainted to begin using criteria queries. Its
+ role is that of a factory for all the individual pieces of the criteria. You obtain a
+ javax.persistence.criteria.CriteriaBuilder instance by calling the
+ getCriteriaBuilder method of either
+ javax.persistence.EntityManagerFactory or
+ javax.persistence.EntityManager.
+
+
+
+ The next step is to obtain a javax.persistence.criteria.CriteriaQuery. This
+ is accomplished using one of the 3 methods on
+ javax.persistence.criteria.CriteriaBuilder for this purpose:
+
+
+
+
+
+ Each serves a different purpose depending on the expected type of the query results.
+
+
+
+
+ Chapter 6 Criteria API of the JPA Specification
+ already contains a decent amount of reference material pertaining to the various parts of a
+ criteria query. So rather than duplicate all that content here, lets instead look at some of
+ the more widely anticipated usages of the API.
+
+
+
+
+ Typed criteria queries
+
+
+ The type of the criteria query (aka the ]]>) indicates the expected types in the query
+ result. This might be an entity, an Integer, or any other object.
+
+
+
+ Selecting an entity
+
+
+ This is probably the most common form of query. The application wants to select entity instances.
+
+
+
+ Selecting the root entity
+
+
+
+
+ The example uses createQuery passing in the Person
+ class reference as the results of the query will be Person objects.
+
+
+
+
+ The call to the CriteriaQuery.select method in this example is
+ unnecessary because personRoot will be the implied selection since we
+ have only a single query root. It was done here only for completeness of an example.
+
+
+ The Person_.eyeColor reference is an example of the static form of JPA
+ metamodel reference. We will use that form exclusively in this chapter. See
+ the documentation for the Hibernate JPA Metamodel Generator for additional details on
+ the JPA static metamodel.
+
+
+
+
+
+ Selecting an expression
+
+
+ The simplest form of selecting an expression is selecting a particular attribute from an entity.
+ But this expression might also represent an aggregation, a mathematical operation, etc.
+
+
+
+ Selecting an attribute
+
+
+
+
+ In this example, the query is typed as java.lang.Integer because that
+ is the anticipated type of the results (the type of the Person#age attribute
+ is java.lang.Integer). Because a query might contain multiple references to
+ the Person entity, attribute references always need to be qualified. This is accomplished by the
+ Root#get method call.
+
+
+
+
+
+ Selecting multiple values
+
+
+ There are actually a few different ways to select multiple values using criteria queries. We
+ will explore 2 options here, but an alternative recommended approach is to use tuples as described in
+ . Or consider a wrapper query; see
+ for details.
+
+
+
+ Selecting an array
+
+
+
+
+ Technically this is classified as a typed query, but you can see from handling the results that
+ this is sort of misleading. Anyway, the expected result type here is an array.
+
+
+
+ The example then uses the array method of
+ javax.persistence.criteria.CriteriaBuilder which explicitly
+ combines individual selections into a
+ javax.persistence.criteria.CompoundSelection.
+
+
+
+ Selecting an array (2)
+
+
+
+
+ Just as we saw in we have a typed criteria
+ query returning an Object array. Both queries are functionally equivalent. This second example
+ uses the multiselect method which behaves slightly differently based on
+ the type given when the criteria query was first built, but in this case it says to select and
+ return an Object[].
+
+
+
+
+ Selecting a wrapper
+
+ Another alternative to is to instead
+ select an object that will wrap the multiple values. Going back to the example
+ query there, rather than returning an array of [Person#id, Person#age]
+ instead declare a class that holds these values and instead return that.
+
+
+
+ Selecting an wrapper
+
+
+
+
+ First we see the simple definition of the wrapper object we will be using to wrap our result
+ values. Specifically notice the constructor and its argument types. Since we will be returning
+ PersonWrapper objects, we use PersonWrapper as the
+ type of our criteria query.
+
+
+
+ This example illustrates the use of the
+ javax.persistence.criteria.CriteriaBuilder method
+ construct which is used to build a wrapper expression. For every row in the
+ result we are saying we would like a PersonWrapper instantiated with
+ the remaining arguments by the matching constructor. This wrapper expression is then passed as
+ the select.
+
+
+
+
+
+ Tuple criteria queries
+
+
+ A better approach to is to use either a
+ wrapper (which we just saw in ) or using the
+ javax.persistence.Tuple contract.
+
+
+
+ Selecting a tuple
+
+
+
+
+ This example illustrates accessing the query results through the
+ javax.persistence.Tuple interface. The example uses the explicit
+ createTupleQuery of
+ javax.persistence.criteria.CriteriaBuilder. An alternate approach
+ is to use createQuery passing Tuple.class.
+
+
+
+ Again we see the use of the multiselect method, just like in
+ . The difference here is that the type of the
+ javax.persistence.criteria.CriteriaQuery was defined as
+ javax.persistence.Tuple so the compound selections in this case are
+ interpreted to be the tuple elements.
+
+
+
+ The javax.persistence.Tuple contract provides 3 forms of access to
+ the underlying elements:
+
+
+
+
+ typed
+
+
+ The example illustrates this form of access
+ in the tuple.get( idPath ) and tuple.get( agePath ) calls.
+ This allows typed access to the underlying tuple values based on the
+ javax.persistence.TupleElement expressions used to build
+ the criteria.
+
+
+
+
+ positional
+
+
+ Allows access to the underlying tuple values based on the position. The simple
+ Object get(int position) form is very similar to the access
+ illustrated in and
+ . The
+ X get(int position, Class type]]> form
+ allows typed positional access, but based on the explicitly supplied type which the tuple
+ value must be type-assignable to.
+
+
+
+
+ aliased
+
+
+ Allows access to the underlying tuple values based an (optionally) assigned alias. The
+ example query did not apply an alias. An alias would be applied via the
+ alias method on
+ javax.persistence.criteria.Selection. Just like
+ positional access, there is both a typed
+ (Object get(String alias)) and an untyped
+ ( X get(String alias, Class type]]> form.
+
+
+
+
+
+
+
+ FROM clause
+
+
+ JPA Specification, section 6.5.2 Query Roots, pg 262
+
+
+ A CriteriaQuery object defines a query over one or more entity, embeddable, or basic abstract
+ schema types. The root objects of the query are entities, from which the other types are reached
+ by navigation.
+
+
+
+
+
+ All the individual parts of the FROM clause (roots, joins, paths) implement the
+ javax.persistence.criteria.From interface.
+
+
+
+
+ Roots
+
+
+ Roots define the basis from which all joins, paths and attributes are available in the query.
+ A root is always an entity type. Roots are defined and added to the criteria by the overloaded
+ from methods on
+ javax.persistence.criteria.CriteriaQuery:
+
+
+
+
+
+ Adding a root
+
+
+
+
+ Criteria queries may define multiple roots, the effect of which is to create a cartesian
+ product between the newly added root and the others. Here is an example matching all single
+ men and all single women:
+
+
+
+ Adding multiple roots
+
+
+
+
+
+ Joins
+
+
+ Joins allow navigation from other javax.persistence.criteria.From
+ to either association or embedded attributes. Joins are created by the numerous overloaded
+ join methods of the
+ javax.persistence.criteria.From interface
+
+
+
+ Example with Embedded and ManyToOne
+
+
+
+
+ Example with Collections
+
+
+
+
+
+ Fetches
+
+
+ Just like in HQL and JPQL, criteria queries can specify that associated data be fetched along
+ with the owner. Fetches are created by the numerous overloaded fetch
+ methods of the javax.persistence.criteria.From interface.
+
+
+
+ Example with Embedded and ManyToOne
+
+
+
+
+
+ Technically speaking, embedded attributes are always fetched with their owner. However in
+ order to define the fetching of Address#country we needed a
+ javax.persistence.criteria.Fetch for its parent path.
+
+
+
+
+ Example with Collections
+
+
+
+
+
+
+ Path expressions
+
+
+ Roots, joins and fetches are themselves paths as well.
+
+
+
+
+
+ Using parameters
+
+
+ Using parameters
+
+
+
+
+ Use the parameter method of
+ javax.persistence.criteria.CriteriaBuilder to obtain a parameter
+ reference. Then use the parameter reference to bind the parameter value to the
+ javax.persistence.Query
+
+
+
+
diff --git a/documentation/src/main/docbook/userGuide/en-US/chapters/query-criteria/extras/CriteriaBuilder_query_creation_snippet.java b/documentation/src/main/docbook/userGuide/en-US/chapters/query-criteria/extras/CriteriaBuilder_query_creation_snippet.java
new file mode 100644
index 0000000000..a4dc3ef01b
--- /dev/null
+++ b/documentation/src/main/docbook/userGuide/en-US/chapters/query-criteria/extras/CriteriaBuilder_query_creation_snippet.java
@@ -0,0 +1,3 @@
+ CriteriaQuery createQuery(Class resultClass);
+CriteriaQuery createTupleQuery();
+CriteriaQuery