Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d1dea13

Browse files
mp911deodrotbohm
authored andcommittedJul 12, 2018
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 14 additions & 7 deletions
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

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

Lines changed: 78 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,7 @@
7373
import org.springframework.data.mongodb.core.aggregation.Fields;
7474
import org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext;
7575
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
76-
import org.springframework.data.mongodb.core.convert.DbRefResolver;
77-
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
78-
import org.springframework.data.mongodb.core.convert.JsonSchemaMapper;
79-
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
80-
import org.springframework.data.mongodb.core.convert.MongoConverter;
81-
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
82-
import org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper;
83-
import org.springframework.data.mongodb.core.convert.MongoWriter;
84-
import org.springframework.data.mongodb.core.convert.QueryMapper;
85-
import org.springframework.data.mongodb.core.convert.UpdateMapper;
76+
import org.springframework.data.mongodb.core.convert.*;
8677
import org.springframework.data.mongodb.core.index.IndexOperations;
8778
import org.springframework.data.mongodb.core.index.IndexOperationsProvider;
8879
import org.springframework.data.mongodb.core.index.MongoMappingEventPublisher;
@@ -1157,26 +1148,26 @@ public long count(Query query, @Nullable Class<?> entityClass, String collection
11571148
* @see org.springframework.data.mongodb.core.MongoOperations#insert(java.lang.Object)
11581149
*/
11591150
@Override
1160-
public void insert(Object objectToSave) {
1151+
public <T> T insert(T objectToSave) {
11611152

11621153
Assert.notNull(objectToSave, "ObjectToSave must not be null!");
11631154

11641155
ensureNotIterable(objectToSave);
1165-
insert(objectToSave, determineEntityCollectionName(objectToSave));
1156+
return insert(objectToSave, determineEntityCollectionName(objectToSave));
11661157
}
11671158

11681159
/*
11691160
* (non-Javadoc)
11701161
* @see org.springframework.data.mongodb.core.MongoOperations#insert(java.lang.Object, java.lang.String)
11711162
*/
11721163
@Override
1173-
public void insert(Object objectToSave, String collectionName) {
1164+
public <T> T insert(T objectToSave, String collectionName) {
11741165

11751166
Assert.notNull(objectToSave, "ObjectToSave must not be null!");
11761167
Assert.notNull(collectionName, "CollectionName must not be null!");
11771168

11781169
ensureNotIterable(objectToSave);
1179-
doInsert(collectionName, objectToSave, this.mongoConverter);
1170+
return (T) doInsert(collectionName, objectToSave, this.mongoConverter);
11801171
}
11811172

11821173
protected void ensureNotIterable(@Nullable Object o) {
@@ -1230,19 +1221,21 @@ private WriteConcern potentiallyForceAcknowledgedWrite(@Nullable WriteConcern wc
12301221
return wc;
12311222
}
12321223

1233-
protected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T> writer) {
1224+
protected <T> T doInsert(String collectionName, T objectToSave, MongoWriter<T> writer) {
12341225

1235-
initializeVersionProperty(objectToSave);
1236-
maybeEmitEvent(new BeforeConvertEvent<>(objectToSave, collectionName));
1237-
assertUpdateableIdIfNotSet(objectToSave);
1226+
T toSave = (T) initializeVersionProperty(objectToSave);
1227+
maybeEmitEvent(new BeforeConvertEvent<>(toSave, collectionName));
1228+
assertUpdateableIdIfNotSet(toSave);
12381229

1239-
Document dbDoc = toDocument(objectToSave, writer);
1230+
Document dbDoc = toDocument(toSave, writer);
12401231

1241-
maybeEmitEvent(new BeforeSaveEvent<>(objectToSave, dbDoc, collectionName));
1242-
Object id = insertDocument(collectionName, dbDoc, objectToSave.getClass());
1232+
maybeEmitEvent(new BeforeSaveEvent<>(toSave, dbDoc, collectionName));
1233+
Object id = insertDocument(collectionName, dbDoc, toSave.getClass());
12431234

1244-
populateIdIfNecessary(objectToSave, id);
1245-
maybeEmitEvent(new AfterSaveEvent<>(objectToSave, dbDoc, collectionName));
1235+
T saved = (T) populateIdIfNecessary(toSave, id);
1236+
maybeEmitEvent(new AfterSaveEvent<>(saved, dbDoc, collectionName));
1237+
1238+
return saved;
12461239
}
12471240

12481241
/**
@@ -1275,7 +1268,7 @@ private <T> Document toDocument(T objectToSave, MongoWriter<T> writer) {
12751268
}
12761269
}
12771270

1278-
private void initializeVersionProperty(Object entity) {
1271+
private Object initializeVersionProperty(Object entity) {
12791272

12801273
MongoPersistentEntity<?> persistentEntity = getPersistentEntity(entity.getClass());
12811274

@@ -1286,36 +1279,41 @@ private void initializeVersionProperty(Object entity) {
12861279
ConvertingPropertyAccessor accessor = new ConvertingPropertyAccessor(persistentEntity.getPropertyAccessor(entity),
12871280
mongoConverter.getConversionService());
12881281
accessor.setProperty(versionProperty, 0);
1282+
1283+
return accessor.getBean();
12891284
}
1285+
1286+
return entity;
12901287
}
12911288

12921289
@Override
1293-
public void insert(Collection<? extends Object> batchToSave, Class<?> entityClass) {
1290+
public <T> Collection<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass) {
12941291

12951292
Assert.notNull(batchToSave, "BatchToSave must not be null!");
12961293

1297-
doInsertBatch(determineCollectionName(entityClass), batchToSave, this.mongoConverter);
1294+
return (Collection) doInsertBatch(determineCollectionName(entityClass), batchToSave, this.mongoConverter);
12981295
}
12991296

13001297
@Override
1301-
public void insert(Collection<? extends Object> batchToSave, String collectionName) {
1298+
public <T> Collection<T> insert(Collection<? extends T> batchToSave, String collectionName) {
13021299

13031300
Assert.notNull(batchToSave, "BatchToSave must not be null!");
13041301
Assert.notNull(collectionName, "CollectionName must not be null!");
13051302

1306-
doInsertBatch(collectionName, batchToSave, this.mongoConverter);
1303+
return (Collection) doInsertBatch(collectionName, batchToSave, this.mongoConverter);
13071304
}
13081305

13091306
@Override
1310-
public void insertAll(Collection<? extends Object> objectsToSave) {
1307+
public <T> Collection<T> insertAll(Collection<? extends T> objectsToSave) {
13111308

13121309
Assert.notNull(objectsToSave, "ObjectsToSave must not be null!");
1313-
doInsertAll(objectsToSave, this.mongoConverter);
1310+
return (Collection) doInsertAll(objectsToSave, this.mongoConverter);
13141311
}
13151312

1316-
protected <T> void doInsertAll(Collection<? extends T> listToSave, MongoWriter<T> writer) {
1313+
protected <T> Collection<T> doInsertAll(Collection<? extends T> listToSave, MongoWriter<T> writer) {
13171314

13181315
Map<String, List<T>> elementsByCollection = new HashMap<>();
1316+
List<T> savedObjects = new ArrayList<>(listToSave.size());
13191317

13201318
for (T element : listToSave) {
13211319

@@ -1337,59 +1335,70 @@ protected <T> void doInsertAll(Collection<? extends T> listToSave, MongoWriter<T
13371335
}
13381336

13391337
for (Map.Entry<String, List<T>> entry : elementsByCollection.entrySet()) {
1340-
doInsertBatch(entry.getKey(), entry.getValue(), this.mongoConverter);
1338+
savedObjects.addAll((Collection) doInsertBatch(entry.getKey(), entry.getValue(), this.mongoConverter));
13411339
}
1340+
1341+
return savedObjects;
13421342
}
13431343

1344-
protected <T> void doInsertBatch(String collectionName, Collection<? extends T> batchToSave, MongoWriter<T> writer) {
1344+
protected <T> Collection<T> doInsertBatch(String collectionName, Collection<? extends T> batchToSave,
1345+
MongoWriter<T> writer) {
13451346

13461347
Assert.notNull(writer, "MongoWriter must not be null!");
13471348

13481349
List<Document> documentList = new ArrayList<>();
1349-
for (T o : batchToSave) {
1350+
List<T> initializedBatchToSave = new ArrayList<>(batchToSave.size());
1351+
for (T uninitialized : batchToSave) {
13501352

1351-
initializeVersionProperty(o);
1352-
maybeEmitEvent(new BeforeConvertEvent<>(o, collectionName));
1353+
T toSave = (T) initializeVersionProperty(uninitialized);
1354+
maybeEmitEvent(new BeforeConvertEvent<>(toSave, collectionName));
13531355

1354-
Document document = toDocument(o, writer);
1356+
Document document = toDocument(toSave, writer);
13551357

1356-
maybeEmitEvent(new BeforeSaveEvent<>(o, document, collectionName));
1358+
maybeEmitEvent(new BeforeSaveEvent<>(toSave, document, collectionName));
13571359
documentList.add(document);
1360+
initializedBatchToSave.add(toSave);
13581361
}
13591362

13601363
List<Object> ids = insertDocumentList(collectionName, documentList);
1364+
List<T> savedObjects = new ArrayList<>(documentList.size());
13611365

13621366
int i = 0;
1363-
for (T obj : batchToSave) {
1367+
for (T obj : initializedBatchToSave) {
1368+
13641369
if (i < ids.size()) {
1365-
populateIdIfNecessary(obj, ids.get(i));
1366-
maybeEmitEvent(new AfterSaveEvent<>(obj, documentList.get(i), collectionName));
1370+
T saved = (T) populateIdIfNecessary(obj, ids.get(i));
1371+
maybeEmitEvent(new AfterSaveEvent<>(saved, documentList.get(i), collectionName));
1372+
savedObjects.add(saved);
1373+
} else {
1374+
savedObjects.add(obj);
13671375
}
13681376
i++;
13691377
}
1378+
1379+
return savedObjects;
13701380
}
13711381

13721382
@Override
1373-
public void save(Object objectToSave) {
1383+
public <T> T save(T objectToSave) {
13741384

13751385
Assert.notNull(objectToSave, "Object to save must not be null!");
1376-
save(objectToSave, determineEntityCollectionName(objectToSave));
1386+
return save(objectToSave, determineEntityCollectionName(objectToSave));
13771387
}
13781388

13791389
@Override
1380-
public void save(Object objectToSave, String collectionName) {
1390+
public <T> T save(T objectToSave, String collectionName) {
13811391

13821392
Assert.notNull(objectToSave, "Object to save must not be null!");
13831393
Assert.hasText(collectionName, "Collection name must not be null or empty!");
13841394

13851395
MongoPersistentEntity<?> entity = getPersistentEntity(objectToSave.getClass());
13861396

13871397
if (entity != null && entity.hasVersionProperty()) {
1388-
doSaveVersioned(objectToSave, entity, collectionName);
1389-
return;
1398+
return doSaveVersioned(objectToSave, entity, collectionName);
13901399
}
13911400

1392-
doSave(collectionName, objectToSave, this.mongoConverter);
1401+
return (T) doSave(collectionName, objectToSave, this.mongoConverter);
13931402
}
13941403

13951404
private <T> T doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity, String collectionName) {
@@ -1398,42 +1407,43 @@ private <T> T doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity, S
13981407
entity.getPropertyAccessor(objectToSave), mongoConverter.getConversionService());
13991408

14001409
MongoPersistentProperty property = entity.getRequiredVersionProperty();
1401-
Number number = convertingAccessor.getProperty(property, Number.class);
1410+
Number number = (Number) convertingAccessor.getProperty(property, Number.class);
14021411

14031412
if (number != null) {
14041413

14051414
// Bump version number
14061415
convertingAccessor.setProperty(property, number.longValue() + 1);
14071416

1408-
maybeEmitEvent(new BeforeConvertEvent<>(objectToSave, collectionName));
1409-
assertUpdateableIdIfNotSet(objectToSave);
1417+
T toSave = (T) convertingAccessor.getBean();
1418+
1419+
maybeEmitEvent(new BeforeConvertEvent<>(toSave, collectionName));
1420+
assertUpdateableIdIfNotSet(toSave);
14101421

14111422
Document document = new Document();
14121423

1413-
this.mongoConverter.write(objectToSave, document);
1424+
this.mongoConverter.write(toSave, document);
14141425

1415-
maybeEmitEvent(new BeforeSaveEvent<>(objectToSave, document, collectionName));
1426+
maybeEmitEvent(new BeforeSaveEvent<>(toSave, document, collectionName));
14161427
Update update = Update.fromDocument(document, ID_FIELD);
14171428

14181429
// Create query for entity with the id and old version
14191430
MongoPersistentProperty idProperty = entity.getRequiredIdProperty();
1420-
Object id = entity.getIdentifierAccessor(objectToSave).getRequiredIdentifier();
1431+
Object id = entity.getIdentifierAccessor(toSave).getRequiredIdentifier();
14211432
Query query = new Query(Criteria.where(idProperty.getName()).is(id).and(property.getName()).is(number));
14221433

1423-
UpdateResult result = doUpdate(collectionName, query, update, objectToSave.getClass(), false, false);
1434+
UpdateResult result = doUpdate(collectionName, query, update, toSave.getClass(), false, false);
14241435

14251436
if (result.getModifiedCount() == 0) {
14261437
throw new OptimisticLockingFailureException(
14271438
String.format("Cannot save entity %s with version %s to collection %s. Has it been modified meanwhile?", id,
14281439
number, collectionName));
14291440
}
1430-
maybeEmitEvent(new AfterSaveEvent<>(objectToSave, document, collectionName));
1441+
maybeEmitEvent(new AfterSaveEvent<>(toSave, document, collectionName));
14311442

1432-
return objectToSave;
1443+
return toSave;
14331444
}
14341445

1435-
doInsert(collectionName, objectToSave, this.mongoConverter);
1436-
return objectToSave;
1446+
return (T) doInsert(collectionName, objectToSave, this.mongoConverter);
14371447
}
14381448

14391449
protected <T> T doSave(String collectionName, T objectToSave, MongoWriter<T> writer) {
@@ -1446,10 +1456,10 @@ protected <T> T doSave(String collectionName, T objectToSave, MongoWriter<T> wri
14461456
maybeEmitEvent(new BeforeSaveEvent<>(objectToSave, dbDoc, collectionName));
14471457
Object id = saveDocument(collectionName, dbDoc, objectToSave.getClass());
14481458

1449-
populateIdIfNecessary(objectToSave, id);
1450-
maybeEmitEvent(new AfterSaveEvent<>(objectToSave, dbDoc, collectionName));
1459+
T saved = (T) populateIdIfNecessary(objectToSave, id);
1460+
maybeEmitEvent(new AfterSaveEvent<>(saved, dbDoc, collectionName));
14511461

1452-
return objectToSave;
1462+
return saved;
14531463
}
14541464

14551465
protected Object insertDocument(final String collectionName, final Document document, final Class<?> entityClass) {
@@ -2674,18 +2684,18 @@ protected <T> T doFindAndReplace(String collectionName, Document mappedQuery, Do
26742684
* @param id
26752685
*/
26762686
@SuppressWarnings("unchecked")
2677-
protected void populateIdIfNecessary(Object savedObject, Object id) {
2687+
protected Object populateIdIfNecessary(Object savedObject, Object id) {
26782688

26792689
if (id == null) {
2680-
return;
2690+
return null;
26812691
}
26822692

26832693
if (savedObject instanceof Map) {
26842694

26852695
Map<String, Object> map = (Map<String, Object>) savedObject;
26862696
map.put(ID_FIELD, id);
26872697

2688-
return;
2698+
return map;
26892699
}
26902700

26912701
MongoPersistentProperty idProperty = getIdPropertyFor(savedObject.getClass());
@@ -2700,7 +2710,11 @@ protected void populateIdIfNecessary(Object savedObject, Object id) {
27002710
if (value == null) {
27012711
new ConvertingPropertyAccessor(accessor, conversionService).setProperty(idProperty, id);
27022712
}
2713+
2714+
return accessor.getBean();
27032715
}
2716+
2717+
return savedObject;
27042718
}
27052719

27062720
private MongoCollection<Document> getAndPrepareCollection(MongoDatabase db, String collectionName) {

‎spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

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

1818
import static org.springframework.data.mongodb.core.query.Criteria.*;
1919

20+
import java.util.ArrayList;
2021
import java.util.Collections;
2122
import java.util.List;
2223
import java.util.Optional;
@@ -77,12 +78,10 @@ public <S extends T> S save(S entity) {
7778
Assert.notNull(entity, "Entity must not be null!");
7879

7980
if (entityInformation.isNew(entity)) {
80-
mongoOperations.insert(entity, entityInformation.getCollectionName());
81-
} else {
82-
mongoOperations.save(entity, entityInformation.getCollectionName());
81+
return mongoOperations.insert(entity, entityInformation.getCollectionName());
8382
}
8483

85-
return entity;
84+
return mongoOperations.save(entity, entityInformation.getCollectionName());
8685
}
8786

8887
/*
@@ -100,12 +99,10 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
10099
if (allNew) {
101100

102101
List<S> result = source.stream().collect(Collectors.toList());
103-
mongoOperations.insert(result, entityInformation.getCollectionName());
104-
return result;
105-
106-
} else {
107-
return source.stream().map(this::save).collect(Collectors.toList());
102+
return new ArrayList<>(mongoOperations.insert(result, entityInformation.getCollectionName()));
108103
}
104+
105+
return source.stream().map(this::save).collect(Collectors.toList());
109106
}
110107

111108
/*
@@ -244,8 +241,7 @@ public <S extends T> S insert(S entity) {
244241

245242
Assert.notNull(entity, "Entity must not be null!");
246243

247-
mongoOperations.insert(entity, entityInformation.getCollectionName());
248-
return entity;
244+
return mongoOperations.insert(entity, entityInformation.getCollectionName());
249245
}
250246

251247
/*
@@ -263,8 +259,7 @@ public <S extends T> List<S> insert(Iterable<S> entities) {
263259
return list;
264260
}
265261

266-
mongoOperations.insertAll(list);
267-
return list;
262+
return new ArrayList<>(mongoOperations.insertAll(list));
268263
}
269264

270265
/*

‎spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.stream.Collectors;
4040
import java.util.stream.IntStream;
4141

42+
import lombok.experimental.Wither;
4243
import org.bson.types.ObjectId;
4344
import org.hamcrest.collection.IsMapContaining;
4445
import org.joda.time.DateTime;
@@ -1638,6 +1639,21 @@ public void initializesVersionOnBatchInsert() {
16381639
assertThat(person.version, is(0));
16391640
}
16401641

1642+
@Test // DATAMONGO-1992
1643+
public void initializesIdAndVersionAndOfImmutableObject() {
1644+
1645+
ImmutableVersioned versioned = new ImmutableVersioned();
1646+
1647+
ImmutableVersioned saved = template.insert(versioned);
1648+
1649+
assertThat(saved, is(not(sameInstance(versioned))));
1650+
assertThat(versioned.id, is(nullValue()));
1651+
assertThat(versioned.version, is(nullValue()));
1652+
1653+
assertThat(saved.id, is(notNullValue()));
1654+
assertThat(saved.version, is(0L));
1655+
}
1656+
16411657
@Test(expected = IllegalArgumentException.class) // DATAMONGO-568, DATAMONGO-1762
16421658
public void queryCantBeNull() {
16431659
template.find(null, PersonWithIdPropertyOfTypeObjectId.class);
@@ -4057,4 +4073,17 @@ public String toString() {
40574073
}
40584074

40594075
}
4076+
4077+
@AllArgsConstructor
4078+
@Wither
4079+
static class ImmutableVersioned {
4080+
4081+
final @Id String id;
4082+
final @Version Long version;
4083+
4084+
public ImmutableVersioned() {
4085+
id = null;
4086+
version = null;
4087+
}
4088+
}
40604089
}

0 commit comments

Comments
 (0)
Please sign in to comment.