diff --git a/spring-data-mongodb-cross-store/src/test/resources/META-INF/spring/applicationContext.xml b/spring-data-mongodb-cross-store/src/test/resources/META-INF/spring/applicationContext.xml
index 64f17ab36..892b69129 100644
--- a/spring-data-mongodb-cross-store/src/test/resources/META-INF/spring/applicationContext.xml
+++ b/spring-data-mongodb-cross-store/src/test/resources/META-INF/spring/applicationContext.xml
@@ -28,7 +28,6 @@
-
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java
index 6403231bf..46b800dd0 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java
@@ -23,7 +23,6 @@ import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
-import org.bson.types.ObjectId;
import org.springframework.data.document.mongodb.index.IndexDefinition;
import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update;
@@ -40,18 +39,11 @@ import org.springframework.data.document.mongodb.query.Update;
public interface MongoOperations {
/**
- * The default collection name used by this template.
+ * The collection name used for the specified class by this template.
*
* @return
*/
- String getDefaultCollectionName();
-
- /**
- * The default collection used by this template.
- *
- * @return The default collection used by this template
- */
- DBCollection getDefaultCollection();
+ String getCollectionName(Class> clazz);
/**
* Execute the a MongoDB command expressed as a JSON string. This will call the method
@@ -83,15 +75,16 @@ public interface MongoOperations {
T execute(DbCallback action);
/**
- * Executes the given {@link CollectionCallback} on the default collection.
+ * Executes the given {@link CollectionCallback} on the entity collection of the specified class.
*
* Allows for returning a result object, that is a domain object or a collection of domain objects.
*
+ * @param entityClass class that determines the collection to use
* @param return type
* @param action callback object that specifies the MongoDB action
* @return a result object returned by the action or null
*/
- T execute(CollectionCallback action);
+ T execute(Class> entityClass, CollectionCallback action);
/**
* Executes the given {@link CollectionCallback} on the collection of the given name.
@@ -219,9 +212,10 @@ public interface MongoOperations {
* Ensure that an index for the provided {@link IndexDefinition} exists for the default collection.
* If not it will be created.
*
- * @param index
+ * @param entityClass class that determines the collection to use
+ * @param indexDefinition
*/
- void ensureIndex(IndexDefinition indexDefinition);
+ void ensureIndex(Class> entityClass, IndexDefinition indexDefinition);
/**
* Ensure that an index for the provided {@link IndexDefinition} exists. If not it will be
@@ -651,11 +645,12 @@ public interface MongoOperations {
* Updates the first object that is found in the default collection that matches the query document
* with the provided updated document.
*
+ * @param entityClass class that determines the collection to use
* @param queryDoc the query document that specifies the criteria used to select a record to be updated
* @param updateDoc the update document that contains the updated object or $ operators to manipulate the
* existing object.
*/
- WriteResult updateFirst(Query query, Update update);
+ WriteResult updateFirst(Class> entityClass, Query query, Update update);
/**
* Updates the first object that is found in the specified collection that matches the query document criteria
@@ -673,11 +668,12 @@ public interface MongoOperations {
* Updates all objects that are found in the default collection that matches the query document criteria
* with the provided updated document.
*
+ * @param entityClass class that determines the collection to use
* @param queryDoc the query document that specifies the criteria used to select a record to be updated
* @param updateDoc the update document that contains the updated object or $ operators to manipulate the
* existing object.
*/
- WriteResult updateMulti(Query query, Update update);
+ WriteResult updateMulti(Class> entityClass, Query query, Update update);
/**
* Updates all objects that are found in the specified collection that matches the query document criteria
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java
index d77679f4d..56bb403ea 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java
@@ -25,12 +25,10 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;
import com.mongodb.DB;
@@ -45,7 +43,6 @@ import com.mongodb.util.JSON;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId;
-import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.core.convert.ConversionFailedException;
@@ -83,22 +80,22 @@ import org.springframework.util.Assert;
* @author Mark Pollack
* @author Oliver Gierke
*/
-public class MongoTemplate implements InitializingBean, MongoOperations, ApplicationEventPublisherAware {
+public class MongoTemplate implements MongoOperations, ApplicationEventPublisherAware {
private static final Log LOGGER = LogFactory.getLog(MongoTemplate.class);
private static final String ID = "_id";
/*
- * 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.
- */
+ * 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;
/*
- * WriteResultChecking to be used for write operations if it has been specified. Otherwise
- * we should not do any checking.
- */
+ * WriteResultChecking to be used for write operations if it has been specified. Otherwise
+ * we should not do any checking.
+ */
private WriteResultChecking writeResultChecking = WriteResultChecking.NONE;
private final MongoConverter mongoConverter;
@@ -107,7 +104,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
private final MongoExceptionTranslator exceptionTranslator = new MongoExceptionTranslator();
private final QueryMapper mapper;
- private String defaultCollectionName;
private String databaseName;
private String username;
private String password;
@@ -120,34 +116,22 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* @param databaseName
*/
public MongoTemplate(Mongo mongo, String databaseName) {
- this(mongo, databaseName, null, null, null, null);
+ this(mongo, databaseName, null, null, null);
}
/**
- * Constructor used for a basic template configuration with a default collection name
+ * Constructor used for a template configuration with a custom {@link org.springframework.data.document.mongodb.convert.MongoConverter}
*
* @param mongo
* @param databaseName
- * @param defaultCollectionName
- */
- public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName) {
- this(mongo, databaseName, defaultCollectionName, null, null, null);
- }
-
- /**
- * Constructor used for a template configuration with a default collection name and a custom {@link org.springframework.data.document.mongodb.convert.MongoConverter}
- *
- * @param mongo
- * @param databaseName
- * @param defaultCollectionName
* @param mongoConverter
*/
- public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName, MongoConverter mongoConverter) {
- this(mongo, databaseName, defaultCollectionName, mongoConverter, null, null);
+ public MongoTemplate(Mongo mongo, String databaseName, MongoConverter mongoConverter) {
+ this(mongo, databaseName, mongoConverter, null, null);
}
/**
- * Constructor used for a template configuration with a default collection name and a custom {@link MongoConverter}
+ * Constructor used for a template configuration with a custom {@link MongoConverter}
* and with a specific {@link com.mongodb.WriteConcern} to be used for all database write operations
*
* @param mongo
@@ -158,12 +142,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* @param writeResultChecking
*/
@SuppressWarnings({"unchecked"})
- MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName, MongoConverter mongoConverter, WriteConcern writeConcern, WriteResultChecking writeResultChecking) {
+ MongoTemplate(Mongo mongo, String databaseName, MongoConverter mongoConverter, WriteConcern writeConcern, WriteResultChecking writeResultChecking) {
Assert.notNull(mongo);
Assert.notNull(databaseName);
- this.defaultCollectionName = defaultCollectionName;
this.mongo = mongo;
this.databaseName = databaseName;
this.writeConcern = writeConcern;
@@ -211,15 +194,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
this.password = password;
}
- /**
- * Sets the name of the default collection to be used.
- *
- * @param defaultCollectionName
- */
- public void setDefaultCollectionName(String defaultCollectionName) {
- this.defaultCollectionName = defaultCollectionName;
- }
-
/**
* Sets the database name to be used.
*
@@ -240,34 +214,22 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#getDefaultCollectionName()
- */
- public String getDefaultCollectionName() {
- return defaultCollectionName;
- }
-
- /* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#getDefaultCollection()
- */
- public DBCollection getDefaultCollection() {
-
- return execute(new DbCallback() {
- public DBCollection doInDB(DB db) throws MongoException, DataAccessException {
- return db.getCollection(getDefaultCollectionName());
- }
- });
+ * @see org.springframework.data.document.mongodb.MongoOperations#getDefaultCollectionName()
+ */
+ public String getCollectionName(Class> clazz) {
+ return this.determineCollectionName(clazz);
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#executeCommand(java.lang.String)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#executeCommand(java.lang.String)
+ */
public CommandResult executeCommand(String jsonCommand) {
return executeCommand((DBObject) JSON.parse(jsonCommand));
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#executeCommand(com.mongodb.DBObject)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#executeCommand(com.mongodb.DBObject)
+ */
public CommandResult executeCommand(final DBObject command) {
CommandResult result = execute(new DbCallback() {
@@ -288,8 +250,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.DBCallback)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.DBCallback)
+ */
public T execute(DbCallback action) {
Assert.notNull(action);
@@ -303,15 +265,15 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.CollectionCallback)
- */
- public T execute(CollectionCallback callback) {
- return execute(getDefaultCollectionName(), callback);
+ * @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.CollectionCallback)
+ */
+ public T execute(Class> entityClass, CollectionCallback callback) {
+ return execute(determineCollectionName(entityClass), callback);
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.CollectionCallback, java.lang.String)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.CollectionCallback, java.lang.String)
+ */
public T execute(String collectionName, CollectionCallback callback) {
Assert.notNull(callback);
@@ -384,8 +346,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#executeInSession(org.springframework.data.document.mongodb.DBCallback)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#executeInSession(org.springframework.data.document.mongodb.DBCallback)
+ */
public T executeInSession(final DbCallback action) {
return execute(new DbCallback() {
@@ -401,22 +363,22 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#createCollection(java.lang.String)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#createCollection(java.lang.String)
+ */
public DBCollection createCollection(final String collectionName) {
return doCreateCollection(collectionName, new BasicDBObject());
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#createCollection(java.lang.String, org.springframework.data.document.mongodb.CollectionOptions)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#createCollection(java.lang.String, org.springframework.data.document.mongodb.CollectionOptions)
+ */
public DBCollection createCollection(final String collectionName, final CollectionOptions collectionOptions) {
return doCreateCollection(collectionName, convertToDbObject(collectionOptions));
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#getCollection(java.lang.String)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#getCollection(java.lang.String)
+ */
public DBCollection getCollection(final String collectionName) {
return execute(new DbCallback() {
public DBCollection doInDB(DB db) throws MongoException, DataAccessException {
@@ -427,8 +389,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#collectionExists(java.lang.String)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#collectionExists(java.lang.String)
+ */
public boolean collectionExists(final String collectionName) {
return execute(new DbCallback() {
public Boolean doInDB(DB db) throws MongoException, DataAccessException {
@@ -438,8 +400,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#dropCollection(java.lang.String)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#dropCollection(java.lang.String)
+ */
public void dropCollection(String collectionName) {
execute(collectionName, new CollectionCallback() {
@@ -452,8 +414,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
// Indexing methods
- public void ensureIndex(IndexDefinition indexDefinition) {
- ensureIndex(getDefaultCollectionName(), indexDefinition);
+ public void ensureIndex(Class> entityClass, IndexDefinition indexDefinition) {
+ ensureIndex(determineCollectionName(entityClass), indexDefinition);
}
public void ensureIndex(String collectionName, final IndexDefinition indexDefinition) {
@@ -473,12 +435,12 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
// Find methods that take a Query to express the query and that return a single object.
public T findOne(Query query, Class targetClass) {
- return findOne(getEntityCollection(targetClass), query, targetClass);
+ return findOne(determineCollectionName(targetClass), query, targetClass);
}
public T findOne(Query query, Class targetClass,
MongoReader reader) {
- return findOne(getEntityCollection(targetClass), query, targetClass, reader);
+ return findOne(determineCollectionName(targetClass), query, targetClass, reader);
}
public T findOne(String collectionName, Query query,
@@ -494,11 +456,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
// Find methods that take a Query to express the query and that return a List of objects.
public List find(Query query, Class targetClass) {
- return find(getEntityCollection(targetClass), query, targetClass);
+ return find(determineCollectionName(targetClass), query, targetClass);
}
public List find(Query query, Class targetClass, MongoReader reader) {
- return find(getEntityCollection(targetClass), query, targetClass, reader);
+ return find(determineCollectionName(targetClass), query, targetClass, reader);
}
public List find(String collectionName, final Query query, Class targetClass) {
@@ -541,12 +503,12 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
// also removed from the collection in the database.
public T findAndRemove(Query query, Class targetClass) {
- return findAndRemove(getEntityCollection(targetClass), query, targetClass);
+ return findAndRemove(determineCollectionName(targetClass), query, targetClass);
}
public T findAndRemove(Query query, Class targetClass,
MongoReader reader) {
- return findAndRemove(getEntityCollection(targetClass), query, targetClass, reader);
+ return findAndRemove(determineCollectionName(targetClass), query, targetClass, reader);
}
public T findAndRemove(String collectionName, Query query,
@@ -560,29 +522,29 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.Object)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.Object)
+ */
public void insert(Object objectToSave) {
- insert(getEntityCollection(objectToSave), objectToSave);
+ insert(determineEntityCollectionName(objectToSave), objectToSave);
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.String, java.lang.Object)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.String, java.lang.Object)
+ */
public void insert(String collectionName, Object objectToSave) {
insert(collectionName, objectToSave, this.mongoConverter);
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#insert(T, org.springframework.data.document.mongodb.MongoWriter)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#insert(T, org.springframework.data.document.mongodb.MongoWriter)
+ */
public void insert(T objectToSave, MongoWriter writer) {
- insert(getEntityCollection(objectToSave), objectToSave, writer);
+ insert(determineEntityCollectionName(objectToSave), objectToSave, writer);
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.String, T, org.springframework.data.document.mongodb.MongoWriter)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.String, T, org.springframework.data.document.mongodb.MongoWriter)
+ */
public void insert(String collectionName, T objectToSave, MongoWriter writer) {
BasicDBObject dbDoc = new BasicDBObject();
@@ -597,29 +559,33 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.util.List)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.util.List)
+ */
public void insertList(List extends Object> listToSave) {
insertList(listToSave, mongoConverter);
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.lang.String, java.util.List)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.lang.String, java.util.List)
+ */
public void insertList(String collectionName, List extends Object> listToSave) {
insertList(collectionName, listToSave, this.mongoConverter);
}
/* (non-Javadoc)
- * @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.util.List, org.springframework.data.document.mongodb.MongoWriter)
- */
+ * @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.util.List, org.springframework.data.document.mongodb.MongoWriter)
+ */
public void insertList(List extends T> listToSave, MongoWriter writer) {
Map> objs = new HashMap>();
for (Object o : listToSave) {
MongoPersistentEntity> entity = mappingContext.getPersistentEntity(o.getClass());
- String collection = entity == null ? getDefaultCollectionName() : entity.getCollection();
+ if (entity == null) {
+ throw new InvalidDataAccessApiUsageException("No Persitent Entity information found for the class " +
+ o.getClass().getName());
+ }
+ String collection = entity.getCollection();
List