You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- The link:{doc-javadoc-url}org/hibernate/query/restriction/Restriction.html[`Restriction`] interface has static methods for constructing a variety of different kinds of restriction in a completely typesafe way.
1071
1082
- Similarly, the link:{doc-javadoc-url}org/hibernate/query/Order.html[`Order`] class has a variety of static methods for constructing different kinds of ordering criteria.
1072
1083
1073
-
We need the following methods of `SelectionQuery`:
1084
+
We need the following methods of link:{doc-javadoc-url}org/hibernate/query/programmatic/SelectionSpecification.html[`SelectionSpecification`]:
1074
1085
1075
1086
.Methods for query restriction and ordering
1076
-
[%breakable,cols="30,~,^15"]
1087
+
[%breakable,cols="20,~]
1077
1088
|===
1078
-
| Method name | Purpose | JPA-standard
1089
+
| Method name | Purpose
1079
1090
1080
-
| `addRestriction()` | Add a restriction on the query results | ✖
1081
-
| `setOrder()` | Specify how the query results should be ordered | ✖
1091
+
| `addRestriction()` | Add a restriction on the query results
1092
+
| `setOrder()`, `addOrder()` | Specify how the query results should be ordered
1093
+
| `addFetching()` | Add a fetched association
1094
+
| `addAugmentation()` | Add a custom function which directly manipulates the query
1082
1095
|===
1083
1096
1084
-
Unfortunately, `Restriction` and `Order` can't be used with JPA's `TypedQuery` interface, and JPA has no built-in alternative, so if we're using `EntityManager`, we need to call `unwrap()` to obtain a `SelectionQuery`.
1085
-
1086
1097
Alternatively, `Restriction` and `Order` can be used with <<paging-and-ordering,generated query or finder methods>>, and even with link:{doc-data-repositories-url}[Jakarta Data repositories].
1087
1098
1088
1099
The interface link:{doc-javadoc-url}org/hibernate/query/restriction/Path.html[`Path`] may be used to express restrictions on fields of an embedded or associated entity class.
However, we emphasize that this API shines in cases where complex manipulations are _not_ required.
1145
+
1146
+
[NOTE]
1147
+
====
1148
+
`SelectionSpecification` (similar to its friend `MutationSpecification`) may be used in cases where a query returns a single "root" entity, possibly with some fetched associations.
1149
+
It is not useful in cases where a query should return multiple entities, a projection of entity fields, or an aggregation.
1150
+
For such cases, the full Criteria API is appropriate.
1151
+
====
1152
+
1099
1153
Programmatic restrictions, and especially programmatic ordering, are often used together with pagination.
Hibernate offers an API for creating a representation of a query, adjusting that representation programmatically, and then creating an executable form of the query. The idea is similar in concept to <<chapters/query/criteria/Criteria.adoc#criteria,criteria queries>>, but focused on ease-of-use and less verbosity.
6
+
7
+
There is support for both <<SelectionSpecification,selection>> and <<MutationSpecification,mutation>> queries via the `SelectionSpecification` and `MutationSpecification` contracts, respectively. These can be obtained from both `Session` and `StatelessSession`.
8
+
9
+
[NOTE]
10
+
====
11
+
These APIs are new in 7.0 and considered incubating.
12
+
====
13
+
14
+
[[SelectionSpecification]]
15
+
=== SelectionSpecification
16
+
17
+
A `SelectionSpecification` allows to iteratively build a query from a "base", adjust the query by adding sorting and restrictions and finally creating an executable <<hql-SelectionQuery,`SelectionQuery`>>. We can use HQL as the base -
0 commit comments