DATADOC-160 refactoring to make parameter ordering more consistent, especially for string parameter specifying collection name override; renamed insertList methods; removed unnecessary methods
This commit is contained in:
@@ -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> T executeInSession(DbCallback<T> 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
|
||||
*/
|
||||
<T> DBCollection createCollection(Class<T> 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
|
||||
*/
|
||||
<T> DBCollection createCollection(Class<T> 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.
|
||||
* <p/>
|
||||
* 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.
|
||||
*/
|
||||
<T> boolean collectionExists(Class<T> entityClass);
|
||||
|
||||
/**
|
||||
* Check to see if a collection with a given name exists.
|
||||
* <p/>
|
||||
@@ -171,6 +203,16 @@ public interface MongoOperations {
|
||||
*/
|
||||
boolean collectionExists(String collectionName);
|
||||
|
||||
/**
|
||||
* Drop the collection with the name indicated by the entity class.
|
||||
* <p/>
|
||||
* Translate any exceptions as necessary.
|
||||
*
|
||||
* @param entityClass
|
||||
* class that determines the collection to drop/delete.
|
||||
*/
|
||||
<T> void dropCollection(Class<T> entityClass);
|
||||
|
||||
/**
|
||||
* Drop the collection with the given name.
|
||||
* <p/>
|
||||
@@ -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.
|
||||
* <p/>
|
||||
* 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
|
||||
*/
|
||||
<T> List<T> getCollection(Class<T> entityClass);
|
||||
<T> List<T> findAll(Class<T> 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
|
||||
*/
|
||||
<T> List<T> getCollection(String collectionName, Class<T> entityClass);
|
||||
<T> List<T> findAll(Class<T> 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.
|
||||
* <p/>
|
||||
* 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 {
|
||||
* <p/>
|
||||
* 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> T findOne(String collectionName, Query query, Class<T> entityClass);
|
||||
<T> T findOne(Query query, Class<T> 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.
|
||||
* <p/>
|
||||
* 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 {
|
||||
* <p/>
|
||||
* 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
|
||||
*/
|
||||
<T> List<T> find(String collectionName, Query query, Class<T> entityClass);
|
||||
<T> List<T> find(Query query, Class<T> 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 {
|
||||
* <p/>
|
||||
* 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.
|
||||
*/
|
||||
<T> List<T> find(String collectionName, Query query, Class<T> entityClass, CursorPreparer preparer);
|
||||
<T> List<T> find(Query query, Class<T> 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 <T>
|
||||
* @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 <T>
|
||||
* @return
|
||||
*/
|
||||
<T> T findById(String collectionName, Object id, Class<T> entityClass);
|
||||
|
||||
<T> T findById(Object id, Class<T> 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.
|
||||
* <p/>
|
||||
* 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}.
|
||||
* <p/>
|
||||
* 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> T findAndRemove(Query query, Class<T> entityClass);
|
||||
@@ -383,23 +424,22 @@ public interface MongoOperations {
|
||||
* <p/>
|
||||
* 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> T findAndRemove(String collectionName, Query query, Class<T> entityClass);
|
||||
<T> T findAndRemove(Query query, Class<T> 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.
|
||||
* <p/>
|
||||
* 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}.
|
||||
* <p/>
|
||||
* 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.
|
||||
* <p/>
|
||||
* 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<? extends Object> listToSave);
|
||||
void insert(Collection<? extends Object> 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<? extends Object> listToSave);
|
||||
void insert(Collection<? extends Object> 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<? extends Object> 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'.
|
||||
* <p/>
|
||||
* 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 <a href="http://static.springsource.org/spring/docs/3.0.x/reference/validation.html#core-convert">Spring 3 Type
|
||||
* Conversion"</a> 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 <T>
|
||||
* @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
|
||||
*/
|
||||
<T> void remove(String collectionName, Query query, Class<T> entityClass);
|
||||
void remove(Query query, String collectionName);
|
||||
|
||||
}
|
||||
@@ -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<String> ITERABLE_CLASSES = new ArrayList<String>() {
|
||||
{
|
||||
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 <T> DBCollection createCollection(Class<T> 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 <T> DBCollection createCollection(Class<T> 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 <T> boolean collectionExists(Class<T> 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 <T> void dropCollection(Class<T> 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<Object>() {
|
||||
public Object doInCollection(DBCollection collection)
|
||||
throws MongoException, DataAccessException {
|
||||
@@ -494,11 +540,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
||||
// single object.
|
||||
|
||||
public <T> T findOne(Query query, Class<T> entityClass) {
|
||||
return findOne(determineCollectionName(entityClass), query, entityClass);
|
||||
return findOne(query, entityClass, determineCollectionName(entityClass));
|
||||
}
|
||||
|
||||
public <T> T findOne(String collectionName, Query query,
|
||||
Class<T> entityClass) {
|
||||
public <T> T findOne(Query query, Class<T> entityClass,
|
||||
String collectionName) {
|
||||
return doFindOne(collectionName, query.getQueryObject(),
|
||||
query.getFieldsObject(), entityClass);
|
||||
}
|
||||
@@ -508,11 +554,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
||||
// of objects.
|
||||
|
||||
public <T> List<T> find(Query query, Class<T> entityClass) {
|
||||
return find(determineCollectionName(entityClass), query, entityClass);
|
||||
return find(query, entityClass, determineCollectionName(entityClass));
|
||||
}
|
||||
|
||||
public <T> List<T> find(String collectionName, final Query query,
|
||||
Class<T> entityClass) {
|
||||
public <T> List<T> find(final Query query, Class<T> 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 <T> List<T> find(String collectionName, Query query,
|
||||
Class<T> entityClass, CursorPreparer preparer) {
|
||||
public <T> List<T> find(Query query, Class<T> 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> T findById(Object id, Class<T> entityClass) {
|
||||
MongoPersistentEntity<?> persistentEntity = mappingContext
|
||||
.getPersistentEntity(entityClass);
|
||||
return findById(persistentEntity.getCollection(), id, entityClass);
|
||||
return findById(id, entityClass, persistentEntity.getCollection());
|
||||
}
|
||||
|
||||
public <T> T findById(String collectionName, Object id, Class<T> entityClass) {
|
||||
public <T> T findById(Object id, Class<T> 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> T findAndRemove(Query query, Class<T> entityClass) {
|
||||
return findAndRemove(determineCollectionName(entityClass), query,
|
||||
entityClass);
|
||||
return findAndRemove(query, entityClass,
|
||||
determineCollectionName(entityClass));
|
||||
}
|
||||
|
||||
public <T> T findAndRemove(String collectionName, Query query,
|
||||
Class<T> entityClass) {
|
||||
public <T> T findAndRemove(Query query, Class<T> 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<? extends Object> listToSave) {
|
||||
doInsertList(listToSave, mongoConverter);
|
||||
public void insert(Collection<? extends Object> 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<? extends Object> listToSave) {
|
||||
doInsertList(collectionName, listToSave, this.mongoConverter);
|
||||
public void insert(Collection<? extends Object> batchToSave,
|
||||
String collectionName) {
|
||||
doInsertBatch(collectionName, batchToSave, this.mongoConverter);
|
||||
}
|
||||
|
||||
protected <T> void doInsertList(List<? extends T> listToSave,
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.document.mongodb.MongoOperations#insertAll(java
|
||||
* .util.Collection)
|
||||
*/
|
||||
public void insertAll(Collection<? extends Object> objectsToSave) {
|
||||
doInsertAll(objectsToSave, this.mongoConverter);
|
||||
}
|
||||
|
||||
protected <T> void doInsertAll(Collection<? extends T> listToSave,
|
||||
MongoWriter<T> writer) {
|
||||
Map<String, List<T>> objs = new HashMap<String, List<T>>();
|
||||
|
||||
@@ -699,17 +756,17 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
||||
}
|
||||
|
||||
for (Map.Entry<String, List<T>> entry : objs.entrySet()) {
|
||||
doInsertList(entry.getKey(), entry.getValue(), this.mongoConverter);
|
||||
doInsertBatch(entry.getKey(), entry.getValue(), this.mongoConverter);
|
||||
}
|
||||
}
|
||||
|
||||
protected <T> void doInsertList(String collectionName,
|
||||
List<? extends T> listToSave, MongoWriter<T> writer) {
|
||||
protected <T> void doInsertBatch(String collectionName,
|
||||
Collection<? extends T> batchToSave, MongoWriter<T> writer) {
|
||||
|
||||
Assert.notNull(writer);
|
||||
|
||||
List<DBObject> dbObjectList = new ArrayList<DBObject>();
|
||||
for (T o : listToSave) {
|
||||
for (T o : batchToSave) {
|
||||
BasicDBObject dbDoc = new BasicDBObject();
|
||||
|
||||
maybeEmitEvent(new BeforeConvertEvent<T>(o));
|
||||
@@ -719,12 +776,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
||||
dbObjectList.add(dbDoc);
|
||||
}
|
||||
List<ObjectId> 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<T>(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 <T> void remove(Query query, Class<T> entityClass) {
|
||||
Assert.notNull(query);
|
||||
remove(determineCollectionName(entityClass), query, entityClass);
|
||||
doRemove(determineCollectionName(entityClass), query, entityClass);
|
||||
}
|
||||
|
||||
public <T> void remove(String collectionName, final Query query,
|
||||
protected <T> void doRemove(String collectionName, final Query query,
|
||||
Class<T> 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 <T> List<T> getCollection(Class<T> entityClass) {
|
||||
public <T> List<T> findAll(Class<T> entityClass) {
|
||||
return executeFindMultiInternal(new FindCallback(null), null,
|
||||
new ReadDbObjectCallback<T>(mongoConverter, entityClass),
|
||||
determineCollectionName(entityClass));
|
||||
}
|
||||
|
||||
public <T> List<T> getCollection(String collectionName, Class<T> entityClass) {
|
||||
public <T> List<T> findAll(Class<T> entityClass, String collectionName) {
|
||||
return executeFindMultiInternal(new FindCallback(null), null,
|
||||
new ReadDbObjectCallback<T>(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 <T> 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();
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -297,7 +297,7 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, 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));
|
||||
}
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> 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<T, ID extends Serializable> 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<T, ID extends Serializable> implements Paging
|
||||
*/
|
||||
public void deleteAll() {
|
||||
|
||||
template.remove(entityInformation.getCollectionName(), new Query());
|
||||
template.remove(new Query(), entityInformation.getCollectionName());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -245,7 +245,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return template.find(entityInformation.getCollectionName(), query, entityInformation.getJavaType());
|
||||
return template.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<Venue> venues = template.getCollection(Venue.class);
|
||||
List<Venue> venues = template.findAll(Venue.class);
|
||||
assertThat(venues.size(), equalTo(12));
|
||||
Collection<?> names = (Collection<?>) parser.parseExpression("![name]").getValue(venues);
|
||||
assertThat(names.size(), equalTo(12));
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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<DBObject> 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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -63,7 +63,7 @@ public class PersonExample {
|
||||
p = mongoOps.findById(p.getId(), PersonWithIdPropertyOfTypeString.class);
|
||||
log.debug("Updated: " + p);
|
||||
|
||||
List<PersonWithIdPropertyOfTypeString> folks = mongoOps.getCollection(PersonWithIdPropertyOfTypeString.class);
|
||||
List<PersonWithIdPropertyOfTypeString> 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<PersonWithIdPropertyOfTypeString> people = mongoOps.getCollection(PersonWithIdPropertyOfTypeString.class);
|
||||
List<PersonWithIdPropertyOfTypeString> people = mongoOps.findAll(PersonWithIdPropertyOfTypeString.class);
|
||||
|
||||
// PersonWithIdPropertyOfTypeString p2 = mongoOps.findOne(query(whereId().is(p.getId())),
|
||||
// PersonWithIdPropertyOfTypeString.class);
|
||||
|
||||
@@ -175,21 +175,21 @@ public class MappingTests {
|
||||
|
||||
Account acct = new Account();
|
||||
acct.setBalance(1000.00f);
|
||||
template.insert("account", acct);
|
||||
template.insert(acct, "account");
|
||||
|
||||
List<Account> accounts = new ArrayList<Account>();
|
||||
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<Person> persons = new ArrayList<Person>();
|
||||
persons.add(p1);
|
||||
persons.add(p2);
|
||||
template.insertList(MongoCollectionUtils.getPreferredCollectionName(Person.class), persons);
|
||||
template.insert(persons, MongoCollectionUtils.getPreferredCollectionName(Person.class));
|
||||
|
||||
List<Person> 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<BasePerson> persons = new ArrayList<BasePerson>();
|
||||
persons.add(new PersonCustomCollection1(55555, "Person", "One"));
|
||||
persons.add(new PersonCustomCollection2(66666, "Person", "Two"));
|
||||
template.insertList(persons);
|
||||
template.insertAll(persons);
|
||||
|
||||
List<PersonCustomCollection1> p1Results = template.find("person1", new Query(Criteria.where("ssn").is(55555)),
|
||||
PersonCustomCollection1.class);
|
||||
List<PersonCustomCollection2> p2Results = template.find("person2", new Query(Criteria.where("ssn").is(66666)),
|
||||
PersonCustomCollection2.class);
|
||||
List<PersonCustomCollection1> p1Results = template.find(new Query(Criteria.where("ssn").is(55555)), PersonCustomCollection1.class,
|
||||
"person1");
|
||||
List<PersonCustomCollection2> 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<Location> result = template.find("places", new Query(Criteria.where("_id").is(loc.getId())), Location.class);
|
||||
List<Location> result = template.find(new Query(Criteria.where("_id").is(loc.getId())), Location.class, "places");
|
||||
assertThat(result.size(), is(1));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user