Skip to content

Commit 7e96c56

Browse files
committed
DATACMNS-1833 - Revise documentation for query method keywords.
Original pull request: #477.
1 parent c4d2bd5 commit 7e96c56

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

src/main/asciidoc/repositories.adoc

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,13 @@ The following strategies are available for the repository infrastructure to reso
342342
[[repositories.query-methods.query-creation]]
343343
=== Query Creation
344344

345-
The query builder mechanism built into the Spring Data repository infrastructure is useful for building constraining queries over entities of the repository. The mechanism strips the `find…By`, `read…By`, `query…By`, `count…By`, and `get…By` prefixes from the method and starts parsing the rest of it. The introducing clause can contain further expressions, such as a `Distinct` to set a distinct flag on the query to be created. However, the first `By` acts as a delimiter to indicate the start of the actual criteria. At a very basic level, you can define conditions on entity properties and concatenate them with `And` and `Or`. The following example shows how to create a number of queries:
345+
The query builder mechanism built into the Spring Data repository infrastructure is useful for building constraining queries over entities of the repository.
346+
347+
The following example shows how to create a number of queries:
346348

347349
.Query creation from method names
348350
====
349-
[source, java]
351+
[source,java]
350352
----
351353
interface PersonRepository extends Repository<Person, Long> {
352354
@@ -368,13 +370,28 @@ interface PersonRepository extends Repository<Person, Long> {
368370
----
369371
====
370372

371-
The actual result of parsing the method depends on the persistence store for which you create the query. However, there are some general things to notice:
373+
Parsing query method names is divided into subject and predicate.
374+
The first part (`find…By`, `exists…By`) defines the subject of the query, the second part forms the predicate.
375+
The introducing clause (subject) can contain further expressions.
376+
Any text between `find` (or other introducing keywords) and `By` is considered to be descriptive unless using one of the result-limiting keywords such as a `Distinct` to set a distinct flag on the query to be created or <<repositories.limit-query-result,`Top`/`First` to limit query results>>.
377+
378+
The appendix contains the <<appendix.query.method.subject,full list of query method subject keywords>> and <<appendix.query.method.predicate,query method predicate keywords including sorting and letter-casing modifiers>>.
379+
However, the first `By` acts as a delimiter to indicate the start of the actual criteria predicate.
380+
At a very basic level, you can define conditions on entity properties and concatenate them with `And` and `Or`.
381+
382+
The actual result of parsing the method depends on the persistence store for which you create the query.
383+
However, there are some general things to notice:
372384

373-
- The expressions are usually property traversals combined with operators that can be concatenated. You can combine property expressions with `AND` and `OR`. You also get support for operators such as `Between`, `LessThan`, `GreaterThan`, and `Like` for the property expressions. The supported operators can vary by datastore, so consult the appropriate part of your reference documentation.
385+
- The expressions are usually property traversals combined with operators that can be concatenated.
386+
You can combine property expressions with `AND` and `OR`.
387+
You also get support for operators such as `Between`, `LessThan`, `GreaterThan`, and `Like` for the property expressions.
388+
The supported operators can vary by datastore, so consult the appropriate part of your reference documentation.
374389

375-
- The method parser supports setting an `IgnoreCase` flag for individual properties (for example, `findByLastnameIgnoreCase(…)`) or for all properties of a type that supports ignoring case (usually `String` instances -- for example, `findByLastnameAndFirstnameAllIgnoreCase(…)`). Whether ignoring cases is supported may vary by store, so consult the relevant sections in the reference documentation for the store-specific query method.
390+
- The method parser supports setting an `IgnoreCase` flag for individual properties (for example, `findByLastnameIgnoreCase(…)`) or for all properties of a type that supports ignoring case (usually `String` instances -- for example, `findByLastnameAndFirstnameAllIgnoreCase(…)`).
391+
Whether ignoring cases is supported may vary by store, so consult the relevant sections in the reference documentation for the store-specific query method.
376392

377-
- You can apply static ordering by appending an `OrderBy` clause to the query method that references a property and by providing a sorting direction (`Asc` or `Desc`). To create a query method that supports dynamic sorting, see "`<<repositories.special-parameters>>`".
393+
- You can apply static ordering by appending an `OrderBy` clause to the query method that references a property and by providing a sorting direction (`Asc` or `Desc`).
394+
To create a query method that supports dynamic sorting, see "`<<repositories.special-parameters>>`".
378395

379396
[[repositories.query-methods.query-property-expressions]]
380397
=== Property Expressions
@@ -496,7 +513,8 @@ List<User> findTop10ByLastname(String lastname, Pageable pageable);
496513
----
497514
====
498515

499-
The limiting expressions also support the `Distinct` keyword. Also, for the queries that limit the result set to one instance, wrapping the result into with the `Optional` keyword is supported.
516+
The limiting expressions also support the `Distinct` keyword for datastores that support distinct queries.
517+
Also, for the queries that limit the result set to one instance, wrapping the result into with the `Optional` keyword is supported.
500518

501519
If pagination or slicing is applied to a limiting query pagination (and the calculation of the number of available pages), it is applied within the limited result.
502520

@@ -507,6 +525,7 @@ NOTE: Limiting the results in combination with dynamic sorting by using a `Sort`
507525

508526
Query methods that return multiple results can use standard Java `Iterable`, `List`, and `Set`.
509527
Beyond that, we support returning Spring Data's `Streamable`, a custom extension of `Iterable`, as well as collection types provided by https://www.vavr.io/[Vavr].
528+
Refer to the appendix explaining all possible <<appendix.query.return.types,query method return types>>.
510529

511530
[[repositories.collections-and-iterables.streamable]]
512531
==== Using Streamable as Query Method Return Type

src/main/asciidoc/repository-query-keywords-reference.adoc

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,32 @@
22
[appendix]
33
= Repository query keywords
44

5-
== Supported query keywords
6-
The following table lists the keywords generally supported by the Spring Data repository query derivation mechanism. However, consult the store-specific documentation for the exact list of supported keywords, because some keywords listed here might not be supported in a particular store.
5+
[[appendix.query.method.subject]]
6+
== Supported query method subject keywords
77

8-
.Query keywords
9-
[options="header", cols="1,3"]
8+
The following table lists the subject keywords generally supported by the Spring Data repository query derivation mechanism to express the predicate.
9+
Consult the store-specific documentation for the exact list of supported keywords, because some keywords listed here might not be supported in a particular store.
10+
11+
.Query subject keywords
12+
[options="header",cols="1,3"]
13+
|===============
14+
|Keyword | Description
15+
|`find…By`, `read…By`, `get…By`, `query…By`, `search…By`, `stream…By`| General query method returning typically the repository type, a `Collection` or `Streamable` subtype or a result wrapper such as `Page`, `GeoResults` or any other store-specific result wrapper. Can be used as `findBy…`, `findMyDomainTypeBy…` or in combination with additional keywords.
16+
|`exists…By`| Exists projection, returning typically a `boolean` result.
17+
|`count…By`| Count projection returning a numeric result.
18+
|`delete…By`, `remove…By`| Delete query method returning either no result (`void`) or the delete count.
19+
|`…First<number>…`, `…Top<number>…`| Limit the query results to the first `<number>` of results. This keyword can occur in any place of the subject between `find` (and the other keywords) and `by`.
20+
|`…Distinct…`| Use a distinct query to return only unique results. Consult the store-specific documentation whether that feature is supported. This keyword can occur in any place of the subject between `find` (and the other keywords) and `by`.
21+
|===============
22+
23+
[[appendix.query.method.predicate]]
24+
== Supported query method predicate keywords and modifiers
25+
26+
The following table lists the predicate keywords generally supported by the Spring Data repository query derivation mechanism.
27+
However, consult the store-specific documentation for the exact list of supported keywords, because some keywords listed here might not be supported in a particular store.
28+
29+
.Query predicate keywords
30+
[options="header",cols="1,3"]
1031
|===============
1132
|Logical keyword|Keyword expressions
1233
|`AND`|`And`
@@ -38,3 +59,14 @@ The following table lists the keywords generally supported by the Spring Data re
3859
|`TRUE`|`True`, `IsTrue`
3960
|`WITHIN`|`Within`, `IsWithin`
4061
|===============
62+
63+
In addition to filter predicates, the following list of modifiers is supported:
64+
65+
.Query predicate modifier keywords
66+
[options="header",cols="1,3"]
67+
|===============
68+
|Keyword | Description
69+
|`IgnoreCase`, `IgnoringCase`| Used with a predicate keyword for case-insensitive comparison.
70+
|`AllIgnoreCase`, `AllIgnoringCase`| Ignore case for all suitable properties. Used somewhere in the query method predicate.
71+
|`OrderBy…`| Specify a static sorting order followed by the property path and direction (e. g. `OrderByFirstnameAscLastnameDesc`).
72+
|===============

src/main/asciidoc/repository-query-return-types-reference.adoc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
[[repository-query-return-types]]
33
= Repository query return types
44

5+
[[appendix.query.return.types]]
56
== Supported Query Return Types
6-
The following table lists the return types generally supported by Spring Data repositories. However, consult the store-specific documentation for the exact list of supported return types, because some types listed here might not be supported in a particular store.
7+
8+
The following table lists the return types generally supported by Spring Data repositories.
9+
However, consult the store-specific documentation for the exact list of supported return types, because some types listed here might not be supported in a particular store.
710

811
NOTE: Geospatial types (such as `GeoResult`, `GeoResults`, and `GeoPage`) are available only for data stores that support geospatial queries.
12+
Some store modules may define their own result wrapper types.
913

1014
.Query return types
11-
[options="header", cols="1,3"]
15+
[options="header",cols="1,3"]
1216
|===============
1317
|Return type|Description
1418
|`void`|Denotes no return value.
@@ -27,7 +31,7 @@ NOTE: Geospatial types (such as `GeoResult`, `GeoResults`, and `GeoPage`) are av
2731
|`Future<T>`|A `Future`. Expects a method to be annotated with `@Async` and requires Spring's asynchronous method execution capability to be enabled.
2832
|`CompletableFuture<T>`|A Java 8 `CompletableFuture`. Expects a method to be annotated with `@Async` and requires Spring's asynchronous method execution capability to be enabled.
2933
|`ListenableFuture`|A `org.springframework.util.concurrent.ListenableFuture`. Expects a method to be annotated with `@Async` and requires Spring's asynchronous method execution capability to be enabled.
30-
|`Slice`|A sized chunk of data with an indication of whether there is more data available. Requires a `Pageable` method parameter.
34+
|`Slice<T>`|A sized chunk of data with an indication of whether there is more data available. Requires a `Pageable` method parameter.
3135
|`Page<T>`|A `Slice` with additional information, such as the total number of results. Requires a `Pageable` method parameter.
3236
|`GeoResult<T>`|A result entry with additional information, such as the distance to a reference location.
3337
|`GeoResults<T>`|A list of `GeoResult<T>` with additional information, such as the average distance to a reference location.

0 commit comments

Comments
 (0)