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 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 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 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 objList = objs.get(collection); if (null == objList) { @@ -636,8 +602,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica } /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.lang.String, java.util.List, org.springframework.data.document.mongodb.MongoWriter) - */ + * @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.lang.String, java.util.List, org.springframework.data.document.mongodb.MongoWriter) + */ public void insertList(String collectionName, List listToSave, MongoWriter writer) { Assert.notNull(writer); @@ -663,29 +629,29 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica } /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#save(java.lang.Object) - */ + * @see org.springframework.data.document.mongodb.MongoOperations#save(java.lang.Object) + */ public void save(Object objectToSave) { - save(getEntityCollection(objectToSave), objectToSave); + save(determineEntityCollectionName(objectToSave), objectToSave); } /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#save(java.lang.String, java.lang.Object) - */ + * @see org.springframework.data.document.mongodb.MongoOperations#save(java.lang.String, java.lang.Object) + */ public void save(String collectionName, Object objectToSave) { save(collectionName, objectToSave, this.mongoConverter); } /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#save(T, org.springframework.data.document.mongodb.MongoWriter) - */ + * @see org.springframework.data.document.mongodb.MongoOperations#save(T, org.springframework.data.document.mongodb.MongoWriter) + */ public void save(T objectToSave, MongoWriter writer) { - save(getEntityCollection(objectToSave), objectToSave, writer); + save(determineEntityCollectionName(objectToSave), objectToSave, writer); } /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#save(java.lang.String, T, org.springframework.data.document.mongodb.MongoWriter) - */ + * @see org.springframework.data.document.mongodb.MongoOperations#save(java.lang.String, T, org.springframework.data.document.mongodb.MongoWriter) + */ public void save(String collectionName, T objectToSave, MongoWriter writer) { BasicDBObject dbDoc = new BasicDBObject(); @@ -806,15 +772,15 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica } /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(com.mongodb.DBObject, com.mongodb.DBObject) - */ - public WriteResult updateFirst(Query query, Update update) { - return updateFirst(getRequiredDefaultCollectionName(), query, update); + * @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(com.mongodb.DBObject, com.mongodb.DBObject) + */ + public WriteResult updateFirst(Class entityClass, Query query, Update update) { + return updateFirst(determineCollectionName(entityClass), query, update); } /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject) - */ + * @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject) + */ public WriteResult updateFirst(String collectionName, final Query query, final Update update) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("calling update using query: " + query.getQueryObject() + " and update: " + update.getUpdateObject(mongoConverter) + " in collecion: " + collectionName); @@ -836,15 +802,15 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica } /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(com.mongodb.DBObject, com.mongodb.DBObject) - */ - public WriteResult updateMulti(Query query, Update update) { - return updateMulti(getRequiredDefaultCollectionName(), query, update); + * @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(com.mongodb.DBObject, com.mongodb.DBObject) + */ + public WriteResult updateMulti(Class entityClass, Query query, Update update) { + return updateMulti(determineCollectionName(entityClass), query, update); } /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject) - */ + * @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject) + */ public WriteResult updateMulti(String collectionName, final Query query, final Update update) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("calling updateMulti using query: " + query.getQueryObject() + " and update: " + update.getUpdateObject(mongoConverter) + " in collecion: " + collectionName); @@ -865,8 +831,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica } /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#remove(com.mongodb.DBObject) - */ + * @see org.springframework.data.document.mongodb.MongoOperations#remove(com.mongodb.DBObject) + */ public void remove(Query query) { remove(query, null); } @@ -878,7 +844,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica public void remove(Query query, Class targetClass) { Assert.notNull(query); - remove(getEntityCollection(targetClass), query, targetClass); + remove(determineCollectionName(targetClass), query, targetClass); } private PersistentEntity getPersistentEntity(Class type) { @@ -918,11 +884,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#getCollection(java.lang.Class) - */ + * @see org.springframework.data.document.mongodb.MongoOperations#getCollection(java.lang.Class) + */ public List getCollection(Class targetClass) { return executeEach(new FindCallback(null), null, new ReadDbObjectCallback(mongoConverter, targetClass), - getDefaultCollectionName()); + determineCollectionName(targetClass)); } public List getCollection(String collectionName, Class targetClass) { @@ -1233,30 +1199,24 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica return newValue; } - private String getRequiredDefaultCollectionName() { - String name = getDefaultCollectionName(); - if (name == null) { - throw new IllegalStateException( - "No 'defaultCollection' or 'defaultCollectionName' specified. Check configuration of MongoTemplate."); - } - return name; - } - - private String getEntityCollection(T obj) { + private String determineEntityCollectionName(T obj) { if (null != obj) { - return getEntityCollection(obj.getClass()); + return determineCollectionName(obj.getClass()); } return null; } - private String getEntityCollection(Class clazz) { + private String determineCollectionName(Class clazz) { if (clazz == null) { - return getDefaultCollectionName(); + throw new InvalidDataAccessApiUsageException("No class parameter provided, entity collection can't be determined for " + clazz); } MongoPersistentEntity entity = mappingContext.getPersistentEntity(clazz); + if (entity == null) { + throw new InvalidDataAccessApiUsageException("No Persitent Entity information found for the class " + clazz.getName()); + } return entity.getCollection(); } @@ -1309,18 +1269,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica converter.setDefaultDatabase(databaseName); } - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() - */ - public void afterPropertiesSet() { - if (this.getDefaultCollectionName() != null) { - if (!collectionExists(getDefaultCollectionName())) { - createCollection(getDefaultCollectionName(), null); - } - } - } - /** * Simple {@link CollectionCallback} that takes a query {@link DBObject} plus an optional fields specification diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/GeoSpatialAppConfig.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/GeoSpatialAppConfig.java index dedec015f..f13192b49 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/GeoSpatialAppConfig.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/GeoSpatialAppConfig.java @@ -33,7 +33,7 @@ public class GeoSpatialAppConfig extends AbstractMongoConfiguration { @Bean public MongoTemplate mongoTemplate() throws Exception { - return new MongoTemplate(mongo(), "geospatial", "newyork", mappingMongoConverter()); + return new MongoTemplate(mongo(), "geospatial", mappingMongoConverter()); } @Bean diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/GeoSpatialTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/GeoSpatialTests.java index db16d16fb..dedacf35d 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/GeoSpatialTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/GeoSpatialTests.java @@ -78,7 +78,7 @@ public class GeoSpatialTests { applicationContext = new AnnotationConfigApplicationContext(GeoSpatialAppConfig.class); template = applicationContext.getBean(MongoTemplate.class); template.setWriteConcern(WriteConcern.FSYNC_SAFE); - template.ensureIndex(new GeospatialIndex("location")); + template.ensureIndex(Venue.class, new GeospatialIndex("location")); indexCreated(); addVenues(); parser = new SpelExpressionParser(); @@ -164,7 +164,7 @@ public class GeoSpatialTests { } public void indexCreated() { - List indexInfo = getIndexInfo(); + List indexInfo = getIndexInfo(Venue.class); LOGGER.debug(indexInfo); assertThat(indexInfo.size(), equalTo(2)); assertThat(indexInfo.get(1).get("name").toString(), equalTo("location_2d")); @@ -173,8 +173,8 @@ public class GeoSpatialTests { } // TODO move to MongoAdmin - public List getIndexInfo() { - return template.execute(new CollectionCallback>() { + public List getIndexInfo(Class clazz) { + return template.execute(clazz, new CollectionCallback>() { public List doInCollection(DBCollection collection) throws MongoException, DataAccessException { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoOperationsUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoOperationsUnitTests.java index 5ef4c9b87..badb97780 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoOperationsUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoOperationsUnitTests.java @@ -88,7 +88,7 @@ public abstract class MongoOperationsUnitTests { @SuppressWarnings({ "unchecked", "rawtypes" }) public void rejectsNullForCollectionCallback() { - getOperations().execute((CollectionCallback) null); + getOperations().execute("test", (CollectionCallback) null); } @Test(expected = IllegalArgumentException.class) @@ -148,7 +148,7 @@ public abstract class MongoOperationsUnitTests { new Execution() { @Override public void doWith(MongoOperations operations) { - operations.execute(collectionCallback); + operations.execute("test", collectionCallback); } }.assertDataAccessException(); } @@ -253,16 +253,6 @@ public abstract class MongoOperationsUnitTests { }.assertDataAccessException(); } - @Test - public void convertsExceptionForGetDefaultCollection() { - new Execution() { - @Override - public void doWith(MongoOperations operations) { - operations.getDefaultCollection(); - } - }.assertDataAccessException(); - } - @Test public void convertsExceptionForInsert() { new Execution() { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java index b77cb356d..39c17659e 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java @@ -84,12 +84,16 @@ public class MongoTemplateTests { MappingMongoConverter converter = new MappingMongoConverter(mappingContext); converter.afterPropertiesSet(); - this.mappingTemplate = new MongoTemplate(mongo, "database", "springdata", converter); + this.mappingTemplate = new MongoTemplate(mongo, "database", converter); } @Before public void setUp() { - template.dropCollection(template.getDefaultCollectionName()); + template.dropCollection(template.getCollectionName(Person.class)); + template.dropCollection(template.getCollectionName(PersonWith_idPropertyOfTypeObjectId.class)); + template.dropCollection(template.getCollectionName(PersonWith_idPropertyOfTypeString.class)); + template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfTypeObjectId.class)); + template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfTypeString.class)); } @Test @@ -109,7 +113,7 @@ public class MongoTemplateTests { @Test public void updateFailure() throws Exception { - MongoTemplate mongoTemplate = new MongoTemplate(template.getDb().getMongo(), "test", "people"); + MongoTemplate mongoTemplate = new MongoTemplate(template.getDb().getMongo(), "test"); mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION); Person person = new Person("Oliver2"); @@ -120,7 +124,7 @@ public class MongoTemplateTests { Update u = new Update().set("firstName", "Sven"); thrown.expect(DataIntegrityViolationException.class); thrown.expectMessage(endsWith("0 documents updated")); - mongoTemplate.updateFirst(q, u); + mongoTemplate.updateFirst(Person.class, q, u); } @@ -134,9 +138,9 @@ public class MongoTemplateTests { p2.setAge(40); template.insert(p2); - template.ensureIndex(new Index().on("age", Order.DESCENDING).unique(Duplicates.DROP)); + template.ensureIndex(Person.class, new Index().on("age", Order.DESCENDING).unique(Duplicates.DROP)); - DBCollection coll = template.getCollection(template.getDefaultCollectionName()); + DBCollection coll = template.getCollection(template.getCollectionName(Person.class)); List indexInfo = coll.getIndexInfo(); assertThat(indexInfo.size(), is(2)); @@ -358,23 +362,23 @@ public class MongoTemplateTests { PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId(); p1.setFirstName("Sven"); p1.setAge(11); - template.insert("springdata", p1); + template.insert(p1); PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId(); p2.setFirstName("Mary"); p2.setAge(21); - template.insert("springdata", p2); + template.insert(p2); Update u = new Update().set("firstName", "Bob").set("age", 10); - WriteResult wr = template.updateMulti("springdata", new Query(), u); + WriteResult wr = template.updateMulti(PersonWithIdPropertyOfTypeObjectId.class, new Query(), u); assertThat(wr.getN(), is(2)); Query q1 = new Query(Criteria.where("age").in(11, 21)); - List r1 = template.find("springdata", q1, PersonWithIdPropertyOfTypeObjectId.class); + List r1 = template.find(q1, PersonWithIdPropertyOfTypeObjectId.class); assertThat(r1.size(), is(0)); Query q2 = new Query(Criteria.where("age").is(10)); - List r2 = template.find("springdata", q2, PersonWithIdPropertyOfTypeObjectId.class); + List r2 = template.find(q2, PersonWithIdPropertyOfTypeObjectId.class); assertThat(r2.size(), is(2)); for (PersonWithIdPropertyOfTypeObjectId p : r2) { assertThat(p.getAge(), is(10)); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateUnitTests.java index e3c0e0ba1..267efb4d8 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateUnitTests.java @@ -48,7 +48,7 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests { @Before public void setUp() { - this.template = new MongoTemplate(mongo, "database", "default"); + this.template = new MongoTemplate(mongo, "database"); } @Test(expected = IllegalArgumentException.class) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/PersonExampleAppConfig.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/PersonExampleAppConfig.java index 5f2aac2fd..e1ac6c94a 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/PersonExampleAppConfig.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/PersonExampleAppConfig.java @@ -30,7 +30,7 @@ public class PersonExampleAppConfig { @Bean public MongoTemplate mongoTemplate() throws Exception { - return new MongoTemplate(mongo(), "database", "personexample"); + return new MongoTemplate(mongo(), "database"); } @Bean diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java index 77381dcd4..90554f740 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java @@ -23,10 +23,7 @@ public class MvcAnalyticsTests { @Before public void setUp() throws Exception { Mongo m = new Mongo(); - mongoTemplate = new MongoTemplate(m, "mvc", "mvc"); - mongoTemplate.afterPropertiesSet(); - - + mongoTemplate = new MongoTemplate(m, "mvc"); } @Test diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/GeoIndexedAppConfig.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/GeoIndexedAppConfig.java index 78d124885..815bc6360 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/GeoIndexedAppConfig.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/GeoIndexedAppConfig.java @@ -17,7 +17,7 @@ public class GeoIndexedAppConfig extends AbstractMongoConfiguration { @Bean public MongoTemplate mongoTemplate() throws Exception { - return new MongoTemplate(mongo(), "geodb", "geolocation", mappingMongoConverter()); + return new MongoTemplate(mongo(), "geodb", mappingMongoConverter()); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java index 0676a1867..cb249197e 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java @@ -334,7 +334,7 @@ public class MappingTests { template.insert(p); addr.setCity("New Town"); - template.updateFirst(query(where("ssn").is(1111)), update("address", addr)); + template.updateFirst(Person.class, query(where("ssn").is(1111)), update("address", addr)); Person p2 = template.findOne(query(where("ssn").is(1111)), Person.class); assertThat(p2.getAddress().getCity(), is("New Town")); diff --git a/spring-data-mongodb/src/test/resources/geospatial.xml b/spring-data-mongodb/src/test/resources/geospatial.xml index 1f4b53fc9..7ffe709a9 100644 --- a/spring-data-mongodb/src/test/resources/geospatial.xml +++ b/spring-data-mongodb/src/test/resources/geospatial.xml @@ -12,7 +12,6 @@ - diff --git a/spring-data-mongodb/src/test/resources/infrastructure.xml b/spring-data-mongodb/src/test/resources/infrastructure.xml index 9c4ddf130..c388bafe2 100644 --- a/spring-data-mongodb/src/test/resources/infrastructure.xml +++ b/spring-data-mongodb/src/test/resources/infrastructure.xml @@ -11,7 +11,6 @@ - diff --git a/spring-data-mongodb/src/test/resources/mapping.xml b/spring-data-mongodb/src/test/resources/mapping.xml index aba61fa66..7ae5ca87d 100644 --- a/spring-data-mongodb/src/test/resources/mapping.xml +++ b/spring-data-mongodb/src/test/resources/mapping.xml @@ -11,7 +11,6 @@ - diff --git a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/config/MongoNamespaceIntegrationTests-context.xml b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/config/MongoNamespaceIntegrationTests-context.xml index 3b2cf0b2f..bec490e13 100644 --- a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/config/MongoNamespaceIntegrationTests-context.xml +++ b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/config/MongoNamespaceIntegrationTests-context.xml @@ -12,7 +12,6 @@ -