22
22
import reactor .core .publisher .Mono ;
23
23
import reactor .util .function .Tuple2 ;
24
24
25
- import java .util .ArrayList ;
26
- import java .util .Collection ;
27
- import java .util .Collections ;
28
- import java .util .HashMap ;
29
- import java .util .HashSet ;
30
- import java .util .Iterator ;
31
- import java .util .List ;
32
- import java .util .Map ;
25
+ import java .util .*;
33
26
import java .util .Map .Entry ;
34
- import java .util .Optional ;
35
- import java .util .Set ;
36
27
import java .util .function .Function ;
37
28
import java .util .stream .Collectors ;
38
29
59
50
import org .springframework .data .geo .Distance ;
60
51
import org .springframework .data .geo .GeoResult ;
61
52
import org .springframework .data .geo .Metric ;
62
- import org .springframework .data .mapping .PersistentEntity ;
63
53
import org .springframework .data .mapping .PersistentPropertyAccessor ;
64
54
import org .springframework .data .mapping .context .MappingContext ;
65
55
import org .springframework .data .mapping .model .ConvertingPropertyAccessor ;
66
- import org .springframework .data .mapping .model . MappingException ;
56
+ import org .springframework .data .mapping .MappingException ;
67
57
import org .springframework .data .mongodb .MongoDbFactory ;
68
58
import org .springframework .data .mongodb .ReactiveMongoDatabaseFactory ;
69
59
import org .springframework .data .mongodb .core .convert .DbRefProxyHandler ;
94
84
import org .springframework .data .mongodb .core .query .Update ;
95
85
import org .springframework .data .mongodb .util .MongoClientVersion ;
96
86
import org .springframework .data .util .Optionals ;
87
+ import org .springframework .data .util .Pair ;
97
88
import org .springframework .util .Assert ;
98
89
import org .springframework .util .ObjectUtils ;
99
90
import org .springframework .util .StringUtils ;
@@ -615,8 +606,8 @@ public <T> Mono<T> findById(Object id, Class<T> entityClass) {
615
606
*/
616
607
public <T > Mono <T > findById (Object id , Class <T > entityClass , String collectionName ) {
617
608
618
- Optional <? extends MongoPersistentEntity <?> > persistentEntity = mappingContext .getPersistentEntity (entityClass );
619
- MongoPersistentProperty idProperty = persistentEntity . flatMap ( PersistentEntity :: getIdProperty ). orElse ( null ) ;
609
+ MongoPersistentEntity <?> persistentEntity = mappingContext .getPersistentEntity (entityClass );
610
+ MongoPersistentProperty idProperty = persistentEntity != null ? persistentEntity . getIdProperty () : null ;
620
611
621
612
String idKey = idProperty == null ? ID_FIELD : idProperty .getName ();
622
613
@@ -764,7 +755,7 @@ public Mono<Long> count(final Query query, final Class<?> entityClass, String co
764
755
765
756
final Document Document = query == null ? null
766
757
: queryMapper .getMappedObject (query .getQueryObject (),
767
- entityClass == null ? Optional . empty () : mappingContext .getPersistentEntity (entityClass ));
758
+ entityClass == null ? null : mappingContext .getPersistentEntity (entityClass ));
768
759
769
760
return collection .count (Document );
770
761
});
@@ -971,29 +962,28 @@ private <T> Mono<T> doSaveVersioned(T objectToSave, MongoPersistentEntity<?> ent
971
962
ConvertingPropertyAccessor convertingAccessor = new ConvertingPropertyAccessor (
972
963
entity .getPropertyAccessor (objectToSave ), mongoConverter .getConversionService ());
973
964
974
- MongoPersistentProperty idProperty = entity .getIdProperty ()
975
- .orElseThrow (() -> new IllegalArgumentException ("No id property present!" ));
976
- MongoPersistentProperty versionProperty = entity .getVersionProperty ()
977
- .orElseThrow (() -> new IllegalArgumentException ("No version property present!" ));
978
- ;
965
+ MongoPersistentProperty idProperty = entity .getRequiredIdProperty ();
966
+ MongoPersistentProperty versionProperty = entity .getRequiredVersionProperty ();
979
967
980
- Optional < Object > version = convertingAccessor .getProperty (versionProperty );
981
- Optional < Number > versionNumber = convertingAccessor .getProperty (versionProperty , Number .class );
968
+ Object version = convertingAccessor .getProperty (versionProperty );
969
+ Number versionNumber = convertingAccessor .getProperty (versionProperty , Number .class );
982
970
983
971
// Fresh instance -> initialize version property
984
- if (! version . isPresent () ) {
972
+ if (version == null ) {
985
973
return doInsert (collectionName , objectToSave , mongoConverter );
986
974
}
987
975
988
- ReactiveMongoTemplate . this . assertUpdateableIdIfNotSet (objectToSave );
976
+ assertUpdateableIdIfNotSet (objectToSave );
989
977
990
978
// Create query for entity with the id and old version
991
- Optional <Object > id = convertingAccessor .getProperty (idProperty );
992
- Query query = new Query (
993
- Criteria .where (idProperty .getName ()).is (id .get ()).and (versionProperty .getName ()).is (version .get ()));
979
+ Object id = convertingAccessor .getProperty (idProperty );
980
+ Query query = new Query (Criteria .where (idProperty .getName ()).is (id ).and (versionProperty .getName ()).is (version ));
994
981
982
+ if (versionNumber == null ) {
983
+ versionNumber = 0 ;
984
+ }
995
985
// Bump version number
996
- convertingAccessor .setProperty (versionProperty , Optional . of ( versionNumber .orElse ( 0 ). longValue () + 1 ) );
986
+ convertingAccessor .setProperty (versionProperty , versionNumber .longValue () + 1 );
997
987
998
988
ReactiveMongoTemplate .this .maybeEmitEvent (new BeforeConvertEvent <T >(objectToSave , collectionName ));
999
989
@@ -1241,7 +1231,7 @@ protected Mono<UpdateResult> doUpdate(final String collectionName, final Query q
1241
1231
private void increaseVersionForUpdateIfNecessary (MongoPersistentEntity <?> persistentEntity , Update update ) {
1242
1232
1243
1233
if (persistentEntity != null && persistentEntity .hasVersionProperty ()) {
1244
- String versionFieldName = persistentEntity .getVersionProperty (). get ().getFieldName ();
1234
+ String versionFieldName = persistentEntity .getRequiredVersionProperty ().getFieldName ();
1245
1235
if (!update .modifies (versionFieldName )) {
1246
1236
update .inc (versionFieldName , 1L );
1247
1237
}
@@ -1254,7 +1244,7 @@ private boolean dbObjectContainsVersionProperty(Document document, MongoPersiste
1254
1244
return false ;
1255
1245
}
1256
1246
1257
- return document .containsKey (persistentEntity .getVersionProperty (). get ().getFieldName ());
1247
+ return document .containsKey (persistentEntity .getRequiredIdProperty ().getFieldName ());
1258
1248
}
1259
1249
1260
1250
/* (non-Javadoc)
@@ -1306,25 +1296,27 @@ public Mono<DeleteResult> remove(Object object, String collection) {
1306
1296
* @param object
1307
1297
* @return
1308
1298
*/
1309
- private Entry <String , Object > extractIdPropertyAndValue (Object object ) {
1299
+ private Pair <String , Object > extractIdPropertyAndValue (Object object ) {
1300
+
1301
+ Assert .notNull (object , "Id cannot be extracted from 'null'." );
1310
1302
1311
1303
Assert .notNull (object , "Id cannot be extracted from 'null'." );
1312
1304
1313
1305
Class <?> objectType = object .getClass ();
1314
1306
1315
1307
if (object instanceof Document ) {
1316
- return Collections . singletonMap (ID_FIELD , ((Document ) object ).get (ID_FIELD )). entrySet (). iterator (). next ( );
1308
+ return Pair . of (ID_FIELD , ((Document ) object ).get (ID_FIELD ));
1317
1309
}
1318
1310
1319
- Optional <? extends MongoPersistentEntity <?>> entity = mappingContext .getPersistentEntity (objectType );
1320
- MongoPersistentProperty idProp = entity .flatMap (PersistentEntity ::getIdProperty ).orElse (null );
1311
+ MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (objectType );
1321
1312
1322
- if (idProp == null ) {
1323
- throw new MappingException ("No id property found for object of type " + objectType );
1313
+ if (entity != null && entity .hasIdProperty ()) {
1314
+
1315
+ MongoPersistentProperty idProperty = entity .getIdProperty ();
1316
+ return Pair .of (idProperty .getFieldName (), entity .getPropertyAccessor (object ).getProperty (idProperty ));
1324
1317
}
1325
1318
1326
- Optional <Object > idValue = entity .get ().getPropertyAccessor (object ).getProperty (idProp );
1327
- return Collections .singletonMap (idProp .getFieldName (), idValue .get ()).entrySet ().iterator ().next ();
1319
+ throw new MappingException ("No id property found for object of type " + objectType );
1328
1320
}
1329
1321
1330
1322
/**
@@ -1335,8 +1327,8 @@ private Entry<String, Object> extractIdPropertyAndValue(Object object) {
1335
1327
*/
1336
1328
private Query getIdQueryFor (Object object ) {
1337
1329
1338
- Entry <String , Object > id = extractIdPropertyAndValue (object );
1339
- return new Query (where (id .getKey ()).is (id .getValue ()));
1330
+ Pair <String , Object > id = extractIdPropertyAndValue (object );
1331
+ return new Query (where (id .getFirst ()).is (id .getSecond ()));
1340
1332
}
1341
1333
1342
1334
/**
@@ -1350,35 +1342,36 @@ private Query getIdInQueryFor(Collection<?> objects) {
1350
1342
Assert .notEmpty (objects , "Cannot create Query for empty collection." );
1351
1343
1352
1344
Iterator <?> it = objects .iterator ();
1353
- Entry <String , Object > firstEntry = extractIdPropertyAndValue (it .next ());
1345
+ Pair <String , Object > firstEntry = extractIdPropertyAndValue (it .next ());
1354
1346
1355
1347
ArrayList <Object > ids = new ArrayList <Object >(objects .size ());
1356
- ids .add (firstEntry .getValue ());
1348
+ ids .add (firstEntry .getSecond ());
1357
1349
1358
1350
while (it .hasNext ()) {
1359
- ids .add (extractIdPropertyAndValue (it .next ()).getValue ());
1351
+ ids .add (extractIdPropertyAndValue (it .next ()).getSecond ());
1360
1352
}
1361
1353
1362
- return new Query (where (firstEntry .getKey ()).in (ids ));
1354
+ return new Query (where (firstEntry .getFirst ()).in (ids ));
1363
1355
}
1364
1356
1365
- private void assertUpdateableIdIfNotSet (Object entity ) {
1357
+ private void assertUpdateableIdIfNotSet (Object value ) {
1366
1358
1367
- Optional <? extends MongoPersistentEntity <?>> persistentEntity = mappingContext
1368
- .getPersistentEntity (entity .getClass ());
1369
- Optional <MongoPersistentProperty > idProperty = persistentEntity .isPresent () ? persistentEntity .get ().getIdProperty ()
1370
- : Optional .empty ();
1359
+ MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (value .getClass ());
1371
1360
1372
- if (!idProperty .isPresent ()) {
1373
- return ;
1374
- }
1361
+ if (entity != null && entity .hasIdProperty ()) {
1375
1362
1376
- Optional <Object > idValue = persistentEntity .get ().getPropertyAccessor (entity ).getProperty (idProperty .get ());
1363
+ MongoPersistentProperty property = entity .getRequiredIdProperty ();
1364
+ Object propertyValue = entity .getPropertyAccessor (value ).getProperty (property );
1377
1365
1378
- if (!idValue .isPresent () && !MongoSimpleTypes .AUTOGENERATED_ID_TYPES .contains (idProperty .get ().getType ())) {
1379
- throw new InvalidDataAccessApiUsageException (
1380
- String .format ("Cannot autogenerate id of type %s for entity of type %s!" ,
1381
- idProperty .get ().getType ().getName (), entity .getClass ().getName ()));
1366
+ if (propertyValue != null ) {
1367
+ return ;
1368
+ }
1369
+
1370
+ if (!MongoSimpleTypes .AUTOGENERATED_ID_TYPES .contains (property .getType ())) {
1371
+ throw new InvalidDataAccessApiUsageException (
1372
+ String .format ("Cannot autogenerate id of type %s for entity of type %s!" , property .getType ().getName (),
1373
+ value .getClass ().getName ()));
1374
+ }
1382
1375
}
1383
1376
}
1384
1377
@@ -1557,7 +1550,7 @@ protected Mono<MongoCollection<Document>> doCreateCollection(final String collec
1557
1550
protected <T > Mono <T > doFindOne (String collectionName , Document query , Document fields , Class <T > entityClass ,
1558
1551
Collation collation ) {
1559
1552
1560
- Optional <? extends MongoPersistentEntity <?> > entity = mappingContext .getPersistentEntity (entityClass );
1553
+ MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (entityClass );
1561
1554
Document mappedQuery = queryMapper .getMappedObject (query , entity );
1562
1555
Document mappedFields = fields == null ? null : queryMapper .getMappedObject (fields , entity );
1563
1556
@@ -1607,7 +1600,7 @@ protected <T> Flux<T> doFind(String collectionName, Document query, Document fie
1607
1600
protected <S , T > Flux <T > doFind (String collectionName , Document query , Document fields , Class <S > entityClass ,
1608
1601
FindPublisherPreparer preparer , DocumentCallback <T > objectCallback ) {
1609
1602
1610
- Optional <? extends MongoPersistentEntity <?> > entity = mappingContext .getPersistentEntity (entityClass );
1603
+ MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (entityClass );
1611
1604
1612
1605
Document mappedFields = queryMapper .getMappedFields (fields , entity );
1613
1606
Document mappedQuery = queryMapper .getMappedObject (query , entity );
@@ -1654,7 +1647,7 @@ protected <T> Mono<T> doFindAndRemove(String collectionName, Document query, Doc
1654
1647
serializeToJsonSafely (query ), fields , sort , entityClass , collectionName ));
1655
1648
}
1656
1649
1657
- Optional <? extends MongoPersistentEntity <?> > entity = mappingContext .getPersistentEntity (entityClass );
1650
+ MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (entityClass );
1658
1651
1659
1652
return executeFindOneInternal (
1660
1653
new FindAndRemoveCallback (queryMapper .getMappedObject (query , entity ), fields , sort , collation ),
@@ -1666,11 +1659,11 @@ protected <T> Mono<T> doFindAndModify(String collectionName, Document query, Doc
1666
1659
1667
1660
FindAndModifyOptions optionsToUse = options != null ? options : new FindAndModifyOptions ();
1668
1661
1669
- Optional <? extends MongoPersistentEntity <?> > entity = mappingContext .getPersistentEntity (entityClass );
1662
+ MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (entityClass );
1670
1663
1671
1664
return Mono .defer (() -> {
1672
1665
1673
- increaseVersionForUpdateIfNecessary (entity . get () , update );
1666
+ increaseVersionForUpdateIfNecessary (entity , update );
1674
1667
1675
1668
Document mappedQuery = queryMapper .getMappedObject (query , entity );
1676
1669
Document mappedUpdate = updateMapper .getMappedObject (update .getUpdateObject (), entity );
@@ -1721,11 +1714,11 @@ private void populateIdIfNecessary(Object savedObject, Object id) {
1721
1714
MongoPersistentEntity <?> entity = mappingContext .getRequiredPersistentEntity (savedObject .getClass ());
1722
1715
PersistentPropertyAccessor accessor = entity .getPropertyAccessor (savedObject );
1723
1716
1724
- if (accessor .getProperty (idProp ). isPresent () ) {
1717
+ if (accessor .getProperty (idProp ) != null ) {
1725
1718
return ;
1726
1719
}
1727
1720
1728
- new ConvertingPropertyAccessor (accessor , conversionService ).setProperty (idProp , Optional . ofNullable ( id ) );
1721
+ new ConvertingPropertyAccessor (accessor , conversionService ).setProperty (idProp , id );
1729
1722
}
1730
1723
1731
1724
private MongoCollection <Document > getAndPrepareCollection (MongoDatabase db , String collectionName ) {
@@ -1896,13 +1889,13 @@ private static RuntimeException potentiallyConvertRuntimeException(RuntimeExcept
1896
1889
}
1897
1890
1898
1891
private MongoPersistentEntity <?> getPersistentEntity (Class <?> type ) {
1899
- return type == null ? null : mappingContext .getPersistentEntity (type ). orElse ( null ) ;
1892
+ return type == null ? null : mappingContext .getPersistentEntity (type );
1900
1893
}
1901
1894
1902
1895
private MongoPersistentProperty getIdPropertyFor (Class <?> type ) {
1903
1896
1904
- Optional <? extends MongoPersistentEntity <?> > persistentEntity = mappingContext .getPersistentEntity (type );
1905
- return persistentEntity . flatMap ( PersistentEntity :: getIdProperty ). orElse ( null ) ;
1897
+ MongoPersistentEntity <?> persistentEntity = mappingContext .getPersistentEntity (type );
1898
+ return persistentEntity != null ? persistentEntity . getIdProperty () : null ;
1906
1899
}
1907
1900
1908
1901
private <T > String determineEntityCollectionName (T obj ) {
@@ -1976,7 +1969,7 @@ private void initializeVersionProperty(Object entity) {
1976
1969
if (mongoPersistentEntity != null && mongoPersistentEntity .hasVersionProperty ()) {
1977
1970
ConvertingPropertyAccessor accessor = new ConvertingPropertyAccessor (
1978
1971
mongoPersistentEntity .getPropertyAccessor (entity ), mongoConverter .getConversionService ());
1979
- accessor .setProperty (mongoPersistentEntity .getVersionProperty (). get (), Optional . of ( 0 ) );
1972
+ accessor .setProperty (mongoPersistentEntity .getRequiredVersionProperty (), 0 );
1980
1973
}
1981
1974
}
1982
1975
@@ -2349,9 +2342,9 @@ private static List<? extends Document> toDocuments(final Collection<? extends D
2349
2342
static class NoOpDbRefResolver implements DbRefResolver {
2350
2343
2351
2344
@ Override
2352
- public Optional < Object > resolveDbRef (MongoPersistentProperty property , DBRef dbref , DbRefResolverCallback callback ,
2345
+ public Object resolveDbRef (MongoPersistentProperty property , DBRef dbref , DbRefResolverCallback callback ,
2353
2346
DbRefProxyHandler proxyHandler ) {
2354
- return Optional . empty () ;
2347
+ return null ;
2355
2348
}
2356
2349
2357
2350
@ Override
0 commit comments