Skip to content

Commit d1dea13

Browse files
mp911deodrotbohm
authored andcommitted
DATAMONGO-1992 - Add mutation support for immutable objects through MongoTemplate and SimpleMongoRepository.
Persisting methods of MongoTemplate and SimpleMongoRepository now return potentially new object instances of immutable objects. New instances are created using wither methods/Kotlin copy(…) methods if an immutable object requires association with an Id or the version number needs to be incremented.
1 parent ba2ab18 commit d1dea13

File tree

6 files changed

+137
-90
lines changed

6 files changed

+137
-90
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableInsertOperation.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,19 @@ interface TerminatingInsert<T> extends TerminatingBulkInsert<T> {
6363
* Insert exactly one object.
6464
*
6565
* @param object must not be {@literal null}.
66+
* @return the inserted object.
6667
* @throws IllegalArgumentException if object is {@literal null}.
6768
*/
68-
void one(T object);
69+
T one(T object);
6970

7071
/**
7172
* Insert a collection of objects.
7273
*
7374
* @param objects must not be {@literal null}.
75+
* @return the inserted objects.
7476
* @throws IllegalArgumentException if objects is {@literal null}.
7577
*/
76-
void all(Collection<? extends T> objects);
78+
Collection<? extends T> all(Collection<? extends T> objects);
7779
}
7880

7981
/**

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ExecutableInsertOperationSupport.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,23 @@ static class ExecutableInsertSupport<T> implements ExecutableInsert<T> {
7272
* @see org.springframework.data.mongodb.core.ExecutableInsertOperation.TerminatingInsert#insert(java.lang.Class)
7373
*/
7474
@Override
75-
public void one(T object) {
75+
public T one(T object) {
7676

7777
Assert.notNull(object, "Object must not be null!");
7878

79-
template.insert(object, getCollectionName());
79+
return template.insert(object, getCollectionName());
8080
}
8181

8282
/*
8383
* (non-Javadoc)
8484
* @see org.springframework.data.mongodb.core.ExecutableInsertOperation.TerminatingInsert#all(java.util.Collection)
8585
*/
8686
@Override
87-
public void all(Collection<? extends T> objects) {
87+
public Collection<T> all(Collection<? extends T> objects) {
8888

8989
Assert.notNull(objects, "Objects must not be null!");
9090

91-
template.insert(objects, getCollectionName());
91+
return template.insert(objects, getCollectionName());
9292
}
9393

9494
/*

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -1141,8 +1141,9 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
11411141
* Insert is used to initially store the object into the database. To update an existing object use the save method.
11421142
*
11431143
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
1144+
* @return the inserted object.
11441145
*/
1145-
void insert(Object objectToSave);
1146+
<T> T insert(T objectToSave);
11461147

11471148
/**
11481149
* Insert the object into the specified collection.
@@ -1154,32 +1155,36 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
11541155
*
11551156
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
11561157
* @param collectionName name of the collection to store the object in. Must not be {@literal null}.
1158+
* @return the inserted object.
11571159
*/
1158-
void insert(Object objectToSave, String collectionName);
1160+
<T> T insert(T objectToSave, String collectionName);
11591161

11601162
/**
11611163
* Insert a Collection of objects into a collection in a single batch write to the database.
11621164
*
11631165
* @param batchToSave the batch of objects to save. Must not be {@literal null}.
11641166
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
1167+
* @return the inserted objects that.
11651168
*/
1166-
void insert(Collection<? extends Object> batchToSave, Class<?> entityClass);
1169+
<T> Collection<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass);
11671170

11681171
/**
11691172
* Insert a batch of objects into the specified collection in a single batch write to the database.
11701173
*
11711174
* @param batchToSave the list of objects to save. Must not be {@literal null}.
11721175
* @param collectionName name of the collection to store the object in. Must not be {@literal null}.
1176+
* @return the inserted objects that.
11731177
*/
1174-
void insert(Collection<? extends Object> batchToSave, String collectionName);
1178+
<T> Collection<T> insert(Collection<? extends T> batchToSave, String collectionName);
11751179

11761180
/**
11771181
* Insert a mixed Collection of objects into a database collection determining the collection name to use based on the
11781182
* class.
11791183
*
11801184
* @param objectsToSave the list of objects to save. Must not be {@literal null}.
1185+
* @return the inserted objects.
11811186
*/
1182-
void insertAll(Collection<? extends Object> objectsToSave);
1187+
<T> Collection<T> insertAll(Collection<? extends T> objectsToSave);
11831188

11841189
/**
11851190
* Save the object to the collection for the entity type of the object to save. This will perform an insert if the
@@ -1195,8 +1200,9 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
11951200
* Conversion"</a> for more details.
11961201
*
11971202
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
1203+
* @return the saved object.
11981204
*/
1199-
void save(Object objectToSave);
1205+
<T> T save(T objectToSave);
12001206

12011207
/**
12021208
* Save the object to the specified collection. This will perform an insert if the object is not already present, that
@@ -1213,8 +1219,9 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
12131219
*
12141220
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
12151221
* @param collectionName name of the collection to store the object in. Must not be {@literal null}.
1222+
* @return the saved object.
12161223
*/
1217-
void save(Object objectToSave, String collectionName);
1224+
<T> T save(T objectToSave, String collectionName);
12181225

12191226
/**
12201227
* Performs an upsert. If no document is found that matches the query, a new document is created and inserted by

0 commit comments

Comments
 (0)