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/repositories/Pagination.adoc
+23-8Lines changed: 23 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -65,8 +65,8 @@ But since Hibernate Data Repositories already validates the content of the `@Ord
65
65
66
66
Dynamic sorting criteria are expressed using the types `Sort` and `Order`:
67
67
68
-
- an instance of `Sort` represents a single criterion for sorting query results, and
69
-
- an instance of `Order` packages multiple ``Sort``s together.
68
+
- an instance of https://jakarta.ee/specifications/data/1.0/apidocs/jakarta.data/jakarta/data/sort[`Sort`] represents a single criterion for sorting query results, and
69
+
- an instance of https://jakarta.ee/specifications/data/1.0/apidocs/jakarta.data/jakarta/data/order[`Order`] packages multiple ``Sort``ing criteria together.
70
70
71
71
A query method may accept an instance of `Sort`.
72
72
@@ -107,6 +107,18 @@ var books =
107
107
108
108
Dynamic sorting criteria may be combined with static criteria.
109
109
110
+
[TIP]
111
+
====
112
+
If the entity has entity supertypes, use a lower bounded wildcard in the repository method declaration, for example:
113
+
[source,java]
114
+
----
115
+
@Find
116
+
List<Book> books(@Pattern String title, Year yearPublished,
117
+
Order<? super Book> order);
118
+
----
119
+
This lets the caller sort by fields of the supertype.
120
+
====
121
+
110
122
[source,java]
111
123
----
112
124
@Find
@@ -120,13 +132,13 @@ We're not convinced this is very useful in practice.
120
132
[[limits]]
121
133
=== Limits
122
134
123
-
A `Limit` is the simplest way to express a subrange of query results.
135
+
A https://jakarta.ee/specifications/data/1.0/apidocs/jakarta.data/jakarta/data/limit[`Limit`] is the simplest way to express a subrange of query results.
124
136
It specifies:
125
137
126
138
- `maxResults`, the maximum number of results to be returned from the database server to the client, and,
127
-
- optionally, `startAt`, an offset from the very first result.
139
+
- optionally, `startAt`, the position of the first result to be returned to the client.
128
140
129
-
These values map directly the familiar `setMaxResults()` and `setFirstResults()` of the Jakarta Persistence `Query` interface.
141
+
These values map directly to the familiar `setMaxResults()` and `setFirstResult()` of the Jakarta Persistence `Query` interface.
130
142
131
143
[source,java]
132
144
----
@@ -142,12 +154,15 @@ var books =
142
154
Limit.of(MAX_RESULTS));
143
155
----
144
156
157
+
[CAUTION]
158
+
Whereas `Query.setFirstResult()` is an _offset_ of the first result to be returned, with `setFirstResult(0)` meaning "no offset", `Limit.startAt` numbers results from one, with `Limit.range(1, n)` meaning "at most _n_ results, starting from the first result".
159
+
145
160
A more sophisticated approach is provided by `PageRequest`.
146
161
147
162
[[offset-based-pagination]]
148
163
=== Offset-based pagination
149
164
150
-
A `PageRequest` is superficially similar to a `Limit`, except that it's specified in terms of:
165
+
A https://jakarta.ee/specifications/data/1.0/apidocs/jakarta.data/jakarta/data/page/pagerequest[`PageRequest`] is superficially similar to a `Limit`, except that it's specified in terms of:
151
166
152
167
- a page `size`, and
153
168
- a numbered `page`.
@@ -176,7 +191,7 @@ The easiest way to be sure that you have a well-defined total order is to specif
176
191
For this reason, we specified `@OrderBy("isbn")` in the previous example.
177
192
====
178
193
179
-
However, a repository method which accepts a `PageRequest` may return a `Page` instead of a `List`, making it easier to implement pagination.
194
+
However, a repository method which accepts a `PageRequest` may return a https://jakarta.ee/specifications/data/1.0/apidocs/jakarta.data/jakarta/data/page/page[`Page`] of results instead of a `List`, making it easier to implement pagination.
180
195
181
196
[source,java]
182
197
----
@@ -238,7 +253,7 @@ For key-based pagination, it's _essential_ that the query has a total order.
238
253
====
239
254
240
255
From our point of view as users of Jakarta Data, key-based pagination works almost exactly like offset-based pagination.
241
-
The difference is that we must declare our repository method to return `CursoredPage`.
256
+
The difference is that we must declare our repository method to return https://jakarta.ee/specifications/data/1.0/apidocs/jakarta.data/jakarta/data/page/cursoredpage[`CursoredPage`].
0 commit comments