144
144
import com .mongodb .reactivestreams .client .MongoDatabase ;
145
145
import com .mongodb .reactivestreams .client .Success ;
146
146
import com .mongodb .util .JSONParseException ;
147
+ import reactor .util .function .Tuples ;
147
148
148
149
/**
149
150
* Primary implementation of {@link ReactiveMongoOperations}. It simplifies the use of Reactive MongoDB usage and helps
@@ -1252,17 +1253,18 @@ protected <T> Mono<T> doInsert(String collectionName, T objectToSave, MongoWrite
1252
1253
1253
1254
return Mono .defer (() -> {
1254
1255
1255
- initializeVersionProperty (objectToSave );
1256
- maybeEmitEvent (new BeforeConvertEvent <>(objectToSave , collectionName ));
1256
+ T toSave = ( T ) initializeVersionProperty (objectToSave );
1257
+ maybeEmitEvent (new BeforeConvertEvent <>(toSave , collectionName ));
1257
1258
1258
- Document dbDoc = toDocument (objectToSave , writer );
1259
+ Document dbDoc = toDocument (toSave , writer );
1259
1260
1260
- maybeEmitEvent (new BeforeSaveEvent <>(objectToSave , dbDoc , collectionName ));
1261
+ maybeEmitEvent (new BeforeSaveEvent <>(toSave , dbDoc , collectionName ));
1261
1262
1262
- Mono <T > afterInsert = insertDBObject (collectionName , dbDoc , objectToSave .getClass ()).flatMap (id -> {
1263
- populateIdIfNecessary (objectToSave , id );
1264
- maybeEmitEvent (new AfterSaveEvent <>(objectToSave , dbDoc , collectionName ));
1265
- return Mono .just (objectToSave );
1263
+ Mono <T > afterInsert = insertDBObject (collectionName , dbDoc , toSave .getClass ()).map (id -> {
1264
+
1265
+ T saved = (T ) populateIdIfNecessary (toSave , id );
1266
+ maybeEmitEvent (new AfterSaveEvent <>(saved , dbDoc , collectionName ));
1267
+ return saved ;
1266
1268
});
1267
1269
1268
1270
return afterInsert ;
@@ -1326,18 +1328,15 @@ protected <T> Flux<T> doInsertBatch(final String collectionName, final Collectio
1326
1328
Assert .notNull (writer , "MongoWriter must not be null!" );
1327
1329
1328
1330
Mono <List <Tuple2 <T , Document >>> prepareDocuments = Flux .fromIterable (batchToSave )
1329
- .flatMap (new Function <T , Flux <Tuple2 <T , Document >>>() {
1330
- @ Override
1331
- public Flux <Tuple2 <T , Document >> apply (T o ) {
1331
+ .map (o -> {
1332
1332
1333
- initializeVersionProperty (o );
1334
- maybeEmitEvent (new BeforeConvertEvent <>(o , collectionName ));
1333
+ T toSave = ( T ) initializeVersionProperty (o );
1334
+ maybeEmitEvent (new BeforeConvertEvent <>(toSave , collectionName ));
1335
1335
1336
- Document dbDoc = toDocument (o , writer );
1336
+ Document dbDoc = toDocument (toSave , writer );
1337
1337
1338
- maybeEmitEvent (new BeforeSaveEvent <>(o , dbDoc , collectionName ));
1339
- return Flux .zip (Mono .just (o ), Mono .just (dbDoc ));
1340
- }
1338
+ maybeEmitEvent (new BeforeSaveEvent <>(toSave , dbDoc , collectionName ));
1339
+ return Tuples .of (toSave , dbDoc );
1341
1340
}).collectList ();
1342
1341
1343
1342
Flux <Tuple2 <T , Document >> insertDocuments = prepareDocuments .flatMapMany (tuples -> {
@@ -1349,9 +1348,9 @@ public Flux<Tuple2<T, Document>> apply(T o) {
1349
1348
1350
1349
return insertDocuments .map (tuple -> {
1351
1350
1352
- populateIdIfNecessary (tuple .getT1 (), tuple .getT2 ().get (ID_FIELD ));
1353
- maybeEmitEvent (new AfterSaveEvent <>(tuple . getT1 () , tuple .getT2 (), collectionName ));
1354
- return tuple . getT1 () ;
1351
+ T saved = ( T ) populateIdIfNecessary (tuple .getT1 (), tuple .getT2 ().get (ID_FIELD ));
1352
+ maybeEmitEvent (new AfterSaveEvent <>(saved , tuple .getT2 (), collectionName ));
1353
+ return saved ;
1355
1354
});
1356
1355
}
1357
1356
@@ -1438,17 +1437,19 @@ private <T> Mono<T> doSaveVersioned(T objectToSave, MongoPersistentEntity<?> ent
1438
1437
// Bump version number
1439
1438
convertingAccessor .setProperty (versionProperty , versionNumber .longValue () + 1 );
1440
1439
1441
- ReactiveMongoTemplate . this . maybeEmitEvent ( new BeforeConvertEvent <>( objectToSave , collectionName ) );
1440
+ T toSave = ( T ) convertingAccessor . getBean ( );
1442
1441
1443
- Document document = ReactiveMongoTemplate .this .toDocument ( objectToSave , mongoConverter );
1442
+ ReactiveMongoTemplate .this .maybeEmitEvent ( new BeforeConvertEvent < T >( toSave , collectionName ) );
1444
1443
1445
- ReactiveMongoTemplate .this .maybeEmitEvent (new BeforeSaveEvent <>(objectToSave , document , collectionName ));
1444
+ Document document = ReactiveMongoTemplate .this .toDocument (toSave , mongoConverter );
1445
+
1446
+ ReactiveMongoTemplate .this .maybeEmitEvent (new BeforeSaveEvent <>(toSave , document , collectionName ));
1446
1447
Update update = Update .fromDocument (document , ID_FIELD );
1447
1448
1448
- return doUpdate (collectionName , query , update , objectToSave .getClass (), false , false ).map (updateResult -> {
1449
+ return doUpdate (collectionName , query , update , toSave .getClass (), false , false ).map (updateResult -> {
1449
1450
1450
- maybeEmitEvent (new AfterSaveEvent <>(objectToSave , document , collectionName ));
1451
- return objectToSave ;
1451
+ maybeEmitEvent (new AfterSaveEvent <>(toSave , document , collectionName ));
1452
+ return toSave ;
1452
1453
});
1453
1454
});
1454
1455
}
@@ -1465,9 +1466,9 @@ protected <T> Mono<T> doSave(String collectionName, T objectToSave, MongoWriter<
1465
1466
1466
1467
return saveDocument (collectionName , dbDoc , objectToSave .getClass ()).map (id -> {
1467
1468
1468
- populateIdIfNecessary (objectToSave , id );
1469
- maybeEmitEvent (new AfterSaveEvent <>(objectToSave , dbDoc , collectionName ));
1470
- return objectToSave ;
1469
+ T saved = ( T ) populateIdIfNecessary (objectToSave , id );
1470
+ maybeEmitEvent (new AfterSaveEvent <>(saved , dbDoc , collectionName ));
1471
+ return saved ;
1471
1472
});
1472
1473
});
1473
1474
}
@@ -2521,35 +2522,37 @@ protected <T> void maybeEmitEvent(MongoMappingEvent<T> event) {
2521
2522
* @param id
2522
2523
*/
2523
2524
@ SuppressWarnings ("unchecked" )
2524
- private void populateIdIfNecessary (Object savedObject , @ Nullable Object id ) {
2525
+ private Object populateIdIfNecessary (Object savedObject , @ Nullable Object id ) {
2525
2526
2526
2527
if (id == null ) {
2527
- return ;
2528
+ return null ;
2528
2529
}
2529
2530
2530
2531
if (savedObject instanceof Map ) {
2531
2532
2532
2533
Map <String , Object > map = (Map <String , Object >) savedObject ;
2533
2534
map .put (ID_FIELD , id );
2534
2535
2535
- return ;
2536
+ return map ;
2536
2537
}
2537
2538
2538
2539
MongoPersistentProperty idProp = getIdPropertyFor (savedObject .getClass ());
2539
2540
2540
2541
if (idProp == null ) {
2541
- return ;
2542
+ return savedObject ;
2542
2543
}
2543
2544
2544
2545
ConversionService conversionService = mongoConverter .getConversionService ();
2545
2546
MongoPersistentEntity <?> entity = mappingContext .getRequiredPersistentEntity (savedObject .getClass ());
2546
2547
PersistentPropertyAccessor accessor = entity .getPropertyAccessor (savedObject );
2547
2548
2548
2549
if (accessor .getProperty (idProp ) != null ) {
2549
- return ;
2550
+ return accessor . getBean () ;
2550
2551
}
2551
2552
2552
2553
new ConvertingPropertyAccessor (accessor , conversionService ).setProperty (idProp , id );
2554
+
2555
+ return accessor .getBean ();
2553
2556
}
2554
2557
2555
2558
private MongoCollection <Document > getAndPrepareCollection (MongoDatabase db , String collectionName ) {
@@ -2807,15 +2810,19 @@ private <T> Document toDocument(T objectToSave, MongoWriter<T> writer) {
2807
2810
}
2808
2811
}
2809
2812
2810
- private void initializeVersionProperty (Object entity ) {
2813
+ private Object initializeVersionProperty (Object entity ) {
2811
2814
2812
2815
MongoPersistentEntity <?> mongoPersistentEntity = getPersistentEntity (entity .getClass ());
2813
2816
2814
2817
if (mongoPersistentEntity != null && mongoPersistentEntity .hasVersionProperty ()) {
2815
2818
ConvertingPropertyAccessor accessor = new ConvertingPropertyAccessor (
2816
2819
mongoPersistentEntity .getPropertyAccessor (entity ), mongoConverter .getConversionService ());
2817
2820
accessor .setProperty (mongoPersistentEntity .getRequiredVersionProperty (), 0 );
2821
+
2822
+ return accessor .getBean ();
2818
2823
}
2824
+
2825
+ return entity ;
2819
2826
}
2820
2827
2821
2828
// Callback implementations
0 commit comments