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 26c2b4063..9f521c028 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 @@ -15,6 +15,7 @@ */ package org.springframework.data.document.mongodb; +import java.util.Collection; import java.util.List; import java.util.Set; @@ -43,7 +44,7 @@ public interface MongoOperations { * * @return */ - String getCollectionName(Class clazz); + String getCollectionName(Class entityClass); /** * Execute the a MongoDB command expressed as a JSON string. This will call the method JSON.parse that is part of the @@ -122,6 +123,26 @@ public interface MongoOperations { */ T executeInSession(DbCallback action); + /** + * Create an uncapped collection with a name based on the provided entity class. + * + * @param entityClass + * class that determines the collection to create + * @return the created collection + */ + DBCollection createCollection(Class entityClass); + + /** + * Create a collect with a name based on the provided entity class using the options. + * + * @param entityClass + * class that determines the collection to create + * @param collectionOptions + * options to use when creating the collection. + * @return the created collection + */ + DBCollection createCollection(Class entityClass, CollectionOptions collectionOptions); + /** * Create an uncapped collection with the provided name. * @@ -160,6 +181,17 @@ public interface MongoOperations { */ DBCollection getCollection(String collectionName); + /** + * Check to see if a collection with a name indicated by the entity class exists. + *

+ * Translate any exceptions as necessary. + * + * @param entityClass + * class that determines the name of the collection + * @return true if a collection with the given name is found, false otherwise. + */ + boolean collectionExists(Class entityClass); + /** * Check to see if a collection with a given name exists. *

@@ -171,6 +203,16 @@ public interface MongoOperations { */ boolean collectionExists(String collectionName); + /** + * Drop the collection with the name indicated by the entity class. + *

+ * Translate any exceptions as necessary. + * + * @param entityClass + * class that determines the collection to drop/delete. + */ + void dropCollection(Class entityClass); + /** * Drop the collection with the given name. *

@@ -182,7 +224,7 @@ public interface MongoOperations { void dropCollection(String collectionName); /** - * Query for a list of objects of type T from the default collection. + * Query for a list of objects of type T from the collection used by the entity class. *

* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless * configured otherwise, an instance of SimpleMongoConverter will be used. @@ -194,7 +236,7 @@ public interface MongoOperations { * the parameterized type of the returned list * @return the converted collection */ - List getCollection(Class entityClass); + List findAll(Class entityClass); /** * Query for a list of objects of type T from the specified collection. @@ -205,35 +247,35 @@ public interface MongoOperations { * If your collection does not contain a homogeneous collection of types, this operation will not be an efficient way * to map objects since the test for class type is done in the client and not on the server. * - * @param collectionName - * name of the collection to retrieve the objects from * @param entityClass * the parameterized type of the returned list. + * @param collectionName + * name of the collection to retrieve the objects from * @return the converted collection */ - List getCollection(String collectionName, Class entityClass); + List findAll(Class entityClass, String collectionName); /** - * Ensure that an index for the provided {@link IndexDefinition} exists for the default collection. If not it will be - * created. + * Ensure that an index for the provided {@link IndexDefinition} exists for the collection indicated by the entity class. + * If not it will be created. * + * @param indexDefinition * @param entityClass * class that determines the collection to use - * @param indexDefinition */ - void ensureIndex(Class entityClass, IndexDefinition indexDefinition); + void ensureIndex(IndexDefinition indexDefinition, Class entityClass); /** * Ensure that an index for the provided {@link IndexDefinition} exists. If not it will be created. * - * @param collectionName * @param index + * @param collectionName */ - void ensureIndex(String collectionName, IndexDefinition indexDefinition); + void ensureIndex(IndexDefinition indexDefinition, String collectionName); /** - * Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object of the - * specified type. + * Map the results of an ad-hoc query on the collection for the entity class to a single instance of an object + * of the specified type. *

* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless * configured otherwise, an instance of SimpleMongoConverter will be used. @@ -259,20 +301,20 @@ public interface MongoOperations { *

* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more * feature rich {@link Query}. - * - * @param collectionName - * name of the collection to retrieve the objects from * @param query * the query class that specifies the criteria used to find a record and also an optional fields * specification * @param entityClass * the parameterized type of the returned list. + * @param collectionName + * name of the collection to retrieve the objects from + * * @return the converted object */ - T findOne(String collectionName, Query query, Class entityClass); + T findOne(Query query, Class entityClass, String collectionName); /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. + * Map the results of an ad-hoc query on the collection for the entity class to a List of the specified type. *

* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless * configured otherwise, an instance of SimpleMongoConverter will be used. @@ -297,17 +339,17 @@ public interface MongoOperations { *

* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more * feature rich {@link Query}. - * - * @param collectionName - * name of the collection to retrieve the objects from * @param query * the query class that specifies the criteria used to find a record and also an optional fields * specification * @param entityClass * the parameterized type of the returned list. + * @param collectionName + * name of the collection to retrieve the objects from + * * @return the List of converted objects */ - List find(String collectionName, Query query, Class entityClass); + List find(Query query, Class entityClass, String collectionName); /** * Map the results of an ad-hoc query on the specified collection to a List of the specified type. @@ -317,9 +359,6 @@ public interface MongoOperations { *

* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more * feature rich {@link Query}. - * - * @param collectionName - * name of the collection to retrieve the objects from * @param query * the query class that specifies the criteria used to find a record and also an optional fields * specification @@ -328,9 +367,12 @@ public interface MongoOperations { * @param preparer * allows for customization of the DBCursor used when iterating over the result set, (apply limits, skips and * so on). + * @param collectionName + * name of the collection to retrieve the objects from + * * @return the List of converted objects. */ - List find(String collectionName, Query query, Class entityClass, CursorPreparer preparer); + List find(Query query, Class entityClass, CursorPreparer preparer, String collectionName); /** * Returns a document with the given id mapped onto the given class. The collection the query is ran against will be @@ -345,31 +387,30 @@ public interface MongoOperations { /** * Returns the document with the given id from the given collection mapped onto the given target class. - * - * @param - * @param collectionName the collection to query for the document * @param id the id of the document to return * @param entityClass the type to convert the document to + * @param collectionName the collection to query for the document + * + * @param * @return */ - T findById(String collectionName, Object id, Class entityClass); - + T findById(Object id, Class entityClass, String collectionName); + /** - * Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object of the - * specified type. The first document that matches the query is returned and also removed from the collection in the - * database. + * Map the results of an ad-hoc query on the collection for the entity type to a single instance of an + * object of the specified type. The first document that matches the query is returned and also removed from + * the collection in the database. *

- * The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless - * configured otherwise, an instance of SimpleMongoConverter will be used. + * The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. *

- * The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more + * The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more * feature rich {@link Query}. * * @param query - * the query class that specifies the criteria used to find a record and also an optional fields - * specification + * the query class that specifies the criteria used to find a + * record and also an optional fields specification * @param entityClass - * the parameterized type of the returned list. + * the parameterized type of the returned list. * @return the converted object */ T findAndRemove(Query query, Class entityClass); @@ -383,23 +424,22 @@ public interface MongoOperations { *

* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more * feature rich {@link Query}. - * - * @param collectionName - * name of the collection to retrieve the objects from * @param query * the query class that specifies the criteria used to find a record and also an optional fields * specification * @param entityClass * the parameterized type of the returned list. + * @param collectionName + * name of the collection to retrieve the objects from + * * @return the converted object */ - T findAndRemove(String collectionName, Query query, Class entityClass); + T findAndRemove(Query query, Class entityClass, String collectionName); /** - * Insert the object into the default collection. + * Insert the object into the collection for the entity type of the object to save. *

- * The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. Unless - * configured otherwise, an instance of SimpleMongoConverter will be used. + * The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. *

* If you object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a * String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your @@ -422,35 +462,44 @@ public interface MongoOperations { * configured otherwise, an instance of SimpleMongoConverter will be used. *

* Insert is used to initially store the object into the database. To update an existing object use the save method. - * - * @param collectionName - * name of the collection to store the object in * @param objectToSave * the object to store in the collection + * @param collectionName + * name of the collection to store the object in */ - void insert(String collectionName, Object objectToSave); + void insert(Object objectToSave, String collectionName); /** - * Insert a list of objects into the default collection in a single batch write to the database. + * Insert a Collection of objects into a collection in a single batch write to the database. * - * @param listToSave + * @param batchToSave * the list of objects to save. + * @param entityClass + * class that determines the collection to use */ - void insertList(List listToSave); + void insert(Collection batchToSave, Class entityClass); /** * Insert a list of objects into the specified collection in a single batch write to the database. - * + * @param batchToSave + * the list of objects to save. * @param collectionName * name of the collection to store the object in - * @param listToSave - * the list of objects to save. */ - void insertList(String collectionName, List listToSave); + void insert(Collection batchToSave, String collectionName); /** - * Save the object to the default collection. This will perform an insert if the object is not already present, that - * is an 'upsert'. + * Insert a mixed Collection of objects into a database collection determining the + * collection name to use based on the class. + * + * @param collectionToSave + * the list of objects to save. + */ + void insertAll(Collection objectsToSave); + + /** + * Save the object to the collection for the entity type of the object to save. This will perform an + * insert if the object is not already present, that is an 'upsert'. *

* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. Unless * configured otherwise, an instance of SimpleMongoConverter will be used. @@ -478,20 +527,19 @@ public interface MongoOperations { * property type will be handled by Spring's BeanWrapper class that leverages Spring 3.0's new Type Cobnversion API. * See Spring 3 Type * Conversion" for more details. - * - * @param collectionName - * name of the collection to store the object in * @param objectToSave * the object to store in the collection + * @param collectionName + * name of the collection to store the object in */ - void save(String collectionName, Object objectToSave); + void save(Object objectToSave, String collectionName); /** - * Updates the first object that is found in the default collection that matches the query document with the provided - * updated document. - * @param queryDoc + * Updates the first object that is found in the collection of the entity class that matches the query document with + * the provided update document. + * @param query * the query document that specifies the criteria used to select a record to be updated - * @param updateDoc + * @param update * the update document that contains the updated object or $ operators to manipulate the existing object. * @param entityClass * class that determines the collection to use @@ -501,22 +549,21 @@ public interface MongoOperations { /** * Updates the first object that is found in the specified collection that matches the query document criteria with * the provided updated document. - * + * @param query + * the query document that specifies the criteria used to select a record to be updated + * @param update + * the update document that contains the updated object or $ operators to manipulate the existing object. * @param collectionName * name of the collection to update the object in - * @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(String collectionName, Query query, Update update); + WriteResult updateFirst(Query query, Update update, String collectionName); /** - * Updates all objects that are found in the default collection that matches the query document criteria with the - * provided updated document. - * @param queryDoc + * Updates all objects that are found in the collection for the entity class that matches the query document criteria + * with the provided updated document. + * @param query * the query document that specifies the criteria used to select a record to be updated - * @param updateDoc + * @param update * the update document that contains the updated object or $ operators to manipulate the existing object. * @param entityClass * class that determines the collection to use @@ -526,15 +573,14 @@ public interface MongoOperations { /** * Updates all objects that are found in the specified collection that matches the query document criteria with the * provided updated document. - * + * @param query + * the query document that specifies the criteria used to select a record to be updated + * @param update + * the update document that contains the updated object or $ operators to manipulate the existing object. * @param collectionName * name of the collection to update the object in - * @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(String collectionName, Query query, Update update); + WriteResult updateMulti(Query query, Update update, String collectionName); /** * Remove the given object from the collection by Id @@ -544,16 +590,10 @@ public interface MongoOperations { void remove(Object object); /** - * Remove all documents from the default collection that match the provided query document criteria. - * - * @param queryDoc - * the query document that specifies the criteria used to remove a record - */ - void remove(Query query); - - /** - * Remove all documents from the default collection that match the provided query document criteria. The Class - * parameter is used to help convert the Id of the object if it is present in the query. + * Remove all documents that match the provided query document criteria from + * the the collection used to store the entityClass. The Class parameter is + * also used to help convert the Id of the object if it is present in the + * query. * * @param * @param query @@ -563,22 +603,12 @@ public interface MongoOperations { /** * Remove all documents from the specified collection that match the provided query document criteria. - * + * There is no conversion/mapping done for any criteria using the id field. + * @param query + * the query document that specifies the criteria used to remove a record * @param collectionName * name of the collection where the objects will removed - * @param queryDoc - * the query document that specifies the criteria used to remove a record */ - void remove(String collectionName, Query query); - - /** - * Remove all documents from the specified collection that match the provided query document criteria. The Class - * parameter is used to help convert the Id of the object if it is present in the query. - * - * @param collectionName - * @param query - * @param entityClass - */ - void remove(String collectionName, Query query, Class entityClass); + void remove(Query query, String collectionName); } \ No newline at end of file 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 5c76ac4eb..a7aaada1d 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 @@ -48,7 +48,6 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConversionService; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataIntegrityViolationException; @@ -90,6 +89,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { private static final Log LOGGER = LogFactory.getLog(MongoTemplate.class); private static final String ID = "_id"; + @SuppressWarnings("serial") private static final List ITERABLE_CLASSES = new ArrayList() { { add(List.class.getName()); @@ -272,8 +272,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * @see org.springframework.data.document.mongodb.MongoOperations# * getDefaultCollectionName() */ - public String getCollectionName(Class clazz) { - return this.determineCollectionName(clazz); + public String getCollectionName(Class entityClass) { + return this.determineCollectionName(entityClass); } /* @@ -387,6 +387,30 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { }); } + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.document.mongodb.MongoOperations#createCollection + * (java.lang.Class) + */ + public DBCollection createCollection(Class entityClass) { + return createCollection(determineCollectionName(entityClass)); + } + + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.document.mongodb.MongoOperations#createCollection + * (java.lang.Class, + * org.springframework.data.document.mongodb.CollectionOptions) + */ + public DBCollection createCollection(Class entityClass, + CollectionOptions collectionOptions) { + return createCollection(determineCollectionName(entityClass), collectionOptions); + } + /* * (non-Javadoc) * @@ -428,6 +452,17 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { }); } + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.document.mongodb.MongoOperations#collectionExists + * (java.lang.Class) + */ + public boolean collectionExists(Class entityClass) { + return collectionExists(determineCollectionName(entityClass)); + } + /* * (non-Javadoc) * @@ -444,6 +479,19 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { }); } + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.document.mongodb.MongoOperations#dropCollection + * (java.lang.Class) + */ + public void dropCollection(Class entityClass) { + + dropCollection(determineCollectionName(entityClass)); + + } + /* * (non-Javadoc) * @@ -468,13 +516,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { // Indexing methods - public void ensureIndex(Class entityClass, - IndexDefinition indexDefinition) { - ensureIndex(determineCollectionName(entityClass), indexDefinition); + public void ensureIndex(IndexDefinition indexDefinition, Class entityClass) { + ensureIndex(indexDefinition, determineCollectionName(entityClass)); } - public void ensureIndex(String collectionName, - final IndexDefinition indexDefinition) { + public void ensureIndex(final IndexDefinition indexDefinition, String collectionName) { execute(collectionName, new CollectionCallback() { public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { @@ -494,11 +540,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { // single object. public T findOne(Query query, Class entityClass) { - return findOne(determineCollectionName(entityClass), query, entityClass); + return findOne(query, entityClass, determineCollectionName(entityClass)); } - public T findOne(String collectionName, Query query, - Class entityClass) { + public T findOne(Query query, Class entityClass, + String collectionName) { return doFindOne(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass); } @@ -508,11 +554,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { // of objects. public List find(Query query, Class entityClass) { - return find(determineCollectionName(entityClass), query, entityClass); + return find(query, entityClass, determineCollectionName(entityClass)); } - public List find(String collectionName, final Query query, - Class entityClass) { + public List find(final Query query, Class entityClass, + String collectionName) { CursorPreparer cursorPreparer = null; if (query.getSkip() > 0 || query.getLimit() > 0 || query.getSortObject() != null) { @@ -542,8 +588,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { query.getFieldsObject(), entityClass, cursorPreparer); } - public List find(String collectionName, Query query, - Class entityClass, CursorPreparer preparer) { + public List find(Query query, Class entityClass, + CursorPreparer preparer, String collectionName) { return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass, preparer); } @@ -551,10 +597,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { public T findById(Object id, Class entityClass) { MongoPersistentEntity persistentEntity = mappingContext .getPersistentEntity(entityClass); - return findById(persistentEntity.getCollection(), id, entityClass); + return findById(id, entityClass, persistentEntity.getCollection()); } - public T findById(String collectionName, Object id, Class entityClass) { + public T findById(Object id, Class entityClass, String collectionName) { MongoPersistentEntity persistentEntity = mappingContext .getPersistentEntity(entityClass); MongoPersistentProperty idProperty = persistentEntity.getIdProperty(); @@ -568,12 +614,12 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { // also removed from the collection in the database. public T findAndRemove(Query query, Class entityClass) { - return findAndRemove(determineCollectionName(entityClass), query, - entityClass); + return findAndRemove(query, entityClass, + determineCollectionName(entityClass)); } - public T findAndRemove(String collectionName, Query query, - Class entityClass) { + public T findAndRemove(Query query, Class entityClass, + String collectionName) { return doFindAndRemove(collectionName, query.getQueryObject(), query.getFieldsObject(), query.getSortObject(), entityClass); } @@ -587,7 +633,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { */ public void insert(Object objectToSave) { ensureNotIterable(objectToSave); - insert(determineEntityCollectionName(objectToSave), objectToSave); + insert(objectToSave, determineEntityCollectionName(objectToSave)); } /* @@ -597,7 +643,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * org.springframework.data.document.mongodb.MongoOperations#insert(java * .lang .String, java.lang.Object) */ - public void insert(String collectionName, Object objectToSave) { + public void insert(Object objectToSave, String collectionName) { ensureNotIterable(objectToSave); doInsert(collectionName, objectToSave, this.mongoConverter); } @@ -658,8 +704,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * org.springframework.data.document.mongodb.MongoOperations#insertList(java * .util.List) */ - public void insertList(List listToSave) { - doInsertList(listToSave, mongoConverter); + public void insert(Collection batchToSave, Class entityClass) { + doInsertBatch(determineCollectionName(entityClass), batchToSave, this.mongoConverter); } /* @@ -669,12 +715,23 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * org.springframework.data.document.mongodb.MongoOperations#insertList(java * .lang.String, java.util.List) */ - public void insertList(String collectionName, - List listToSave) { - doInsertList(collectionName, listToSave, this.mongoConverter); + public void insert(Collection batchToSave, + String collectionName) { + doInsertBatch(collectionName, batchToSave, this.mongoConverter); } - protected void doInsertList(List listToSave, + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.document.mongodb.MongoOperations#insertAll(java + * .util.Collection) + */ + public void insertAll(Collection objectsToSave) { + doInsertAll(objectsToSave, this.mongoConverter); + } + + protected void doInsertAll(Collection listToSave, MongoWriter writer) { Map> objs = new HashMap>(); @@ -699,17 +756,17 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { } for (Map.Entry> entry : objs.entrySet()) { - doInsertList(entry.getKey(), entry.getValue(), this.mongoConverter); + doInsertBatch(entry.getKey(), entry.getValue(), this.mongoConverter); } } - protected void doInsertList(String collectionName, - List listToSave, MongoWriter writer) { + protected void doInsertBatch(String collectionName, + Collection batchToSave, MongoWriter writer) { Assert.notNull(writer); List dbObjectList = new ArrayList(); - for (T o : listToSave) { + for (T o : batchToSave) { BasicDBObject dbDoc = new BasicDBObject(); maybeEmitEvent(new BeforeConvertEvent(o)); @@ -719,12 +776,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { dbObjectList.add(dbDoc); } List ids = insertDBObjectList(collectionName, dbObjectList); - for (int i = 0; i < listToSave.size(); i++) { + int i = 0; + for (T obj : batchToSave) { if (i < ids.size()) { - T obj = listToSave.get(i); populateIdIfNecessary(obj, ids.get(i)); maybeEmitEvent(new AfterSaveEvent(obj, dbObjectList.get(i))); } + i++; } } @@ -736,7 +794,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * .Object) */ public void save(Object objectToSave) { - save(determineEntityCollectionName(objectToSave), objectToSave); + save(objectToSave, determineEntityCollectionName(objectToSave)); } /* @@ -746,7 +804,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * org.springframework.data.document.mongodb.MongoOperations#save(java.lang * .String, java.lang.Object) */ - public void save(String collectionName, Object objectToSave) { + public void save(Object objectToSave, String collectionName) { doSave(collectionName, objectToSave, this.mongoConverter); } @@ -859,8 +917,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * org.springframework.data.document.mongodb.MongoOperations#updateFirst * (java .lang.String, com.mongodb.DBObject, com.mongodb.DBObject) */ - public WriteResult updateFirst(final String collectionName, - final Query query, final Update update) { + public WriteResult updateFirst(final Query query, + final Update update, final String collectionName) { return doUpdate(collectionName, query, update, null, false, false); } @@ -884,8 +942,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * 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) { + public WriteResult updateMulti(final Query query, final Update update, + String collectionName) { return doUpdate(collectionName, query, update, null, false, true); } @@ -947,17 +1005,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.document.mongodb.MongoOperations#remove(com. - * mongodb .DBObject) - */ - public void remove(Query query) { - remove(query, null); - } - public void remove(Object object) { remove(new Query(where(getIdPropertyName(object)) .is(getIdValue(object))), object.getClass()); @@ -965,10 +1012,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { public void remove(Query query, Class entityClass) { Assert.notNull(query); - remove(determineCollectionName(entityClass), query, entityClass); + doRemove(determineCollectionName(entityClass), query, entityClass); } - public void remove(String collectionName, final Query query, + protected void doRemove(String collectionName, final Query query, Class entityClass) { if (query == null) { throw new InvalidDataAccessApiUsageException( @@ -1004,8 +1051,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * org.springframework.data.document.mongodb.MongoOperations#remove(java * .lang .String, com.mongodb.DBObject) */ - public void remove(String collectionName, final Query query) { - remove(collectionName, query, null); + public void remove(final Query query, String collectionName) { + doRemove(collectionName, query, null); } /* @@ -1015,13 +1062,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * org.springframework.data.document.mongodb.MongoOperations#getCollection * (java.lang.Class) */ - public List getCollection(Class entityClass) { + public List findAll(Class entityClass) { return executeFindMultiInternal(new FindCallback(null), null, new ReadDbObjectCallback(mongoConverter, entityClass), determineCollectionName(entityClass)); } - public List getCollection(String collectionName, Class entityClass) { + public List findAll(Class entityClass, String collectionName) { return executeFindMultiInternal(new FindCallback(null), null, new ReadDbObjectCallback(mongoConverter, entityClass), collectionName); @@ -1393,19 +1440,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { return mappingContext.getPersistentEntity(type).getIdProperty(); } - private ObjectId convertIdValue(MongoConverter converter, Object value) { - ObjectId newValue = null; - try { - if (value instanceof String && ObjectId.isValid((String) value)) { - newValue = converter.convertObjectId(value); - } - } catch (ConversionFailedException iae) { - LOGGER.warn("Unable to convert the String " + value - + " to an ObjectId"); - } - return newValue; - } - private String determineEntityCollectionName(T obj) { if (null != obj) { return determineCollectionName(obj.getClass()); @@ -1414,20 +1448,20 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { return null; } - private String determineCollectionName(Class clazz) { + private String determineCollectionName(Class entityClass) { - if (clazz == null) { + if (entityClass == null) { throw new InvalidDataAccessApiUsageException( "No class parameter provided, entity collection can't be determined for " - + clazz); + + entityClass); } MongoPersistentEntity entity = mappingContext - .getPersistentEntity(clazz); + .getPersistentEntity(entityClass); if (entity == null) { throw new InvalidDataAccessApiUsageException( "No Persitent Entity information found for the class " - + clazz.getName()); + + entityClass.getName()); } return entity.getCollection(); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/AbstractMongoQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/AbstractMongoQuery.java index f0665b4a3..ed302a0f7 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/AbstractMongoQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/AbstractMongoQuery.java @@ -103,7 +103,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { MongoEntityInformation metadata = method.getEntityInformation(); String collectionName = metadata.getCollectionName(); - return template.find(collectionName, query, metadata.getJavaType()); + return template.find(query, metadata.getJavaType(), collectionName); } } @@ -158,8 +158,8 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { MongoEntityInformation metadata = method.getEntityInformation(); int count = getCollectionCursor(metadata.getCollectionName(), query.getQueryObject()).count(); - List result = template.find(metadata.getCollectionName(), applyPagination(query, pageable), - metadata.getJavaType()); + List result = template.find(applyPagination(query, pageable), metadata.getJavaType(), + metadata.getCollectionName()); return new PageImpl(result, pageable, count); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepositoryFactoryBean.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepositoryFactoryBean.java index f74f00b2c..498a4c405 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepositoryFactoryBean.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepositoryFactoryBean.java @@ -297,7 +297,7 @@ public class MongoRepositoryFactoryBean, S, ID exten } MongoEntityInformation metadata = query.getQueryMethod().getEntityInformation(); - operations.ensureIndex(metadata.getCollectionName(), index); + operations.ensureIndex(index, metadata.getCollectionName()); LOG.debug(String.format("Created %s!", index)); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java index a3b01bcf7..c7be76469 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java @@ -71,7 +71,7 @@ public class SimpleMongoRepository implements Paging */ public T save(T entity) { - template.save(entityInformation.getCollectionName(), entity); + template.save(entity, entityInformation.getCollectionName()); return entity; } @@ -144,7 +144,7 @@ public class SimpleMongoRepository implements Paging * @see org.springframework.data.repository.Repository#delete(java.io.Serializable) */ public void delete(ID id) { - template.remove(entityInformation.getCollectionName(), getIdQuery(id), entityInformation.getJavaType()); + template.remove(getIdQuery(id), entityInformation.getJavaType()); } /* @@ -177,7 +177,7 @@ public class SimpleMongoRepository implements Paging */ public void deleteAll() { - template.remove(entityInformation.getCollectionName(), new Query()); + template.remove(new Query(), entityInformation.getCollectionName()); } /* @@ -245,7 +245,7 @@ public class SimpleMongoRepository implements Paging return Collections.emptyList(); } - return template.find(entityInformation.getCollectionName(), query, entityInformation.getJavaType()); + return template.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName()); } /** 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 69aca05ef..3750e4ff2 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 @@ -71,7 +71,7 @@ public class GeoSpatialTests { applicationContext = new AnnotationConfigApplicationContext(GeoSpatialAppConfig.class); template = applicationContext.getBean(MongoTemplate.class); template.setWriteConcern(WriteConcern.FSYNC_SAFE); - template.ensureIndex(Venue.class, new GeospatialIndex("location")); + template.ensureIndex(new GeospatialIndex("location"), Venue.class); indexCreated(); addVenues(); parser = new SpelExpressionParser(); @@ -158,7 +158,7 @@ public class GeoSpatialTests { assertThat(template, notNullValue()); Venue foundVenue = template.findOne(new Query(Criteria.where("name").is("Penn Station")), Venue.class); assertThat(foundVenue, notNullValue()); - List venues = template.getCollection(Venue.class); + List venues = template.findAll(Venue.class); assertThat(venues.size(), equalTo(12)); Collection names = (Collection) parser.parseExpression("![name]").getValue(venues); assertThat(names.size(), equalTo(12)); 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 70e3a9c1e..6ec056a61 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 @@ -214,7 +214,7 @@ public abstract class MongoOperationsUnitTests { new Execution() { @Override public void doWith(MongoOperations operations) { - operations.getCollection(Object.class); + operations.findAll(Object.class); } }.assertDataAccessException(); } @@ -234,7 +234,7 @@ public abstract class MongoOperationsUnitTests { new Execution() { @Override public void doWith(MongoOperations operations) { - operations.getCollection("collection", Object.class); + operations.findAll(Object.class, "collection"); } }.assertDataAccessException(); } @@ -244,7 +244,7 @@ public abstract class MongoOperationsUnitTests { new Execution() { @Override public void doWith(MongoOperations operations) { - operations.getCollection("collection", Object.class); + operations.findAll(Object.class, "collection"); } }.assertDataAccessException(); } @@ -274,7 +274,7 @@ public abstract class MongoOperationsUnitTests { new Execution() { @Override public void doWith(MongoOperations operations) { - operations.insert("collection", person); + operations.insert(person, "collection"); } }.assertDataAccessException(); } @@ -284,7 +284,7 @@ public abstract class MongoOperationsUnitTests { new Execution() { @Override public void doWith(MongoOperations operations) { - operations.insertList(persons); + operations.insertAll(persons); } }.assertDataAccessException(); } @@ -294,7 +294,7 @@ public abstract class MongoOperationsUnitTests { new Execution() { @Override public void doWith(MongoOperations operations) { - operations.insertList("collection", persons); + operations.insert(persons, "collection"); } }.assertDataAccessException(); } 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 68308da6f..4b5295055 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 @@ -149,7 +149,7 @@ public class MongoTemplateTests { p2.setAge(40); template.insert(p2); - template.ensureIndex(Person.class, new Index().on("age", Order.DESCENDING).unique(Duplicates.DROP)); + template.ensureIndex(new Index().on("age", Order.DESCENDING).unique(Duplicates.DROP), Person.class); DBCollection coll = template.getCollection(template.getCollectionName(Person.class)); List indexInfo = coll.getIndexInfo(); @@ -372,7 +372,7 @@ public class MongoTemplateTests { } private void checkCollectionContents(Class entityClass, int count) { - assertThat(template.getCollection(entityClass).size(), is(count)); + assertThat(template.findAll(entityClass).size(), is(count)); } @Test 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 c3d0ad03b..26f0ee294 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 @@ -66,7 +66,7 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests { MongoTemplate template = mockOutGetDb(); when(db.getCollection("collection")).thenThrow(new MongoException("Exception!")); - template.remove("collection", null); + template.remove(null, "collection"); } @Test diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/PersonExample.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/PersonExample.java index 096f0b753..4693c0912 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/PersonExample.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/PersonExample.java @@ -63,7 +63,7 @@ public class PersonExample { p = mongoOps.findById(p.getId(), PersonWithIdPropertyOfTypeString.class); log.debug("Updated: " + p); - List folks = mongoOps.getCollection(PersonWithIdPropertyOfTypeString.class); + List folks = mongoOps.findAll(PersonWithIdPropertyOfTypeString.class); log.debug("Querying for all people..."); for (PersonWithIdPropertyOfTypeString element : folks) { log.debug(element); @@ -73,7 +73,7 @@ public class PersonExample { mongoOps.remove(p); - List people = mongoOps.getCollection(PersonWithIdPropertyOfTypeString.class); + List people = mongoOps.findAll(PersonWithIdPropertyOfTypeString.class); // PersonWithIdPropertyOfTypeString p2 = mongoOps.findOne(query(whereId().is(p.getId())), // PersonWithIdPropertyOfTypeString.class); 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 54a2c4c6e..7bf815fc3 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 @@ -175,21 +175,21 @@ public class MappingTests { Account acct = new Account(); acct.setBalance(1000.00f); - template.insert("account", acct); + template.insert(acct, "account"); List accounts = new ArrayList(); accounts.add(acct); Person p = new Person(123456789, "John", "Doe", 37, addr); p.setAccounts(accounts); - template.insert("person", p); + template.insert(p, "person"); Account newAcct = new Account(); newAcct.setBalance(10000.00f); - template.insert("account", newAcct); + template.insert(newAcct, "account"); accounts.add(newAcct); - template.save("person", p); + template.save(p, "person"); assertNotNull(p.getId()); @@ -214,7 +214,7 @@ public class MappingTests { List persons = new ArrayList(); persons.add(p1); persons.add(p2); - template.insertList(MongoCollectionUtils.getPreferredCollectionName(Person.class), persons); + template.insert(persons, MongoCollectionUtils.getPreferredCollectionName(Person.class)); List result = template.find(new Query(Criteria.where("ssn").is(1234567890)), Person.class); assertThat(result.size(), is(1)); @@ -225,12 +225,12 @@ public class MappingTests { List persons = new ArrayList(); persons.add(new PersonCustomCollection1(55555, "Person", "One")); persons.add(new PersonCustomCollection2(66666, "Person", "Two")); - template.insertList(persons); + template.insertAll(persons); - List p1Results = template.find("person1", new Query(Criteria.where("ssn").is(55555)), - PersonCustomCollection1.class); - List p2Results = template.find("person2", new Query(Criteria.where("ssn").is(66666)), - PersonCustomCollection2.class); + List p1Results = template.find(new Query(Criteria.where("ssn").is(55555)), PersonCustomCollection1.class, + "person1"); + List p2Results = template.find(new Query(Criteria.where("ssn").is(66666)), PersonCustomCollection2.class, + "person2"); assertThat(p1Results.size(), is(1)); assertThat(p2Results.size(), is(1)); } @@ -240,7 +240,7 @@ public class MappingTests { Location loc = new Location(new double[]{1.0, 2.0}, new int[]{1, 2, 3, 4}, new float[]{1.0f, 2.0f}); template.insert(loc); - List result = template.find("places", new Query(Criteria.where("_id").is(loc.getId())), Location.class); + List result = template.find(new Query(Criteria.where("_id").is(loc.getId())), Location.class, "places"); assertThat(result.size(), is(1)); }