Skip to content

Commit 1b05fd9

Browse files
committed
Extend the documentation about how to declare repositories.
The new version goes into more details of the various options and is heavily based on changes made for version 3.0.x Closes #2536 See #2537
1 parent 220a0b8 commit 1b05fd9

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/main/asciidoc/repositories.adoc

+23-6
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,34 @@ The sections that follow explain each step in detail:
210210

211211
To define a repository interface, you first need to define a domain class-specific repository interface.
212212
The interface must extend `Repository` and be typed to the domain class and an ID type.
213-
If you want to expose CRUD methods for that domain type, extend `CrudRepository` instead of `Repository`.
213+
If you want to expose CRUD methods for that domain type, you may extend `CrudRepository`, or one of its variants instead of `Repository`.
214214

215215
[[repositories.definition-tuning]]
216216
=== Fine-tuning Repository Definition
217217

218-
Typically, your repository interface extends `Repository`, `CrudRepository`, or `PagingAndSortingRepository`.
219-
Alternatively, if you do not want to extend Spring Data interfaces, you can also annotate your repository interface with `@RepositoryDefinition`.
220-
Extending `CrudRepository` exposes a complete set of methods to manipulate your entities.
221-
If you prefer to be selective about the methods being exposed, copy the methods you want to expose from `CrudRepository` into your domain repository.
218+
There are a few variants how you can get started with your repository interface.
222219

223-
NOTE: Doing so lets you define your own abstractions on top of the provided Spring Data Repositories functionality.
220+
The typical approach is to extend `CrudRepository`, which gives you methods for CRUD functionality.
221+
CRUD stands for Create, Read, Update, Delete.
222+
223+
If you are using a reactive store you might choose `ReactiveCrudRepository`, or `RxJava3CrudRepository` depending on which reactive framework you are using.
224+
225+
If you are using Kotlin you might pick `CoroutineCrudRepository` which utilizes Kotlin's coroutines.
226+
227+
Alternatively you can extend `PagingAndSortingRepository`, `ReactiveSortingRepository`, `RxJava3SortingRepository`, or `CoroutineSortingRepository` if you need methods that allow to specify a `Sort` abstraction or in the first case a `Pageable` abstraction.
228+
These interfaces currently extend the respective CRUD repository of the underlying technology, but will stop to do so with version 3.0.x.
229+
Therefore, we considere it good practice to explicitly inherit from these CRUD repositories explicitely.
230+
231+
If you do not want to extend Spring Data interfaces, you can also annotate your repository interface with `@RepositoryDefinition`.
232+
Extending one of the CRUD repository interfaces exposes a complete set of methods to manipulate your entities.
233+
If you prefer to be selective about the methods being exposed, copy the methods you want to expose from the CRUD repository into your domain repository.
234+
When doing so, you may change the return type of methods.
235+
Spring Data will honor the return type if possible.
236+
For example, for methods returning multiple entities you may choose `Iterable<T>`, `List<T>`, `Collection<T>` or a VAVR list.
237+
238+
If many repositories in your application should have the same set of methods you can define your own base interface to inherit from.
239+
Such an interface must be annotated with `@NoRepositoryBean`.
240+
This prevents Spring Data to try to create an instance of it directly and failing because it can't determine the entity for that repository, since it still contains a generic type variable.
224241

225242
The following example shows how to selectively expose CRUD methods (`findById` and `save`, in this case):
226243

0 commit comments

Comments
 (0)