Skip to content

Commit 089e470

Browse files
committed
Polishing.
Add DDD context. Explain identifier to domain object relationship. Tweak wording. See #3173 Original pull request: #3174
1 parent f736636 commit 089e470

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
The central interface in the Spring Data repository abstraction is `Repository`.
55
It takes the domain class to manage as well as the identifier type of the domain class as type arguments.
66
This interface acts primarily as a marker interface to capture the types to work with and to help you to discover interfaces that extend this one.
7+
8+
[TIP]
9+
====
10+
Spring Data considers domain types to be entities, more specifically aggregates.
11+
So you will see the term "entity" used throughout the documentation that can be interchanged with the term "domain type" or "aggregate".
12+
13+
As you might have noticed in the introduction it already hinted towards domain-driven concepts.
14+
We consider domain objects in the sense of DDD.
15+
Domain objects have identifiers (otherwise these would be identity-less value objects), and we somehow need to refer to identifiers when working with certain patterns to access data.
16+
Referring to identifiers will become more meaningful as we talk about repositories and query methods.
17+
====
18+
719
The {spring-data-commons-javadoc-base}/org/springframework/data/repository/CrudRepository.html[`CrudRepository`] and {spring-data-commons-javadoc-base}/org/springframework/data/repository/ListCrudRepository.html[`ListCrudRepository`] interfaces provide sophisticated CRUD functionality for the entity class that is being managed.
820

921
[[repositories.repository]]
@@ -39,7 +51,11 @@ The methods declared in this interface are commonly referred to as CRUD methods.
3951

4052
[IMPORTANT]
4153
====
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]`".
54+
The repository interface implies a few reserved methods like `findById(ID identifier)` that target the domain type identifier property regardless of its property name.
55+
Read more about this in "`xref:repositories/query-methods-details.adoc#repositories.query-methods.reserved-methods[Defining Query Methods]`".
56+
57+
You can annotate your query method with `@Query` to provide a custom query if a property named `Id` doesn't refer to the identifier.
58+
Following that path can easily lead to confusion and is discouraged as you will quickly hit type limits if the `ID` type and the type of your `Id` property deviate.
4359
====
4460

4561
NOTE: We also provide persistence technology-specific abstractions, such as `JpaRepository` or `MongoRepository`.

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ Consult the store-specific documentation for the exact list of supported keyword
2424
[[appendix.query.method.reserved]]
2525
== Reserved methods
2626

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]`".
27+
The following table lists reserved methods that use predefined functionality (as defined in `CrudRepository`).
28+
These methods are directly invoked on the backing (store-specific) implementation of the repository proxy.
29+
See also "`xref:repositories/query-methods-details.adoc#repositories.query-methods.reserved-methods[Defining Query Methods]`".
2830

2931
.Reserved methods
3032
|===============

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

+27-17
Original file line numberDiff line numberDiff line change
@@ -87,35 +87,45 @@ Whether ignoring cases is supported may vary by store, so consult the relevant s
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

8989
[[repositories.query-methods.reserved-methods]]
90-
== Reserved Methods
90+
== Reserved Method Names
9191

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.
92+
While derived repository methods bind to properties by name, there are a few exceptions to this rule when it comes to certain method names inherited from the base repository targeting the _identifier_ property.
93+
Those _reserved methods_ like `CrudRepository#findById` (or just `findById`) are targeting the _identifier_ property regardless of the actual property name used in the declared method.
94+
95+
Consider the following domain type holding a property `pk` marked as the identifier via `@Id` and a property called `id`.
96+
In this case you need to pay close attention to the naming of your lookup methods as they may collide with predefined signatures:
9497

9598
====
9699
[source,java]
97100
----
98-
public class User {
99-
@Id
100-
Long pk; <1>
101-
Long id; <2>
102-
// ...
101+
class User {
102+
@Id Long pk; <1>
103+
104+
Long id; <2>
105+
106+
// …
103107
}
104108
105-
public interface UserRepository extends Repository<User, Long> {
106-
Optional<User> findById(Long id); <3>
107-
Optional<User> findByPk(Long pk); <4>
109+
interface UserRepository extends Repository<User, Long> {
110+
111+
Optional<User> findById(Long id); <3>
112+
113+
Optional<User> findByPk(Long pk); <4>
114+
108115
Optional<User> findUserById(Long id); <5>
109116
}
110117
----
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_.
118+
119+
<1> The identifier property (primary key).
120+
<2> A property named `id`, but not the identifier.
121+
<3> Targets the `pk` property (the one marked with `@Id` which is considered to be the identifier) as it refers to a `CrudRepository` base repository method.
122+
Therefore, it is not a derived query using of `id` as the property name would suggest because it is one of the _reserved methods_.
123+
<4> Targets the `pk` property by name as it is a derived query.
124+
<5> Targets the `id` property by using the descriptive token between `find` and `by` to avoid collisions with _reserved methods_.
116125
====
117126

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.
127+
This special behaviour not only targets lookup methods but also applies to the `exits` and `delete` ones.
128+
Please refer to the "`xref:repositories/query-keywords-reference.adoc#appendix.query.method.reserved[Repository query keywords]`" for the list of methods.
119129

120130
[[repositories.query-methods.query-property-expressions]]
121131
== Property Expressions

0 commit comments

Comments
 (0)