Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f736636

Browse files
christophstroblmp911de
authored andcommitted
Update documentation.
Add section on reserved method names within repository interfaces. Closes #3173 Original pull request: #3174
1 parent b17bdec commit f736636

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

‎src/main/antora/modules/ROOT/pages/repositories/core-concepts.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public interface CrudRepository<T, ID> extends Repository<T, ID> {
3737
The methods declared in this interface are commonly referred to as CRUD methods.
3838
`ListCrudRepository` offers equivalent methods, but they return `List` where the `CrudRepository` methods return an `Iterable`.
3939

40+
[IMPORTANT]
41+
====
42+
The repository interface implies a few reserved methods like `findById(ID identifier)` that always target the domain types identifier property regardless of its property name. Read more about this in "`xref:repositories/query-methods-details.adoc#repositories.query-methods.reserved-methods[Defining Query Methods]`".
43+
====
44+
4045
NOTE: We also provide persistence technology-specific abstractions, such as `JpaRepository` or `MongoRepository`.
4146
Those interfaces extend `CrudRepository` and expose the capabilities of the underlying persistence technology in addition to the rather generic persistence technology-agnostic interfaces such as `CrudRepository`.
4247

‎src/main/antora/modules/ROOT/pages/repositories/query-keywords-reference.adoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ Consult the store-specific documentation for the exact list of supported keyword
2121
|`...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`.
2222
|===============
2323

24+
[[appendix.query.method.reserved]]
25+
== Reserved methods
26+
27+
The following table lists reserved methods that use predefined functionality which is directly invoked on the backing (store specific) implementation of the repository proxy. See also "`xref:repositories/query-methods-details.adoc#repositories.query-methods.reserved-methods[Defining Query Methods]`".
28+
29+
.Reserved methods
30+
|===============
31+
|`deleteAllById(Iterable<ID> identifiers)`
32+
|`deleteById(ID identifier)`
33+
|`existsById(ID identifier)`
34+
|`findAllById(Iterable<ID> identifiers)`
35+
|`findById(ID identifier)`
36+
|===============
37+
2438
[[appendix.query.method.predicate]]
2539
== Supported query method predicate keywords and modifiers
2640

‎src/main/antora/modules/ROOT/pages/repositories/query-methods-details.adoc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,37 @@ Whether ignoring cases is supported may vary by store, so consult the relevant s
8686
- 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`).
8787
To create a query method that supports dynamic sorting, see "`xref:repositories/query-methods-details.adoc#repositories.special-parameters[Paging, Iterating Large Results, Sorting & Limiting]`".
8888

89+
[[repositories.query-methods.reserved-methods]]
90+
== Reserved Methods
91+
92+
While repository methods typically bind to properties by name, there are a few exceptions to this rule when it comes to certain method names targeting the _identifier_ property. Those _reserved methods_ like `CrudRepository#findById` or just `findById` are targeting the _identifier_ property independent of the actual property name used in the declared method. +
93+
Consider the following domain type holding a property `pk` marked as the identifier via `@Id` and a property called `id`. In this case you need to pay close attention to the naming of your lookup methods as they may collide with predefined signatures.
94+
95+
====
96+
[source,java]
97+
----
98+
public class User {
99+
@Id
100+
Long pk; <1>
101+
Long id; <2>
102+
// ...
103+
}
104+
105+
public interface UserRepository extends Repository<User, Long> {
106+
Optional<User> findById(Long id); <3>
107+
Optional<User> findByPk(Long pk); <4>
108+
Optional<User> findUserById(Long id); <5>
109+
}
110+
----
111+
<1> The identifier property / primary key.
112+
<2> A property called id, but not the identifying one.
113+
<3> Targets the `User#pk` property (the one marked with `@Id` which is considered to be the identifier) instead of `User#id` as the property name would suggest because it is one of the _reserved methods_.
114+
<4> Targets the `User#pk` property by name.
115+
<5> Targets the `User#id` property by using the descriptive token between `find` and `by` to avoid collisions with _reserved methods_.
116+
====
117+
118+
This special behaviour not only targets lookup methods but also applies to the `exits` and `delete` ones. Please refer to the "`xref:repositories/query-keywords-reference.adoc#appendix.query.method.reserved[Repository query keywords]`" for the list of methods.
119+
89120
[[repositories.query-methods.query-property-expressions]]
90121
== Property Expressions
91122

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /