Skip to content

Commit dc6f7f0

Browse files
committed
Refine Fluent Query reference documentation.
Closes #3419
1 parent 12d78b7 commit dc6f7f0

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

src/main/antora/modules/ROOT/pages/query-by-example.adoc

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,54 @@ The following table describes the scope of the various `ExampleMatcher` settings
200200
[[query-by-example.fluent]]
201201
== Fluent API
202202

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)`.
211204

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`.
212208

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+
====
214234
[source,java]
215235
----
216-
Optional<Person> match = repository.findBy(example,
217-
q -> q
218-
.sortBy(Sort.by("lastname").descending())
219-
.first()
236+
Page<CustomerProjection> page = repository.findBy(example,
237+
q -> q.as(CustomerProjection.class)
238+
.page(PageRequest.of(0, 20, Sort.by("lastname")))
220239
);
221240
----
241+
====
242+
243+
.Use the fluent API to get the last of potentially many results, ordered by `lastname`
244+
====
245+
[source,java]
246+
----
247+
Optional<Customer> match = repository.findBy(example,
248+
q -> q.sortBy(Sort.by("lastname").descending())
249+
.first()
250+
);
251+
----
252+
====
253+

0 commit comments

Comments
 (0)