Skip to content

Commit 4261fd7

Browse files
schaudermp911de
authored andcommitted
Clarify behaviour of when save or delete operations result in a NOOP.
Behaviour of implementing modules need to adapt to the new clarification. Closes #2651 Original pull request #2673
1 parent e69df92 commit 4261fd7

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

src/main/java/org/springframework/data/repository/CrudRepository.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.util.Optional;
1919

20+
import org.springframework.dao.OptimisticLockingFailureException;
21+
2022
/**
2123
* Interface for generic CRUD operations on a repository for a specific type.
2224
*
@@ -34,6 +36,9 @@ public interface CrudRepository<T, ID> extends Repository<T, ID> {
3436
* @param entity must not be {@literal null}.
3537
* @return the saved entity; will never be {@literal null}.
3638
* @throws IllegalArgumentException in case the given {@literal entity} is {@literal null}.
39+
* @throws OptimisticLockingFailureException when the entity has a version attribute with a different value from that
40+
* found in the persistence store. This also occurs when the entity which has a version attribute does no
41+
* longer exist in the database.
3742
*/
3843
<S extends T> S save(S entity);
3944

@@ -45,6 +50,9 @@ public interface CrudRepository<T, ID> extends Repository<T, ID> {
4550
* as the {@literal Iterable} passed as an argument.
4651
* @throws IllegalArgumentException in case the given {@link Iterable entities} or one of its entities is
4752
* {@literal null}.
53+
* @throws OptimisticLockingFailureException when at least one entity has a version attribute with a different value from that
54+
* found in the persistence store. This also occurs when the entity which has a version attribute does no
55+
* longer exist in the database.
4856
*/
4957
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
5058

@@ -96,6 +104,9 @@ public interface CrudRepository<T, ID> extends Repository<T, ID> {
96104

97105
/**
98106
* Deletes the entity with the given id.
107+
* <p>
108+
* When no entity with the given id is available, <b>no</b> exception will be thrown.
109+
* </p>
99110
*
100111
* @param id must not be {@literal null}.
101112
* @throws IllegalArgumentException in case the given {@literal id} is {@literal null}
@@ -107,11 +118,18 @@ public interface CrudRepository<T, ID> extends Repository<T, ID> {
107118
*
108119
* @param entity must not be {@literal null}.
109120
* @throws IllegalArgumentException in case the given entity is {@literal null}.
121+
* @throws OptimisticLockingFailureException when the entity has a version attribute with a different value from that
122+
* found in the persistence store. This also occurs when the entity which has a version attribute does no
123+
* longer exist in the database.
110124
*/
111125
void delete(T entity);
112126

113127
/**
114128
* Deletes all instances of the type {@code T} with the given IDs.
129+
* <p>
130+
* When some or all of the ids aren't found in the persistence store this is silently ignored and <b>no</b> exception
131+
* is thrown.
132+
* </p>
115133
*
116134
* @param ids must not be {@literal null}. Must not contain {@literal null} elements.
117135
* @throws IllegalArgumentException in case the given {@literal ids} or one of its elements is {@literal null}.
@@ -124,6 +142,9 @@ public interface CrudRepository<T, ID> extends Repository<T, ID> {
124142
*
125143
* @param entities must not be {@literal null}. Must not contain {@literal null} elements.
126144
* @throws IllegalArgumentException in case the given {@literal entities} or one of its entities is {@literal null}.
145+
* @throws OptimisticLockingFailureException when at least one of the entities has a version attribute with a
146+
* different value from that found in the persistence store. This also occurs when the entity which has a
147+
* version attribute does no longer exist in the database.
127148
*/
128149
void deleteAll(Iterable<? extends T> entities);
129150

src/main/java/org/springframework/data/repository/reactive/ReactiveCrudRepository.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
/**
2626
* Interface for generic CRUD operations on a repository for a specific type. This repository follows reactive paradigms
2727
* and uses Project Reactor types which are built on top of Reactive Streams.
28+
* <p>
29+
* Save and delete operations with entities that have a version attribute trigger an {@code onError} with a
30+
* {@link org.springframework.dao.OptimisticLockingFailureException} when they encounter a different version value in
31+
* the persistence store than in the entity passed as an argument.
32+
* </p>
33+
* <p>
34+
* Other delete operations that only receive ids or entities without version attribute do not trigger an error when no
35+
* matching data is found in the persistence store.
36+
* </p>
2837
*
2938
* @author Mark Paluch
3039
* @author Christph Strobl

src/main/java/org/springframework/data/repository/reactive/RxJava3CrudRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
/**
2727
* Interface for generic CRUD operations on a repository for a specific type. This repository follows reactive paradigms
2828
* and uses RxJava 3 types.
29+
* <p>
30+
* Save and delete operations with entities that have a version attribute trigger an {@code onError} with a {@link org.springframework.dao.OptimisticLockingFailureException} when they encounter a different version value in the persistence store than in the entity passed as an argument.
31+
* </p>
32+
* <p>
33+
* Other delete operations that only receive ids or entities without version attribute do not trigger an error when no matching data is found in the persistence store.
34+
* </p>
2935
*
3036
* @author Mark Paluch
3137
* @since 2.4

src/main/kotlin/org/springframework/data/repository/kotlin/CoroutineCrudRepository.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ import reactor.core.publisher.Mono
2222

2323
/**
2424
* Interface for generic CRUD operations using Kotlin Coroutines on a repository for a specific type.
25+
* <p>
26+
* Save and delete operations with entities that have a version attribute throw a {@link org.springframework.dao.OptimisticLockingFailureException} when they encounter a different version value in the persistence store than in the entity passed as an argument.
27+
* </p>
28+
* <p>
29+
* Other delete operations that only receive ids or entities without version attribute do not trigger an error when no matching data is found in the persistence store.
30+
* </p>
31+
2532
*
2633
* @author Mark Paluch
2734
* @author Christoph Strobl
@@ -86,7 +93,7 @@ interface CoroutineCrudRepository<T, ID> : Repository<T, ID> {
8693
fun findAll(): Flow<T>
8794

8895
/**
89-
* Returns all instances of the type `T` with the given IDs.
96+
* Returns all instances of the type {@code T} with the given IDs.
9097
* If some or all ids are not found, no entities are returned for these IDs.
9198
* Note that the order of elements in the result is not guaranteed.
9299
*
@@ -98,7 +105,7 @@ interface CoroutineCrudRepository<T, ID> : Repository<T, ID> {
98105
fun findAllById(ids: Iterable<ID>): Flow<T>
99106

100107
/**
101-
* Returns all instances of the type `T` with the given IDs.
108+
* Returns all instances of the type {@code T} with the given IDs.
102109
* If some or all ids are not found, no entities are returned for these IDs.
103110
* Note that the order of elements in the result is not guaranteed.
104111
*
@@ -133,7 +140,7 @@ interface CoroutineCrudRepository<T, ID> : Repository<T, ID> {
133140
suspend fun delete(entity: T)
134141

135142
/**
136-
* Deletes all instances of the type `T` with the given IDs.
143+
* Deletes all instances of the type {@code T} with the given IDs.
137144
*
138145
* @param ids must not be null nor contain any null values.
139146
* @throws IllegalArgumentException in case the given [ids][Iterable] or one of its items is null.

0 commit comments

Comments
 (0)