diff --git a/pom.xml b/pom.xml
index 50a350594..614dc2ba3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -31,6 +31,7 @@
2.0.0.BUILD-SNAPSHOT
3.2.2
1.2.0
+ 2.2.17
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
index f3e11c048..f474a5fbe 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
@@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.SerializationUtils.*;
+import static org.springframework.data.util.Optionals.*;
import java.io.IOException;
import java.util.*;
@@ -94,6 +95,8 @@ import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.util.MongoClientVersion;
import org.springframework.data.util.CloseableIterator;
+import org.springframework.data.util.Optionals;
+import org.springframework.data.util.Pair;
import org.springframework.jca.cci.core.ConnectionCallback;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@@ -355,7 +358,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
public CloseableIterator doInCollection(MongoCollection collection)
throws MongoException, DataAccessException {
- MongoPersistentEntity> persistentEntity = mappingContext.getPersistentEntity(entityType);
+ MongoPersistentEntity> persistentEntity = mappingContext.getRequiredPersistentEntity(entityType);
Document mappedFields = queryMapper.getMappedFields(query.getFieldsObject(), persistentEntity);
Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), persistentEntity);
@@ -441,7 +444,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
Assert.notNull(query, "Query must not be null!");
- Document queryObject = queryMapper.getMappedObject(query.getQueryObject(), null);
+ Document queryObject = queryMapper.getMappedObject(query.getQueryObject(), Optional.empty());
Document sortObject = query.getSortObject();
Document fieldsObject = query.getFieldsObject();
@@ -633,9 +636,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
}
public T findById(Object id, Class entityClass, String collectionName) {
- MongoPersistentEntity> persistentEntity = mappingContext.getPersistentEntity(entityClass);
- MongoPersistentProperty idProperty = persistentEntity == null ? null : persistentEntity.getIdProperty();
- String idKey = idProperty == null ? ID_FIELD : idProperty.getName();
+
+ String idKey = mappingContext.getPersistentEntity(entityClass)//
+ .flatMap(it -> it.getIdProperty())//
+ .map(it -> it.getName()).orElse(ID_FIELD);
+
return doFindOne(collectionName, new Document(idKey, id), null, entityClass);
}
@@ -755,13 +760,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
final Document document = query == null ? null
: queryMapper.getMappedObject(query.getQueryObject(),
- entityClass == null ? null : mappingContext.getPersistentEntity(entityClass));
+ Optional.ofNullable(entityClass).flatMap(it -> mappingContext.getPersistentEntity(entityClass)));
- return execute(collectionName, new CollectionCallback() {
- public Long doInCollection(MongoCollection collection) throws MongoException, DataAccessException {
- return collection.count(document);
- }
- });
+ return execute(collectionName, (CollectionCallback) collection -> collection.count(document));
}
/*
@@ -878,13 +879,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
private void initializeVersionProperty(Object entity) {
- MongoPersistentEntity> mongoPersistentEntity = getPersistentEntity(entity.getClass());
+ Optional extends MongoPersistentEntity>> persistentEntity = getPersistentEntity(entity.getClass());
- if (mongoPersistentEntity != null && mongoPersistentEntity.hasVersionProperty()) {
- ConvertingPropertyAccessor accessor = new ConvertingPropertyAccessor(
- mongoPersistentEntity.getPropertyAccessor(entity), mongoConverter.getConversionService());
- accessor.setProperty(mongoPersistentEntity.getVersionProperty(), 0);
- }
+ ifAllPresent(persistentEntity, persistentEntity.flatMap(it -> it.getVersionProperty()), (l, r) -> {
+ ConvertingPropertyAccessor accessor = new ConvertingPropertyAccessor(l.getPropertyAccessor(entity),
+ mongoConverter.getConversionService());
+ accessor.setProperty(r, Optional.of(0));
+ });
}
public void insert(Collection extends Object> batchToSave, Class> entityClass) {
@@ -909,11 +910,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
continue;
}
- MongoPersistentEntity> entity = mappingContext.getPersistentEntity(element.getClass());
-
- if (entity == null) {
- throw new InvalidDataAccessApiUsageException("No PersistentEntity information found for " + element.getClass());
- }
+ MongoPersistentEntity> entity = mappingContext.getRequiredPersistentEntity(element.getClass());
String collection = entity.getCollection();
List collectionElements = elementsByCollection.get(collection);
@@ -970,35 +967,26 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
Assert.notNull(objectToSave, "Object to save must not be null!");
Assert.hasText(collectionName, "Collection name must not be null or empty!");
- MongoPersistentEntity> mongoPersistentEntity = getPersistentEntity(objectToSave.getClass());
+ Optional extends MongoPersistentEntity>> entity = getPersistentEntity(objectToSave.getClass());
+ Optional versionProperty = entity.flatMap(it -> it.getVersionProperty());
- // No optimistic locking -> simple save
- if (mongoPersistentEntity == null || !mongoPersistentEntity.hasVersionProperty()) {
- doSave(collectionName, objectToSave, this.mongoConverter);
- return;
- }
-
- doSaveVersioned(objectToSave, mongoPersistentEntity, collectionName);
+ mapIfAllPresent(entity, versionProperty, //
+ (l, r) -> doSaveVersioned(objectToSave, l, collectionName))//
+ .orElseGet(() -> doSave(collectionName, objectToSave, this.mongoConverter));
}
- private void doSaveVersioned(T objectToSave, MongoPersistentEntity> entity, String collectionName) {
+ private T doSaveVersioned(T objectToSave, MongoPersistentEntity> entity, String collectionName) {
ConvertingPropertyAccessor convertingAccessor = new ConvertingPropertyAccessor(
entity.getPropertyAccessor(objectToSave), mongoConverter.getConversionService());
- MongoPersistentProperty idProperty = entity.getIdProperty();
- MongoPersistentProperty versionProperty = entity.getVersionProperty();
+ Optional versionProperty = entity.getVersionProperty();
+ Optional versionNumber = versionProperty.flatMap(it -> convertingAccessor.getProperty(it, Number.class));
- Object version = convertingAccessor.getProperty(versionProperty);
- Number versionNumber = convertingAccessor.getProperty(versionProperty, Number.class);
-
- // Fresh instance -> initialize version property
- if (version == null) {
- doInsert(collectionName, objectToSave, this.mongoConverter);
- } else {
+ return mapIfAllPresent(versionProperty, versionNumber, (property, number) -> {
// Bump version number
- convertingAccessor.setProperty(versionProperty, versionNumber.longValue() + 1);
+ convertingAccessor.setProperty(property, Optional.of(number.longValue() + 1));
maybeEmitEvent(new BeforeConvertEvent(objectToSave, collectionName));
assertUpdateableIdIfNotSet(objectToSave);
@@ -1011,8 +999,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
Update update = Update.fromDocument(document, ID_FIELD);
// Create query for entity with the id and old version
- Object id = convertingAccessor.getProperty(idProperty);
- Query query = new Query(Criteria.where(idProperty.getName()).is(id).and(versionProperty.getName()).is(version));
+ MongoPersistentProperty idProperty = entity.getRequiredIdProperty();
+ Object id = convertingAccessor.getProperty(idProperty)
+ .orElseThrow(() -> new IllegalStateException("Required id not found!"));
+ Query query = new Query(Criteria.where(idProperty.getName()).is(id).and(property.getName()).is(number));
UpdateResult result = doUpdate(collectionName, query, update, objectToSave.getClass(), false, false);
@@ -1022,10 +1012,16 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
versionNumber, collectionName));
}
maybeEmitEvent(new AfterSaveEvent(objectToSave, document, collectionName));
- }
+
+ return objectToSave;
+
+ }).orElseGet(() -> {
+ doInsert(collectionName, objectToSave, this.mongoConverter);
+ return objectToSave;
+ });
}
- protected void doSave(String collectionName, T objectToSave, MongoWriter writer) {
+ protected T doSave(String collectionName, T objectToSave, MongoWriter writer) {
maybeEmitEvent(new BeforeConvertEvent(objectToSave, collectionName));
assertUpdateableIdIfNotSet(objectToSave);
@@ -1037,6 +1033,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
populateIdIfNecessary(objectToSave, id);
maybeEmitEvent(new AfterSaveEvent(objectToSave, dbDoc, collectionName));
+
+ return objectToSave;
}
protected Object insertDocument(final String collectionName, final Document document, final Class> entityClass) {
@@ -1174,7 +1172,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
public UpdateResult doInCollection(MongoCollection collection)
throws MongoException, DataAccessException {
- MongoPersistentEntity> entity = entityClass == null ? null : getPersistentEntity(entityClass);
+ Optional extends MongoPersistentEntity>> entity = entityClass == null ? null
+ : getPersistentEntity(entityClass);
increaseVersionForUpdateIfNecessary(entity, update);
@@ -1210,23 +1209,23 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
});
}
- private void increaseVersionForUpdateIfNecessary(MongoPersistentEntity> persistentEntity, Update update) {
+ private void increaseVersionForUpdateIfNecessary(Optional extends MongoPersistentEntity>> persistentEntity,
+ Update update) {
- if (persistentEntity != null && persistentEntity.hasVersionProperty()) {
- String versionFieldName = persistentEntity.getVersionProperty().getFieldName();
+ ifAllPresent(persistentEntity, persistentEntity.flatMap(it -> it.getVersionProperty()), (entity, property) -> {
+ String versionFieldName = property.getFieldName();
if (!update.modifies(versionFieldName)) {
update.inc(versionFieldName, 1L);
}
- }
+ });
}
- private boolean documentContainsVersionProperty(Document document, MongoPersistentEntity> persistentEntity) {
+ private boolean documentContainsVersionProperty(Document document,
+ Optional extends MongoPersistentEntity>> persistentEntity) {
- if (persistentEntity == null || !persistentEntity.hasVersionProperty()) {
- return false;
- }
-
- return document.containsKey(persistentEntity.getVersionProperty().getFieldName());
+ return mapIfAllPresent(persistentEntity, persistentEntity.flatMap(it -> it.getVersionProperty()), //
+ (entity, property) -> document.containsKey(property.getFieldName()))//
+ .orElse(false);
}
public DeleteResult remove(Object object) {
@@ -1256,25 +1255,21 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
* @param object
* @return
*/
- private Entry extractIdPropertyAndValue(Object object) {
+ private Pair> extractIdPropertyAndValue(Object object) {
Assert.notNull(object, "Id cannot be extracted from 'null'.");
Class> objectType = object.getClass();
if (object instanceof Document) {
- return Collections.singletonMap(ID_FIELD, ((Document) object).get(ID_FIELD)).entrySet().iterator().next();
+ return Pair.of(ID_FIELD, Optional.ofNullable(((Document) object).get(ID_FIELD)));
}
- MongoPersistentEntity> entity = mappingContext.getPersistentEntity(objectType);
- MongoPersistentProperty idProp = entity == null ? null : entity.getIdProperty();
+ Optional extends MongoPersistentEntity>> entity = mappingContext.getPersistentEntity(objectType);
+ return mapIfAllPresent(entity, entity.flatMap(it -> it.getIdProperty()), //
- if (idProp == null || entity == null) {
- throw new MappingException("No id property found for object of type " + objectType);
- }
-
- Object idValue = entity.getPropertyAccessor(object).getProperty(idProp);
- return Collections.singletonMap(idProp.getFieldName(), idValue).entrySet().iterator().next();
+ (l, r) -> Pair.of(r.getFieldName(), l.getPropertyAccessor(object).getProperty(r)))//
+ .orElseThrow(() -> new MappingException("No id property found for object of type " + objectType));
}
/**
@@ -1285,8 +1280,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
*/
private Query getIdQueryFor(Object object) {
- Entry id = extractIdPropertyAndValue(object);
- return new Query(where(id.getKey()).is(id.getValue()));
+ Pair> id = extractIdPropertyAndValue(object);
+ return new Query(where(id.getFirst()).is(id.getSecond().orElse(null)));
}
/**
@@ -1300,34 +1295,38 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
Assert.notEmpty(objects, "Cannot create Query for empty collection.");
Iterator> it = objects.iterator();
- Entry firstEntry = extractIdPropertyAndValue(it.next());
+ Pair> pair = extractIdPropertyAndValue(it.next());
ArrayList