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: src/main/antora/modules/ROOT/pages/query-by-example.adoc
+45-13Lines changed: 45 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -200,22 +200,54 @@ The following table describes the scope of the various `ExampleMatcher` settings
200
200
[[query-by-example.fluent]]
201
201
== Fluent API
202
202
203
-
`QueryByExampleExecutor` offers one more method, which we did not mention so far: `<S extends T, R> R findBy(Example<S> example, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction)`.
204
-
As with other methods, it executes a query derived from an `Example`.
205
-
However, with the second argument, you can control aspects of that execution that you cannot dynamically control otherwise.
206
-
You do so by invoking the various methods of the `FetchableFluentQuery` in the second argument.
207
-
`sortBy` lets you specify an ordering for your result.
208
-
`as` lets you specify the type to which you want the result to be transformed.
209
-
`project` limits the queried attributes.
210
-
`first`, `firstValue`, `one`, `oneValue`, `all`, `page`, `slice`, `stream`, `count`, and `exists` define what kind of result you get and how the query behaves when more than the expected number of results are available.
203
+
`QueryByExampleExecutor` defines fluent query methods for flexible execution of queries based on `Example` instances through `findBy(Example<S> example, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction)`.
211
204
205
+
As with other methods, it executes a query derived from a `Example`.
206
+
However, the query function allows you to take control over aspects of query execution that you cannot dynamically control otherwise.
207
+
You do so by invoking the various intermediate and terminal methods of `FetchableFluentQuery`.
212
208
213
-
.Use the fluent API to get the last of potentially many results, ordered by lastname.
209
+
**Intermediate methods**
210
+
211
+
* `sortBy`: Apply an ordering for your result.
212
+
Repeated method calls append each `Sort` (note that `page(Pageable)` using a sorted `Pageable` overrides any previous sort order).
213
+
* `limit`: Limit the result count.
214
+
* `as`: Specify the type to be read or projected to.
215
+
* `project`: Limit the queries properties.
216
+
217
+
**Terminal methods**
218
+
219
+
* `first`, `firstValue`: Return the first value. `first` returns an `Optional<T>` or `Optional.empty()` if the query did not yield any result. `firstValue` is its nullable variant without the need to use `Optional`.
220
+
* `one`, `oneValue`: Return the one value. `one` returns an `Optional<T>` or `Optional.empty()` if the query did not yield any result. `oneValue` is its nullable variant without the need to use `Optional`.
221
+
Throws `IncorrectResultSizeDataAccessException` if more than one match found.
222
+
* `all`: Return all results as a `List<T>`.
223
+
* `page(Pageable)`: Return all results as a `Page<T>`.
224
+
* `slice(Pageable)`: Return all results as a `Slice<T>`.
225
+
* `scroll(ScrollPosition)`: Use scrolling (offset, keyset) to retrieve results as a `Window<T>`.
226
+
* `stream()`: Return a `Stream<T>` to process results lazily.
227
+
The stream is stateful and must be closed after use.
228
+
* `count` and `exists`: Return the count of matching entities or whether any match exists.
229
+
230
+
NOTE: Intermediate and terminal methods must be invoked within the query function.
231
+
232
+
.Use the fluent API to get a projected `Page`, ordered by `lastname`
233
+
====
214
234
[source,java]
215
235
----
216
-
Optional<Person> match = repository.findBy(example,
0 commit comments