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
Copy file name to clipboardExpand all lines: documentation/src/main/asciidoc/introduction/Interacting.adoc
+74-32Lines changed: 74 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,7 @@ A persistence context—that is, a `Session` or `EntityManager`—absolutely pos
61
61
If you accidentally leak a session across threads, you will suffer.
62
62
====
63
63
64
-
.Container-managed peristence contexts
64
+
.Container-managed persistence contexts
65
65
****
66
66
In a container environment, the lifecycle of a persistence context scoped to the transaction will usually be managed for you.
67
67
****
@@ -279,7 +279,7 @@ But there's a way to set things up so that an operation will propagate to associ
279
279
[[cascade]]
280
280
=== Cascading persistence operations
281
281
282
-
It's quite often the case that the lifecycle of a _child_ entity is completely dependent on the lifeycle of some _parent_.
282
+
It's quite often the case that the lifecycle of a _child_ entity is completely dependent on the lifecycle of some _parent_.
283
283
This is especially common for many-to-one and one-to-one associations, though it's very rare for many-to-many associations.
284
284
285
285
For example, it's quite common to make an `Order` and all its ``Item``s persistent in the same transaction, or to delete a `Project` and its ``Files``s at once.
@@ -652,14 +652,14 @@ Using the JPA-standard APIs, this would be a `CriteriaBuilder`, and we get it fr
Do you find some of the code above a bit too verbose?
763
+
We do.
764
+
765
+
[[criteria-definition]]
766
+
=== A more comfortable way to write criteria queries
767
+
768
+
Actually, what makes the JPA criteria API less ergonomic that it should be is the need to call all operations of the `CriteriaBuilder` as instance methods, instead of having them as `static` functions.
769
+
The reason it works this way is that each JPA providor has its own implementation of `CriteriaBuilder`.
770
+
771
+
// [%unbreakable]
772
+
// [TIP]
773
+
// ====
774
+
Hibernate 6.3 introduces the helper class `CriteriaDefinition` to reduce the verbosity of criteria queries.
775
+
Our example looks like this:
776
+
777
+
[source,java]
778
+
----
779
+
CriteriaQuery<Book> query =
780
+
new CriteriaDefinition(entityManagerFactory, Book.class) {{
When all else fails, and sometimes even before that, we're left with the option of writing a query in SQL.
763
795
764
796
[[native-queries]]
@@ -859,7 +891,7 @@ For example, this:
859
891
List<Book> books =
860
892
session.createSelectionQuery("from Book where title like ?1 order by title")
861
893
.setParameter(1, titlePattern)
862
-
.setMaxResults(10)
894
+
.setMaxResults(MAX_RESULTS)
863
895
.getResultList();
864
896
----
865
897
@@ -870,22 +902,32 @@ is simpler than:
870
902
List<Book> books =
871
903
session.createSelectionQuery("from Book where title like ?1 order by title fetch first ?2 rows only")
872
904
.setParameter(1, titlePattern)
873
-
.setParameter(2, 10)
905
+
.setParameter(2, MAX_RESULTS)
906
+
.getResultList();
907
+
----
908
+
909
+
Hibernate's `SelectionQuery` has a slightly different way to paginate the query results:
910
+
911
+
[source,java]
912
+
----
913
+
List<Book> books =
914
+
session.createSelectionQuery("from Book where title like ?1 order by title")
915
+
.setParameter(1, titlePattern)
916
+
.setPage(Page.first(MAX_RESULTS))
874
917
.getResultList();
875
918
----
876
919
877
920
A closely-related issue is ordering.
878
921
It's quite common for pagination to be combined with the need to order query results by a field that's determined at runtime.
879
-
So, as an alternative to the HQL `order by` clause, Hibernate's `SelectionQuery` interface offers the ability to specify that the query results should be ordered by one or more fields of the entity type returned by the query:
922
+
So, as an alternative to the HQL `order by` clause, `SelectionQuery` offers the ability to specify that the query results should be ordered by one or more fields of the entity type returned by the query:
880
923
881
924
[source,java]
882
925
----
883
926
List<Book> books =
884
927
session.createSelectionQuery("from Book where title like ?1")
var resultList = session.createSelectionQuery(query).getResultList();
962
1003
for (var result: resultList) {
963
1004
String title = result.get(bookTitle);
@@ -1032,9 +1073,10 @@ Note that the code which executes the named query is not aware of whether the qu
1032
1073
1033
1074
It's nice to have our queries checked at startup time.
1034
1075
It's even better to have them checked at compile time.
1035
-
Back in <<generated-query-methods>>, we mentioned that the {query-validator}[Query Validator] can do that for us.
1036
-
In fact, the Query Validator will even check HQL query strings that occur as arguments to `createQuery()` and friends.
1037
-
So if we use the Query Validator, there's not much advantage to the use of named queries.
1076
+
Back in <<generated-query-methods>>, we mentioned that the Metamodel Generator can do that for us, and we presented that as a reason to use `@NamedQuery`.
1077
+
1078
+
But actually, Hibernate has a separate <<query-validator,Query Validator>> capable of performing compile-time validation of HQL query strings that occur as arguments to `createQuery()` and friends.
1079
+
If we use the Query Validator, there's not much advantage to the use of named queries.
0 commit comments