73
73
import org .springframework .data .mongodb .core .aggregation .Fields ;
74
74
import org .springframework .data .mongodb .core .aggregation .TypeBasedAggregationOperationContext ;
75
75
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 .*;
86
77
import org .springframework .data .mongodb .core .index .IndexOperations ;
87
78
import org .springframework .data .mongodb .core .index .IndexOperationsProvider ;
88
79
import org .springframework .data .mongodb .core .index .MongoMappingEventPublisher ;
@@ -1157,26 +1148,26 @@ public long count(Query query, @Nullable Class<?> entityClass, String collection
1157
1148
* @see org.springframework.data.mongodb.core.MongoOperations#insert(java.lang.Object)
1158
1149
*/
1159
1150
@ Override
1160
- public void insert (Object objectToSave ) {
1151
+ public < T > T insert (T objectToSave ) {
1161
1152
1162
1153
Assert .notNull (objectToSave , "ObjectToSave must not be null!" );
1163
1154
1164
1155
ensureNotIterable (objectToSave );
1165
- insert (objectToSave , determineEntityCollectionName (objectToSave ));
1156
+ return insert (objectToSave , determineEntityCollectionName (objectToSave ));
1166
1157
}
1167
1158
1168
1159
/*
1169
1160
* (non-Javadoc)
1170
1161
* @see org.springframework.data.mongodb.core.MongoOperations#insert(java.lang.Object, java.lang.String)
1171
1162
*/
1172
1163
@ Override
1173
- public void insert (Object objectToSave , String collectionName ) {
1164
+ public < T > T insert (T objectToSave , String collectionName ) {
1174
1165
1175
1166
Assert .notNull (objectToSave , "ObjectToSave must not be null!" );
1176
1167
Assert .notNull (collectionName , "CollectionName must not be null!" );
1177
1168
1178
1169
ensureNotIterable (objectToSave );
1179
- doInsert (collectionName , objectToSave , this .mongoConverter );
1170
+ return ( T ) doInsert (collectionName , objectToSave , this .mongoConverter );
1180
1171
}
1181
1172
1182
1173
protected void ensureNotIterable (@ Nullable Object o ) {
@@ -1230,19 +1221,21 @@ private WriteConcern potentiallyForceAcknowledgedWrite(@Nullable WriteConcern wc
1230
1221
return wc ;
1231
1222
}
1232
1223
1233
- protected <T > void doInsert (String collectionName , T objectToSave , MongoWriter <T > writer ) {
1224
+ protected <T > T doInsert (String collectionName , T objectToSave , MongoWriter <T > writer ) {
1234
1225
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 );
1238
1229
1239
- Document dbDoc = toDocument (objectToSave , writer );
1230
+ Document dbDoc = toDocument (toSave , writer );
1240
1231
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 ());
1243
1234
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 ;
1246
1239
}
1247
1240
1248
1241
/**
@@ -1275,7 +1268,7 @@ private <T> Document toDocument(T objectToSave, MongoWriter<T> writer) {
1275
1268
}
1276
1269
}
1277
1270
1278
- private void initializeVersionProperty (Object entity ) {
1271
+ private Object initializeVersionProperty (Object entity ) {
1279
1272
1280
1273
MongoPersistentEntity <?> persistentEntity = getPersistentEntity (entity .getClass ());
1281
1274
@@ -1286,36 +1279,41 @@ private void initializeVersionProperty(Object entity) {
1286
1279
ConvertingPropertyAccessor accessor = new ConvertingPropertyAccessor (persistentEntity .getPropertyAccessor (entity ),
1287
1280
mongoConverter .getConversionService ());
1288
1281
accessor .setProperty (versionProperty , 0 );
1282
+
1283
+ return accessor .getBean ();
1289
1284
}
1285
+
1286
+ return entity ;
1290
1287
}
1291
1288
1292
1289
@ Override
1293
- public void insert (Collection <? extends Object > batchToSave , Class <?> entityClass ) {
1290
+ public < T > Collection < T > insert (Collection <? extends T > batchToSave , Class <?> entityClass ) {
1294
1291
1295
1292
Assert .notNull (batchToSave , "BatchToSave must not be null!" );
1296
1293
1297
- doInsertBatch (determineCollectionName (entityClass ), batchToSave , this .mongoConverter );
1294
+ return ( Collection ) doInsertBatch (determineCollectionName (entityClass ), batchToSave , this .mongoConverter );
1298
1295
}
1299
1296
1300
1297
@ Override
1301
- public void insert (Collection <? extends Object > batchToSave , String collectionName ) {
1298
+ public < T > Collection < T > insert (Collection <? extends T > batchToSave , String collectionName ) {
1302
1299
1303
1300
Assert .notNull (batchToSave , "BatchToSave must not be null!" );
1304
1301
Assert .notNull (collectionName , "CollectionName must not be null!" );
1305
1302
1306
- doInsertBatch (collectionName , batchToSave , this .mongoConverter );
1303
+ return ( Collection ) doInsertBatch (collectionName , batchToSave , this .mongoConverter );
1307
1304
}
1308
1305
1309
1306
@ Override
1310
- public void insertAll (Collection <? extends Object > objectsToSave ) {
1307
+ public < T > Collection < T > insertAll (Collection <? extends T > objectsToSave ) {
1311
1308
1312
1309
Assert .notNull (objectsToSave , "ObjectsToSave must not be null!" );
1313
- doInsertAll (objectsToSave , this .mongoConverter );
1310
+ return ( Collection ) doInsertAll (objectsToSave , this .mongoConverter );
1314
1311
}
1315
1312
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 ) {
1317
1314
1318
1315
Map <String , List <T >> elementsByCollection = new HashMap <>();
1316
+ List <T > savedObjects = new ArrayList <>(listToSave .size ());
1319
1317
1320
1318
for (T element : listToSave ) {
1321
1319
@@ -1337,59 +1335,70 @@ protected <T> void doInsertAll(Collection<? extends T> listToSave, MongoWriter<T
1337
1335
}
1338
1336
1339
1337
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 ) );
1341
1339
}
1340
+
1341
+ return savedObjects ;
1342
1342
}
1343
1343
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 ) {
1345
1346
1346
1347
Assert .notNull (writer , "MongoWriter must not be null!" );
1347
1348
1348
1349
List <Document > documentList = new ArrayList <>();
1349
- for (T o : batchToSave ) {
1350
+ List <T > initializedBatchToSave = new ArrayList <>(batchToSave .size ());
1351
+ for (T uninitialized : batchToSave ) {
1350
1352
1351
- initializeVersionProperty (o );
1352
- maybeEmitEvent (new BeforeConvertEvent <>(o , collectionName ));
1353
+ T toSave = ( T ) initializeVersionProperty (uninitialized );
1354
+ maybeEmitEvent (new BeforeConvertEvent <>(toSave , collectionName ));
1353
1355
1354
- Document document = toDocument (o , writer );
1356
+ Document document = toDocument (toSave , writer );
1355
1357
1356
- maybeEmitEvent (new BeforeSaveEvent <>(o , document , collectionName ));
1358
+ maybeEmitEvent (new BeforeSaveEvent <>(toSave , document , collectionName ));
1357
1359
documentList .add (document );
1360
+ initializedBatchToSave .add (toSave );
1358
1361
}
1359
1362
1360
1363
List <Object > ids = insertDocumentList (collectionName , documentList );
1364
+ List <T > savedObjects = new ArrayList <>(documentList .size ());
1361
1365
1362
1366
int i = 0 ;
1363
- for (T obj : batchToSave ) {
1367
+ for (T obj : initializedBatchToSave ) {
1368
+
1364
1369
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 );
1367
1375
}
1368
1376
i ++;
1369
1377
}
1378
+
1379
+ return savedObjects ;
1370
1380
}
1371
1381
1372
1382
@ Override
1373
- public void save (Object objectToSave ) {
1383
+ public < T > T save (T objectToSave ) {
1374
1384
1375
1385
Assert .notNull (objectToSave , "Object to save must not be null!" );
1376
- save (objectToSave , determineEntityCollectionName (objectToSave ));
1386
+ return save (objectToSave , determineEntityCollectionName (objectToSave ));
1377
1387
}
1378
1388
1379
1389
@ Override
1380
- public void save (Object objectToSave , String collectionName ) {
1390
+ public < T > T save (T objectToSave , String collectionName ) {
1381
1391
1382
1392
Assert .notNull (objectToSave , "Object to save must not be null!" );
1383
1393
Assert .hasText (collectionName , "Collection name must not be null or empty!" );
1384
1394
1385
1395
MongoPersistentEntity <?> entity = getPersistentEntity (objectToSave .getClass ());
1386
1396
1387
1397
if (entity != null && entity .hasVersionProperty ()) {
1388
- doSaveVersioned (objectToSave , entity , collectionName );
1389
- return ;
1398
+ return doSaveVersioned (objectToSave , entity , collectionName );
1390
1399
}
1391
1400
1392
- doSave (collectionName , objectToSave , this .mongoConverter );
1401
+ return ( T ) doSave (collectionName , objectToSave , this .mongoConverter );
1393
1402
}
1394
1403
1395
1404
private <T > T doSaveVersioned (T objectToSave , MongoPersistentEntity <?> entity , String collectionName ) {
@@ -1398,42 +1407,43 @@ private <T> T doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity, S
1398
1407
entity .getPropertyAccessor (objectToSave ), mongoConverter .getConversionService ());
1399
1408
1400
1409
MongoPersistentProperty property = entity .getRequiredVersionProperty ();
1401
- Number number = convertingAccessor .getProperty (property , Number .class );
1410
+ Number number = ( Number ) convertingAccessor .getProperty (property , Number .class );
1402
1411
1403
1412
if (number != null ) {
1404
1413
1405
1414
// Bump version number
1406
1415
convertingAccessor .setProperty (property , number .longValue () + 1 );
1407
1416
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 );
1410
1421
1411
1422
Document document = new Document ();
1412
1423
1413
- this .mongoConverter .write (objectToSave , document );
1424
+ this .mongoConverter .write (toSave , document );
1414
1425
1415
- maybeEmitEvent (new BeforeSaveEvent <>(objectToSave , document , collectionName ));
1426
+ maybeEmitEvent (new BeforeSaveEvent <>(toSave , document , collectionName ));
1416
1427
Update update = Update .fromDocument (document , ID_FIELD );
1417
1428
1418
1429
// Create query for entity with the id and old version
1419
1430
MongoPersistentProperty idProperty = entity .getRequiredIdProperty ();
1420
- Object id = entity .getIdentifierAccessor (objectToSave ).getRequiredIdentifier ();
1431
+ Object id = entity .getIdentifierAccessor (toSave ).getRequiredIdentifier ();
1421
1432
Query query = new Query (Criteria .where (idProperty .getName ()).is (id ).and (property .getName ()).is (number ));
1422
1433
1423
- UpdateResult result = doUpdate (collectionName , query , update , objectToSave .getClass (), false , false );
1434
+ UpdateResult result = doUpdate (collectionName , query , update , toSave .getClass (), false , false );
1424
1435
1425
1436
if (result .getModifiedCount () == 0 ) {
1426
1437
throw new OptimisticLockingFailureException (
1427
1438
String .format ("Cannot save entity %s with version %s to collection %s. Has it been modified meanwhile?" , id ,
1428
1439
number , collectionName ));
1429
1440
}
1430
- maybeEmitEvent (new AfterSaveEvent <>(objectToSave , document , collectionName ));
1441
+ maybeEmitEvent (new AfterSaveEvent <>(toSave , document , collectionName ));
1431
1442
1432
- return objectToSave ;
1443
+ return toSave ;
1433
1444
}
1434
1445
1435
- doInsert (collectionName , objectToSave , this .mongoConverter );
1436
- return objectToSave ;
1446
+ return (T ) doInsert (collectionName , objectToSave , this .mongoConverter );
1437
1447
}
1438
1448
1439
1449
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
1446
1456
maybeEmitEvent (new BeforeSaveEvent <>(objectToSave , dbDoc , collectionName ));
1447
1457
Object id = saveDocument (collectionName , dbDoc , objectToSave .getClass ());
1448
1458
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 ));
1451
1461
1452
- return objectToSave ;
1462
+ return saved ;
1453
1463
}
1454
1464
1455
1465
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
2674
2684
* @param id
2675
2685
*/
2676
2686
@ SuppressWarnings ("unchecked" )
2677
- protected void populateIdIfNecessary (Object savedObject , Object id ) {
2687
+ protected Object populateIdIfNecessary (Object savedObject , Object id ) {
2678
2688
2679
2689
if (id == null ) {
2680
- return ;
2690
+ return null ;
2681
2691
}
2682
2692
2683
2693
if (savedObject instanceof Map ) {
2684
2694
2685
2695
Map <String , Object > map = (Map <String , Object >) savedObject ;
2686
2696
map .put (ID_FIELD , id );
2687
2697
2688
- return ;
2698
+ return map ;
2689
2699
}
2690
2700
2691
2701
MongoPersistentProperty idProperty = getIdPropertyFor (savedObject .getClass ());
@@ -2700,7 +2710,11 @@ protected void populateIdIfNecessary(Object savedObject, Object id) {
2700
2710
if (value == null ) {
2701
2711
new ConvertingPropertyAccessor (accessor , conversionService ).setProperty (idProperty , id );
2702
2712
}
2713
+
2714
+ return accessor .getBean ();
2703
2715
}
2716
+
2717
+ return savedObject ;
2704
2718
}
2705
2719
2706
2720
private MongoCollection <Document > getAndPrepareCollection (MongoDatabase db , String collectionName ) {
0 commit comments