diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAction.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAction.java
index 140ed2ed4..e1ffbad73 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAction.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAction.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2011 the original author or authors.
+ * Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,59 +13,53 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.springframework.data.mongodb.core;
+import org.springframework.util.Assert;
+
import com.mongodb.DBObject;
import com.mongodb.WriteConcern;
/**
* Represents an action taken against the collection. Used by {@link WriteConcernResolver} to determine a custom
- * WriteConcern based on this information.
- *
- * Properties that will always be not-null are collectionName and defaultWriteConcern. The EntityClass is null only for
- * the MongoActionOperaton.INSERT_LIST.
- *
+ * {@link WriteConcern} based on this information.
*
*
INSERT, SAVE have null query
*
REMOVE has null document
- *
INSERT_LIST has null entityClass, document, and query
+ *
INSERT_LIST has null entityType, document, and query
*
*
* @author Mark Pollack
- *
+ * @author Oliver Gierke
*/
public class MongoAction {
- private String collectionName;
-
- private WriteConcern defaultWriteConcern;
-
- private Class> entityClass;
-
- private MongoActionOperation mongoActionOperation;
-
- private DBObject query;
-
- private DBObject document;
+ private final String collectionName;
+ private final WriteConcern defaultWriteConcern;
+ private final Class> entityType;
+ private final MongoActionOperation mongoActionOperation;
+ private final DBObject query;
+ private final DBObject document;
/**
- * Create an instance of a MongoAction
+ * Create an instance of a {@link MongoAction}.
*
- * @param defaultWriteConcern the default write concern
+ * @param defaultWriteConcern the default write concern.
* @param mongoActionOperation action being taken against the collection
- * @param collectionName the collection name
- * @param entityClass the POJO that is being operated against
+ * @param collectionName the collection name, must not be {@literal null} or empty.
+ * @param entityType the POJO that is being operated against
* @param document the converted DBObject from the POJO or Spring Update object
* @param query the converted DBOjbect from the Spring Query object
*/
public MongoAction(WriteConcern defaultWriteConcern, MongoActionOperation mongoActionOperation,
- String collectionName, Class> entityClass, DBObject document, DBObject query) {
- super();
+ String collectionName, Class> entityType, DBObject document, DBObject query) {
+
+ Assert.hasText(collectionName, "Collection name must not be null or empty!");
+
this.defaultWriteConcern = defaultWriteConcern;
this.mongoActionOperation = mongoActionOperation;
this.collectionName = collectionName;
- this.entityClass = entityClass;
+ this.entityType = entityType;
this.query = query;
this.document = document;
}
@@ -78,8 +72,16 @@ public class MongoAction {
return defaultWriteConcern;
}
+ /**
+ * @deprecated use {@link #getEntityType()} instead.
+ */
+ @Deprecated
public Class> getEntityClass() {
- return entityClass;
+ return entityType;
+ }
+
+ public Class> getEntityType() {
+ return entityType;
}
public MongoActionOperation getMongoActionOperation() {
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoActionOperation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoActionOperation.java
index 7dd3f6ca6..c12752152 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoActionOperation.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoActionOperation.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2011 the original author or authors.
+ * Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,8 +20,8 @@ package org.springframework.data.mongodb.core;
* for a given mutating operation
*
* @author Mark Pollack
+ * @author Oliver Gierke
* @see MongoAction
- *
*/
public enum MongoActionOperation {
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 2dc4a5e9f..2dd85c7f4 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
@@ -116,7 +116,7 @@ import com.mongodb.util.JSONParseException;
public class MongoTemplate implements MongoOperations, ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(MongoTemplate.class);
- private static final String ID = "_id";
+ private static final String ID_FIELD = "_id";
private static final WriteResultChecking DEFAULT_WRITE_RESULT_CHECKING = WriteResultChecking.NONE;
private static final Collection ITERABLE_CLASSES;
@@ -130,32 +130,16 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
ITERABLE_CLASSES = Collections.unmodifiableCollection(iterableClasses);
}
- /*
- * WriteConcern to be used for write operations if it has been specified.
- * Otherwise we should not use a WriteConcern defaulting to the one set for
- * the DB or Collection.
- */
- private WriteConcern writeConcern = null;
-
- private WriteConcernResolver writeConcernResolver = new DefaultWriteConcernResolver();
-
- /*
- * WriteResultChecking to be used for write operations if it has been
- * specified. Otherwise we should not do any checking.
- */
- private WriteResultChecking writeResultChecking = WriteResultChecking.NONE;
-
- /**
- * Set the ReadPreference when operating on a collection. See {@link #prepareCollection(DBCollection)}
- */
- private ReadPreference readPreference = null;
-
private final MongoConverter mongoConverter;
private final MappingContext extends MongoPersistentEntity>, MongoPersistentProperty> mappingContext;
private final MongoDbFactory mongoDbFactory;
private final MongoExceptionTranslator exceptionTranslator = new MongoExceptionTranslator();
private final QueryMapper mapper;
+ private WriteConcern writeConcern;
+ private WriteConcernResolver writeConcernResolver = DefaultWriteConcernResolver.INSTANCE;
+ private WriteResultChecking writeResultChecking = WriteResultChecking.NONE;
+ private ReadPreference readPreference;
private ApplicationEventPublisher eventPublisher;
private ResourceLoader resourceLoader;
private MongoPersistentEntityIndexCreator indexCreator;
@@ -163,8 +147,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
/**
* Constructor used for a basic template configuration
*
- * @param mongo
- * @param databaseName
+ * @param mongo must not be {@literal null}.
+ * @param databaseName must not be {@literal null} or empty.
*/
public MongoTemplate(Mongo mongo, String databaseName) {
this(new SimpleMongoDbFactory(mongo, databaseName), null);
@@ -174,8 +158,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
* Constructor used for a template configuration with user credentials in the form of
* {@link org.springframework.data.authentication.UserCredentials}
*
- * @param mongo
- * @param databaseName
+ * @param mongo must not be {@literal null}.
+ * @param databaseName must not be {@literal null} or empty.
* @param userCredentials
*/
public MongoTemplate(Mongo mongo, String databaseName, UserCredentials userCredentials) {
@@ -183,9 +167,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
/**
- * Constructor used for a basic template configuration
+ * Constructor used for a basic template configuration.
*
- * @param mongoDbFactory
+ * @param mongoDbFactory must not be {@literal null}.
*/
public MongoTemplate(MongoDbFactory mongoDbFactory) {
this(mongoDbFactory, null);
@@ -194,7 +178,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
/**
* Constructor used for a basic template configuration.
*
- * @param mongoDbFactory
+ * @param mongoDbFactory must not be {@literal null}.
* @param mongoConverter
*/
public MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter) {
@@ -228,7 +212,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
/**
- * Configures the {@link WriteConcern} to be used with the template.
+ * Configures the {@link WriteConcern} to be used with the template. If none is configured the {@link WriteConcern}
+ * configured on the {@link MongoDbFactory} will apply. If you configured a {@link Mongo} instance no
+ * {@link WriteConcern} will be used.
*
* @param writeConcern
*/
@@ -276,7 +262,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
* can be found we manually add the internally created one as {@link ApplicationListener} to make sure indexes get
* created appropriately for entity types persisted through this {@link MongoTemplate} instance.
*
- * @param context
+ * @param context must not be {@literal null}.
*/
private void prepareIndexCreator(ApplicationContext context) {
@@ -512,7 +498,7 @@ 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 : idProperty.getName();
+ String idKey = idProperty == null ? ID_FIELD : idProperty.getName();
return doFindOne(collectionName, new BasicDBObject(idKey, id), null, entityClass);
}
@@ -799,7 +785,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
this.mongoConverter.write(objectToSave, dbObject);
maybeEmitEvent(new BeforeSaveEvent(objectToSave, dbObject));
- Update update = Update.fromDBObject(dbObject, ID);
+ Update update = Update.fromDBObject(dbObject, ID_FIELD);
updateFirst(query, update, objectToSave.getClass());
maybeEmitEvent(new AfterSaveEvent(objectToSave, dbObject));
@@ -834,21 +820,17 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class> entityClass) {
if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("insert DBObject containing fields: " + dbDoc.keySet() + " in collection: " + collectionName);
+ LOGGER.debug("Inserting DBObject containing fields: " + dbDoc.keySet() + " in collection: " + collectionName);
}
return execute(collectionName, new CollectionCallback