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/Advanced.adoc
+17-36Lines changed: 17 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,15 +64,13 @@ class User {
64
64
}
65
65
----
66
66
67
-
Here, as usual, `example_.BY_REGION` is generated by the Metamodel Generator, and is just a constant with the value `"ByRegion"`.
68
-
69
67
If the `@Filter` annotation does not explicitly specify a restriction, the default restriction given by the `@FilterDef` will be applied to the entity.
70
68
But an entity is free to override the default condition.
@@ -99,7 +97,7 @@ You should do this right at the _start_ of the session.
99
97
[source,java]
100
98
----
101
99
sessionFactory.inTransaction(session -> {
102
-
session.enableFilter(example_.FILTER_BY_REGION)
100
+
session.enableFilter("ByRegion")
103
101
.setParameter("region", "es")
104
102
.validate();
105
103
@@ -257,7 +255,7 @@ Native SQL queries are _not_ automatically filtered by tenant id; you'll have to
257
255
258
256
[TIP]
259
257
====
260
-
If you only need to filter rows by a static condition with no parameters, `@SQLRestriction` is a much simpler way to do that.
258
+
If you only need to filter rows by a static condition with no parameters, `@Where` is a much simpler way to do that.
261
259
====
262
260
263
261
[[custom-sql]]
@@ -648,7 +646,6 @@ These annotations allow us to specify how the elements of a collection should be
648
646
| Annotation | Purpose | JPA-standard
649
647
650
648
| `@OrderBy` | Specifies a fragment of JPQL used to order the collection | ✔
651
-
| `@SQLOrder` | Specifies a fragment of SQL used to order the collection | ✖
652
649
|===
653
650
654
651
On the other hand, the following annotation specify how a collection should be sorted in memory, and are used for collections of type `SortedSet` or `SortedMap`:
@@ -902,7 +899,7 @@ class Book { ... }
902
899
903
900
Note that even though we've placed this annotation on the `Book` entity, a fetch profile—unlike an entity graph—isn't "rooted" at any particular entity.
904
901
905
-
We may specify association fetching strategies using the `fetchOverrides` member of the `@FetchProfile` annotation, but frankly it looks so messy that we're embarrassed to show it to you here.
902
+
We may specify association fetching strategies using the `fetchOverrides` member of the `@FetchProfile` annotation.
906
903
907
904
[NOTE]
908
905
====
@@ -914,17 +911,19 @@ A better way is to annotate an association with the fetch profiles it should be
Book eagerBook = session.find(Book.class, bookId);
991
987
----
992
988
@@ -997,18 +993,3 @@ But Hibernate offers alternatives that we think are more compelling most of the
997
993
998
994
The one and only advantage unique to fetch profiles is that they let us very selectively request subselect fetching.
999
995
We can't do that with entity graphs, and we can't do it with HQL.
1000
-
1001
-
[TIP]
1002
-
====
1003
-
There's a special built-in fetch profile named `org.hibernate.defaultProfile` which is defined as the profile with `@FetchProfileOverride(mode=JOIN)` applied to every eager `@ManyToOne` or `@OneToOne` association.
Then ``outer join``s for such associations will _automatically_ be added to every HQL or criteria query.
1012
-
This is nice if you can't be bothered typing out those ``join fetch``es explicitly.
1013
-
And in principle it even helps partially mitigate the <<lazy-problem,problem>> of JPA having specified the wrong default for the `fetch` member of `@ManyToOne`.
When all else fails, and sometimes even before that, we're left with the option of writing a query in SQL.
760
746
761
747
[[native-queries]]
@@ -878,24 +864,12 @@ Hibernate's `SelectionQuery` has a slightly different way to paginate the query
878
864
List<Book> books =
879
865
session.createSelectionQuery("from Book where title like ?1 order by title")
880
866
.setParameter(1, titlePattern)
881
-
.setPage(Page.first(MAX_RESULTS))
882
-
.getResultList();
883
-
----
884
-
885
-
A closely-related issue is ordering.
886
-
It's quite common for pagination to be combined with the need to order query results by a field that's determined at runtime.
887
-
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:
888
-
889
-
[source,java]
890
-
----
891
-
List<Book> books =
892
-
session.createSelectionQuery("from Book where title like ?1")
Here, `BookQueries_.QUERY_10_BOOKS_BY_TITLE` is a constant with value `"10BooksByTitle"`, generated by the Metamodel Generator.
1030
-
1031
1003
Note that the code which executes the named query is not aware of whether the query was written in HQL or in native SQL, making it slightly easier to change and optimize the query later.
Copy file name to clipboardExpand all lines: documentation/src/main/asciidoc/introduction/Introduction.adoc
-2Lines changed: 0 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -500,13 +500,11 @@ This is an example of a _query method_, a function which accepts arguments to th
500
500
And that's all it does; it doesn't orchestrate additional program logic, and it doesn't perform transaction or session management.
501
501
502
502
It's even better to specify the query string using the `@NamedQuery` annotation, so that Hibernate can validate the query it at startup time, that is, when the `SessionFactory` is created, instead of when the query is first executed.
503
-
Indeed, since we included the <<metamodel-generator,Metamodel Generator>> in our <<build-gradle,Gradle build>>, the query can even be validated at _compile time_.
504
503
505
504
We need a place to put the annotation, so lets move our query method to a new class:
506
505
507
506
[source,java]
508
507
----
509
-
@CheckHQL // validate named queries at compile time
510
508
@NamedQuery(name="findBooksByTitle",
511
509
query="from Book where title like :title order by title")
0 commit comments