Skip to content

Commit 8da40c1

Browse files
committed
Added documentation.
See #2537 Original pull request #2540
1 parent 5117e09 commit 8da40c1

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

src/main/asciidoc/repositories.adoc

+26-8
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ public interface CrudRepository<T, ID> extends Repository<T, ID> {
5858
NOTE: We also provide persistence technology-specific abstractions, such as `JpaRepository` or `MongoRepository`.
5959
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`.
6060

61-
On top of the `CrudRepository`, there is a https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/PagingAndSortingRepository.html[`PagingAndSortingRepository`] abstraction that adds additional methods to ease paginated access to entities:
61+
Additional to the `CrudRepository`, there is a https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/PagingAndSortingRepository.html[`PagingAndSortingRepository`] abstraction that adds additional methods to ease paginated access to entities:
6262

6363
.`PagingAndSortingRepository` interface
6464
====
6565
[source,java]
6666
----
67-
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
67+
public interface PagingAndSortingRepository<T, ID> {
6868
6969
Iterable<T> findAll(Sort sort);
7070
@@ -210,17 +210,35 @@ 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+
With version 3.0 we also introduced `ListCrudRepository` which is very similar to the `CrudRepository` but for those methods that return multiple entities it returns a `List` instead of an `Iterable` which you might find easier to use.
223+
224+
If you are using a reactive store you might choose `ReactiveCrudRepository`, or `RxJava3CrudRepository` depending on which reactive framework you are using.
225+
226+
If you are using Kotlin you might pick `CoroutineCrudRepository` which utilizes Kotlin's coroutines.
227+
228+
Additional 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.
229+
Note that the various sorting repositories no longer extended their respective CRUD repository as they did in Spring Data Versions pre 3.0.
230+
Therefore, you need to extend both interfaces if you want functionality of both.
231+
232+
If you do not want to extend Spring Data interfaces, you can also annotate your repository interface with `@RepositoryDefinition`.
233+
Extending one of the CRUD repository interfaces exposes a complete set of methods to manipulate your entities.
234+
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.
235+
When doing so, you may change the return type of methods.
236+
Spring Data will honor the return type if possible.
237+
For example, for methods returning multiple entities you may choose `Iterable<T>`, `List<T>`, `Collection<T>` or a VAVR list.
238+
239+
If many repositories in your application should have the same set of methods you can define your own base interface to inherit from.
240+
Such an interface must be annotated with `@NoRepositoryBean`.
241+
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.
224242

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

0 commit comments

Comments
 (0)