DATADOC-117 removed defaultCollection on MongoTemplate/Operations; changed getDefaultCollectionName to getCollectionName(Class clazz) to determine collection name used for specific class; added a class parameter where necessary
This commit is contained in:
@@ -28,7 +28,6 @@
|
||||
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
|
||||
<constructor-arg name="mongo" ref="mongo"/>
|
||||
<constructor-arg name="databaseName" value="test"/>
|
||||
<constructor-arg name="defaultCollectionName" value="cross-store"/>
|
||||
<constructor-arg name="mongoConverter" ref="mappingConverter"/>
|
||||
</bean>
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.WriteResult;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.data.document.mongodb.index.IndexDefinition;
|
||||
import org.springframework.data.document.mongodb.query.Query;
|
||||
import org.springframework.data.document.mongodb.query.Update;
|
||||
@@ -40,18 +39,11 @@ import org.springframework.data.document.mongodb.query.Update;
|
||||
public interface MongoOperations {
|
||||
|
||||
/**
|
||||
* The default collection name used by this template.
|
||||
* The collection name used for the specified class by this template.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getDefaultCollectionName();
|
||||
|
||||
/**
|
||||
* The default collection used by this template.
|
||||
*
|
||||
* @return The default collection used by this template
|
||||
*/
|
||||
DBCollection getDefaultCollection();
|
||||
String getCollectionName(Class<?> clazz);
|
||||
|
||||
/**
|
||||
* Execute the a MongoDB command expressed as a JSON string. This will call the method
|
||||
@@ -83,15 +75,16 @@ public interface MongoOperations {
|
||||
<T> T execute(DbCallback<T> action);
|
||||
|
||||
/**
|
||||
* Executes the given {@link CollectionCallback} on the default collection.
|
||||
* Executes the given {@link CollectionCallback} on the entity collection of the specified class.
|
||||
* <p/>
|
||||
* Allows for returning a result object, that is a domain object or a collection of domain objects.
|
||||
*
|
||||
* @param entityClass class that determines the collection to use
|
||||
* @param <T> return type
|
||||
* @param action callback object that specifies the MongoDB action
|
||||
* @return a result object returned by the action or <tt>null</tt>
|
||||
*/
|
||||
<T> T execute(CollectionCallback<T> action);
|
||||
<T> T execute(Class<?> entityClass, CollectionCallback<T> action);
|
||||
|
||||
/**
|
||||
* Executes the given {@link CollectionCallback} on the collection of the given name.
|
||||
@@ -219,9 +212,10 @@ public interface MongoOperations {
|
||||
* Ensure that an index for the provided {@link IndexDefinition} exists for the default collection.
|
||||
* If not it will be created.
|
||||
*
|
||||
* @param index
|
||||
* @param entityClass class that determines the collection to use
|
||||
* @param indexDefinition
|
||||
*/
|
||||
void ensureIndex(IndexDefinition indexDefinition);
|
||||
void ensureIndex(Class<?> entityClass, IndexDefinition indexDefinition);
|
||||
|
||||
/**
|
||||
* Ensure that an index for the provided {@link IndexDefinition} exists. If not it will be
|
||||
@@ -651,11 +645,12 @@ public interface MongoOperations {
|
||||
* Updates the first object that is found in the default collection that matches the query document
|
||||
* with the provided updated document.
|
||||
*
|
||||
* @param entityClass class that determines the collection to use
|
||||
* @param queryDoc the query document that specifies the criteria used to select a record to be updated
|
||||
* @param updateDoc the update document that contains the updated object or $ operators to manipulate the
|
||||
* existing object.
|
||||
*/
|
||||
WriteResult updateFirst(Query query, Update update);
|
||||
WriteResult updateFirst(Class<?> entityClass, Query query, Update update);
|
||||
|
||||
/**
|
||||
* Updates the first object that is found in the specified collection that matches the query document criteria
|
||||
@@ -673,11 +668,12 @@ public interface MongoOperations {
|
||||
* Updates all objects that are found in the default collection that matches the query document criteria
|
||||
* with the provided updated document.
|
||||
*
|
||||
* @param entityClass class that determines the collection to use
|
||||
* @param queryDoc the query document that specifies the criteria used to select a record to be updated
|
||||
* @param updateDoc the update document that contains the updated object or $ operators to manipulate the
|
||||
* existing object.
|
||||
*/
|
||||
WriteResult updateMulti(Query query, Update update);
|
||||
WriteResult updateMulti(Class<?> entityClass, Query query, Update update);
|
||||
|
||||
/**
|
||||
* Updates all objects that are found in the specified collection that matches the query document criteria
|
||||
|
||||
@@ -25,12 +25,10 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.mongodb.BasicDBList;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.CommandResult;
|
||||
import com.mongodb.DB;
|
||||
@@ -45,7 +43,6 @@ import com.mongodb.util.JSON;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
@@ -83,7 +80,7 @@ import org.springframework.util.Assert;
|
||||
* @author Mark Pollack
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class MongoTemplate implements InitializingBean, MongoOperations, ApplicationEventPublisherAware {
|
||||
public class MongoTemplate implements MongoOperations, ApplicationEventPublisherAware {
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(MongoTemplate.class);
|
||||
|
||||
@@ -107,7 +104,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
private final MongoExceptionTranslator exceptionTranslator = new MongoExceptionTranslator();
|
||||
private final QueryMapper mapper;
|
||||
|
||||
private String defaultCollectionName;
|
||||
private String databaseName;
|
||||
private String username;
|
||||
private String password;
|
||||
@@ -120,34 +116,22 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
* @param databaseName
|
||||
*/
|
||||
public MongoTemplate(Mongo mongo, String databaseName) {
|
||||
this(mongo, databaseName, null, null, null, null);
|
||||
this(mongo, databaseName, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor used for a basic template configuration with a default collection name
|
||||
* Constructor used for a template configuration with a custom {@link org.springframework.data.document.mongodb.convert.MongoConverter}
|
||||
*
|
||||
* @param mongo
|
||||
* @param databaseName
|
||||
* @param defaultCollectionName
|
||||
*/
|
||||
public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName) {
|
||||
this(mongo, databaseName, defaultCollectionName, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor used for a template configuration with a default collection name and a custom {@link org.springframework.data.document.mongodb.convert.MongoConverter}
|
||||
*
|
||||
* @param mongo
|
||||
* @param databaseName
|
||||
* @param defaultCollectionName
|
||||
* @param mongoConverter
|
||||
*/
|
||||
public MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName, MongoConverter mongoConverter) {
|
||||
this(mongo, databaseName, defaultCollectionName, mongoConverter, null, null);
|
||||
public MongoTemplate(Mongo mongo, String databaseName, MongoConverter mongoConverter) {
|
||||
this(mongo, databaseName, mongoConverter, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor used for a template configuration with a default collection name and a custom {@link MongoConverter}
|
||||
* Constructor used for a template configuration with a custom {@link MongoConverter}
|
||||
* and with a specific {@link com.mongodb.WriteConcern} to be used for all database write operations
|
||||
*
|
||||
* @param mongo
|
||||
@@ -158,12 +142,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
* @param writeResultChecking
|
||||
*/
|
||||
@SuppressWarnings({"unchecked"})
|
||||
MongoTemplate(Mongo mongo, String databaseName, String defaultCollectionName, MongoConverter mongoConverter, WriteConcern writeConcern, WriteResultChecking writeResultChecking) {
|
||||
MongoTemplate(Mongo mongo, String databaseName, MongoConverter mongoConverter, WriteConcern writeConcern, WriteResultChecking writeResultChecking) {
|
||||
|
||||
Assert.notNull(mongo);
|
||||
Assert.notNull(databaseName);
|
||||
|
||||
this.defaultCollectionName = defaultCollectionName;
|
||||
this.mongo = mongo;
|
||||
this.databaseName = databaseName;
|
||||
this.writeConcern = writeConcern;
|
||||
@@ -211,15 +194,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name of the default collection to be used.
|
||||
*
|
||||
* @param defaultCollectionName
|
||||
*/
|
||||
public void setDefaultCollectionName(String defaultCollectionName) {
|
||||
this.defaultCollectionName = defaultCollectionName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the database name to be used.
|
||||
*
|
||||
@@ -242,20 +216,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.document.mongodb.MongoOperations#getDefaultCollectionName()
|
||||
*/
|
||||
public String getDefaultCollectionName() {
|
||||
return defaultCollectionName;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.document.mongodb.MongoOperations#getDefaultCollection()
|
||||
*/
|
||||
public DBCollection getDefaultCollection() {
|
||||
|
||||
return execute(new DbCallback<DBCollection>() {
|
||||
public DBCollection doInDB(DB db) throws MongoException, DataAccessException {
|
||||
return db.getCollection(getDefaultCollectionName());
|
||||
}
|
||||
});
|
||||
public String getCollectionName(Class<?> clazz) {
|
||||
return this.determineCollectionName(clazz);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -305,8 +267,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.CollectionCallback)
|
||||
*/
|
||||
public <T> T execute(CollectionCallback<T> callback) {
|
||||
return execute(getDefaultCollectionName(), callback);
|
||||
public <T> T execute(Class<?> entityClass, CollectionCallback<T> callback) {
|
||||
return execute(determineCollectionName(entityClass), callback);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -452,8 +414,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
|
||||
// Indexing methods
|
||||
|
||||
public void ensureIndex(IndexDefinition indexDefinition) {
|
||||
ensureIndex(getDefaultCollectionName(), indexDefinition);
|
||||
public void ensureIndex(Class<?> entityClass, IndexDefinition indexDefinition) {
|
||||
ensureIndex(determineCollectionName(entityClass), indexDefinition);
|
||||
}
|
||||
|
||||
public void ensureIndex(String collectionName, final IndexDefinition indexDefinition) {
|
||||
@@ -473,12 +435,12 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
// Find methods that take a Query to express the query and that return a single object.
|
||||
|
||||
public <T> T findOne(Query query, Class<T> targetClass) {
|
||||
return findOne(getEntityCollection(targetClass), query, targetClass);
|
||||
return findOne(determineCollectionName(targetClass), query, targetClass);
|
||||
}
|
||||
|
||||
public <T> T findOne(Query query, Class<T> targetClass,
|
||||
MongoReader<T> reader) {
|
||||
return findOne(getEntityCollection(targetClass), query, targetClass, reader);
|
||||
return findOne(determineCollectionName(targetClass), query, targetClass, reader);
|
||||
}
|
||||
|
||||
public <T> T findOne(String collectionName, Query query,
|
||||
@@ -494,11 +456,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
// Find methods that take a Query to express the query and that return a List of objects.
|
||||
|
||||
public <T> List<T> find(Query query, Class<T> targetClass) {
|
||||
return find(getEntityCollection(targetClass), query, targetClass);
|
||||
return find(determineCollectionName(targetClass), query, targetClass);
|
||||
}
|
||||
|
||||
public <T> List<T> find(Query query, Class<T> targetClass, MongoReader<T> reader) {
|
||||
return find(getEntityCollection(targetClass), query, targetClass, reader);
|
||||
return find(determineCollectionName(targetClass), query, targetClass, reader);
|
||||
}
|
||||
|
||||
public <T> List<T> find(String collectionName, final Query query, Class<T> targetClass) {
|
||||
@@ -541,12 +503,12 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
// also removed from the collection in the database.
|
||||
|
||||
public <T> T findAndRemove(Query query, Class<T> targetClass) {
|
||||
return findAndRemove(getEntityCollection(targetClass), query, targetClass);
|
||||
return findAndRemove(determineCollectionName(targetClass), query, targetClass);
|
||||
}
|
||||
|
||||
public <T> T findAndRemove(Query query, Class<T> targetClass,
|
||||
MongoReader<T> reader) {
|
||||
return findAndRemove(getEntityCollection(targetClass), query, targetClass, reader);
|
||||
return findAndRemove(determineCollectionName(targetClass), query, targetClass, reader);
|
||||
}
|
||||
|
||||
public <T> T findAndRemove(String collectionName, Query query,
|
||||
@@ -563,7 +525,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
* @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.Object)
|
||||
*/
|
||||
public void insert(Object objectToSave) {
|
||||
insert(getEntityCollection(objectToSave), objectToSave);
|
||||
insert(determineEntityCollectionName(objectToSave), objectToSave);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -577,7 +539,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
* @see org.springframework.data.document.mongodb.MongoOperations#insert(T, org.springframework.data.document.mongodb.MongoWriter)
|
||||
*/
|
||||
public <T> void insert(T objectToSave, MongoWriter<T> writer) {
|
||||
insert(getEntityCollection(objectToSave), objectToSave, writer);
|
||||
insert(determineEntityCollectionName(objectToSave), objectToSave, writer);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -619,7 +581,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
for (Object o : listToSave) {
|
||||
|
||||
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(o.getClass());
|
||||
String collection = entity == null ? getDefaultCollectionName() : entity.getCollection();
|
||||
if (entity == null) {
|
||||
throw new InvalidDataAccessApiUsageException("No Persitent Entity information found for the class " +
|
||||
o.getClass().getName());
|
||||
}
|
||||
String collection = entity.getCollection();
|
||||
|
||||
List<Object> objList = objs.get(collection);
|
||||
if (null == objList) {
|
||||
@@ -666,7 +632,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
* @see org.springframework.data.document.mongodb.MongoOperations#save(java.lang.Object)
|
||||
*/
|
||||
public void save(Object objectToSave) {
|
||||
save(getEntityCollection(objectToSave), objectToSave);
|
||||
save(determineEntityCollectionName(objectToSave), objectToSave);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -680,7 +646,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
* @see org.springframework.data.document.mongodb.MongoOperations#save(T, org.springframework.data.document.mongodb.MongoWriter)
|
||||
*/
|
||||
public <T> void save(T objectToSave, MongoWriter<T> writer) {
|
||||
save(getEntityCollection(objectToSave), objectToSave, writer);
|
||||
save(determineEntityCollectionName(objectToSave), objectToSave, writer);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -808,8 +774,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(com.mongodb.DBObject, com.mongodb.DBObject)
|
||||
*/
|
||||
public WriteResult updateFirst(Query query, Update update) {
|
||||
return updateFirst(getRequiredDefaultCollectionName(), query, update);
|
||||
public WriteResult updateFirst(Class<?> entityClass, Query query, Update update) {
|
||||
return updateFirst(determineCollectionName(entityClass), query, update);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -838,8 +804,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(com.mongodb.DBObject, com.mongodb.DBObject)
|
||||
*/
|
||||
public WriteResult updateMulti(Query query, Update update) {
|
||||
return updateMulti(getRequiredDefaultCollectionName(), query, update);
|
||||
public WriteResult updateMulti(Class<?> entityClass, Query query, Update update) {
|
||||
return updateMulti(determineCollectionName(entityClass), query, update);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -878,7 +844,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
|
||||
public <T> void remove(Query query, Class<T> targetClass) {
|
||||
Assert.notNull(query);
|
||||
remove(getEntityCollection(targetClass), query, targetClass);
|
||||
remove(determineCollectionName(targetClass), query, targetClass);
|
||||
}
|
||||
|
||||
private PersistentEntity<?> getPersistentEntity(Class<?> type) {
|
||||
@@ -922,7 +888,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
*/
|
||||
public <T> List<T> getCollection(Class<T> targetClass) {
|
||||
return executeEach(new FindCallback(null), null, new ReadDbObjectCallback<T>(mongoConverter, targetClass),
|
||||
getDefaultCollectionName());
|
||||
determineCollectionName(targetClass));
|
||||
}
|
||||
|
||||
public <T> List<T> getCollection(String collectionName, Class<T> targetClass) {
|
||||
@@ -1233,30 +1199,24 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
return newValue;
|
||||
}
|
||||
|
||||
private String getRequiredDefaultCollectionName() {
|
||||
String name = getDefaultCollectionName();
|
||||
if (name == null) {
|
||||
throw new IllegalStateException(
|
||||
"No 'defaultCollection' or 'defaultCollectionName' specified. Check configuration of MongoTemplate.");
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
private <T> String getEntityCollection(T obj) {
|
||||
private <T> String determineEntityCollectionName(T obj) {
|
||||
if (null != obj) {
|
||||
return getEntityCollection(obj.getClass());
|
||||
return determineCollectionName(obj.getClass());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getEntityCollection(Class<?> clazz) {
|
||||
private String determineCollectionName(Class<?> clazz) {
|
||||
|
||||
if (clazz == null) {
|
||||
return getDefaultCollectionName();
|
||||
throw new InvalidDataAccessApiUsageException("No class parameter provided, entity collection can't be determined for " + clazz);
|
||||
}
|
||||
|
||||
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(clazz);
|
||||
if (entity == null) {
|
||||
throw new InvalidDataAccessApiUsageException("No Persitent Entity information found for the class " + clazz.getName());
|
||||
}
|
||||
return entity.getCollection();
|
||||
}
|
||||
|
||||
@@ -1309,18 +1269,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
converter.setDefaultDatabase(databaseName);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() {
|
||||
if (this.getDefaultCollectionName() != null) {
|
||||
if (!collectionExists(getDefaultCollectionName())) {
|
||||
createCollection(getDefaultCollectionName(), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Simple {@link CollectionCallback} that takes a query {@link DBObject} plus an optional fields specification
|
||||
|
||||
@@ -33,7 +33,7 @@ public class GeoSpatialAppConfig extends AbstractMongoConfiguration {
|
||||
|
||||
@Bean
|
||||
public MongoTemplate mongoTemplate() throws Exception {
|
||||
return new MongoTemplate(mongo(), "geospatial", "newyork", mappingMongoConverter());
|
||||
return new MongoTemplate(mongo(), "geospatial", mappingMongoConverter());
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -78,7 +78,7 @@ public class GeoSpatialTests {
|
||||
applicationContext = new AnnotationConfigApplicationContext(GeoSpatialAppConfig.class);
|
||||
template = applicationContext.getBean(MongoTemplate.class);
|
||||
template.setWriteConcern(WriteConcern.FSYNC_SAFE);
|
||||
template.ensureIndex(new GeospatialIndex("location"));
|
||||
template.ensureIndex(Venue.class, new GeospatialIndex("location"));
|
||||
indexCreated();
|
||||
addVenues();
|
||||
parser = new SpelExpressionParser();
|
||||
@@ -164,7 +164,7 @@ public class GeoSpatialTests {
|
||||
}
|
||||
|
||||
public void indexCreated() {
|
||||
List<DBObject> indexInfo = getIndexInfo();
|
||||
List<DBObject> indexInfo = getIndexInfo(Venue.class);
|
||||
LOGGER.debug(indexInfo);
|
||||
assertThat(indexInfo.size(), equalTo(2));
|
||||
assertThat(indexInfo.get(1).get("name").toString(), equalTo("location_2d"));
|
||||
@@ -173,8 +173,8 @@ public class GeoSpatialTests {
|
||||
}
|
||||
|
||||
// TODO move to MongoAdmin
|
||||
public List<DBObject> getIndexInfo() {
|
||||
return template.execute(new CollectionCallback<List<DBObject>>() {
|
||||
public List<DBObject> getIndexInfo(Class clazz) {
|
||||
return template.execute(clazz, new CollectionCallback<List<DBObject>>() {
|
||||
|
||||
public List<DBObject> doInCollection(DBCollection collection)
|
||||
throws MongoException, DataAccessException {
|
||||
|
||||
@@ -88,7 +88,7 @@ public abstract class MongoOperationsUnitTests {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void rejectsNullForCollectionCallback() {
|
||||
|
||||
getOperations().execute((CollectionCallback) null);
|
||||
getOperations().execute("test", (CollectionCallback) null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -148,7 +148,7 @@ public abstract class MongoOperationsUnitTests {
|
||||
new Execution() {
|
||||
@Override
|
||||
public void doWith(MongoOperations operations) {
|
||||
operations.execute(collectionCallback);
|
||||
operations.execute("test", collectionCallback);
|
||||
}
|
||||
}.assertDataAccessException();
|
||||
}
|
||||
@@ -253,16 +253,6 @@ public abstract class MongoOperationsUnitTests {
|
||||
}.assertDataAccessException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertsExceptionForGetDefaultCollection() {
|
||||
new Execution() {
|
||||
@Override
|
||||
public void doWith(MongoOperations operations) {
|
||||
operations.getDefaultCollection();
|
||||
}
|
||||
}.assertDataAccessException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertsExceptionForInsert() {
|
||||
new Execution() {
|
||||
|
||||
@@ -84,12 +84,16 @@ public class MongoTemplateTests {
|
||||
MappingMongoConverter converter = new MappingMongoConverter(mappingContext);
|
||||
converter.afterPropertiesSet();
|
||||
|
||||
this.mappingTemplate = new MongoTemplate(mongo, "database", "springdata", converter);
|
||||
this.mappingTemplate = new MongoTemplate(mongo, "database", converter);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
template.dropCollection(template.getDefaultCollectionName());
|
||||
template.dropCollection(template.getCollectionName(Person.class));
|
||||
template.dropCollection(template.getCollectionName(PersonWith_idPropertyOfTypeObjectId.class));
|
||||
template.dropCollection(template.getCollectionName(PersonWith_idPropertyOfTypeString.class));
|
||||
template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfTypeObjectId.class));
|
||||
template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfTypeString.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -109,7 +113,7 @@ public class MongoTemplateTests {
|
||||
@Test
|
||||
public void updateFailure() throws Exception {
|
||||
|
||||
MongoTemplate mongoTemplate = new MongoTemplate(template.getDb().getMongo(), "test", "people");
|
||||
MongoTemplate mongoTemplate = new MongoTemplate(template.getDb().getMongo(), "test");
|
||||
mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
|
||||
|
||||
Person person = new Person("Oliver2");
|
||||
@@ -120,7 +124,7 @@ public class MongoTemplateTests {
|
||||
Update u = new Update().set("firstName", "Sven");
|
||||
thrown.expect(DataIntegrityViolationException.class);
|
||||
thrown.expectMessage(endsWith("0 documents updated"));
|
||||
mongoTemplate.updateFirst(q, u);
|
||||
mongoTemplate.updateFirst(Person.class, q, u);
|
||||
|
||||
}
|
||||
|
||||
@@ -134,9 +138,9 @@ public class MongoTemplateTests {
|
||||
p2.setAge(40);
|
||||
template.insert(p2);
|
||||
|
||||
template.ensureIndex(new Index().on("age", Order.DESCENDING).unique(Duplicates.DROP));
|
||||
template.ensureIndex(Person.class, new Index().on("age", Order.DESCENDING).unique(Duplicates.DROP));
|
||||
|
||||
DBCollection coll = template.getCollection(template.getDefaultCollectionName());
|
||||
DBCollection coll = template.getCollection(template.getCollectionName(Person.class));
|
||||
List<DBObject> indexInfo = coll.getIndexInfo();
|
||||
|
||||
assertThat(indexInfo.size(), is(2));
|
||||
@@ -358,23 +362,23 @@ public class MongoTemplateTests {
|
||||
PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId();
|
||||
p1.setFirstName("Sven");
|
||||
p1.setAge(11);
|
||||
template.insert("springdata", p1);
|
||||
template.insert(p1);
|
||||
PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId();
|
||||
p2.setFirstName("Mary");
|
||||
p2.setAge(21);
|
||||
template.insert("springdata", p2);
|
||||
template.insert(p2);
|
||||
|
||||
Update u = new Update().set("firstName", "Bob").set("age", 10);
|
||||
|
||||
WriteResult wr = template.updateMulti("springdata", new Query(), u);
|
||||
WriteResult wr = template.updateMulti(PersonWithIdPropertyOfTypeObjectId.class, new Query(), u);
|
||||
|
||||
assertThat(wr.getN(), is(2));
|
||||
|
||||
Query q1 = new Query(Criteria.where("age").in(11, 21));
|
||||
List<PersonWithIdPropertyOfTypeObjectId> r1 = template.find("springdata", q1, PersonWithIdPropertyOfTypeObjectId.class);
|
||||
List<PersonWithIdPropertyOfTypeObjectId> r1 = template.find(q1, PersonWithIdPropertyOfTypeObjectId.class);
|
||||
assertThat(r1.size(), is(0));
|
||||
Query q2 = new Query(Criteria.where("age").is(10));
|
||||
List<PersonWithIdPropertyOfTypeObjectId> r2 = template.find("springdata", q2, PersonWithIdPropertyOfTypeObjectId.class);
|
||||
List<PersonWithIdPropertyOfTypeObjectId> r2 = template.find(q2, PersonWithIdPropertyOfTypeObjectId.class);
|
||||
assertThat(r2.size(), is(2));
|
||||
for (PersonWithIdPropertyOfTypeObjectId p : r2) {
|
||||
assertThat(p.getAge(), is(10));
|
||||
|
||||
@@ -48,7 +48,7 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.template = new MongoTemplate(mongo, "database", "default");
|
||||
this.template = new MongoTemplate(mongo, "database");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
|
||||
@@ -30,7 +30,7 @@ public class PersonExampleAppConfig {
|
||||
|
||||
@Bean
|
||||
public MongoTemplate mongoTemplate() throws Exception {
|
||||
return new MongoTemplate(mongo(), "database", "personexample");
|
||||
return new MongoTemplate(mongo(), "database");
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -23,10 +23,7 @@ public class MvcAnalyticsTests {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Mongo m = new Mongo();
|
||||
mongoTemplate = new MongoTemplate(m, "mvc", "mvc");
|
||||
mongoTemplate.afterPropertiesSet();
|
||||
|
||||
|
||||
mongoTemplate = new MongoTemplate(m, "mvc");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -17,7 +17,7 @@ public class GeoIndexedAppConfig extends AbstractMongoConfiguration {
|
||||
|
||||
@Bean
|
||||
public MongoTemplate mongoTemplate() throws Exception {
|
||||
return new MongoTemplate(mongo(), "geodb", "geolocation", mappingMongoConverter());
|
||||
return new MongoTemplate(mongo(), "geodb", mappingMongoConverter());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -334,7 +334,7 @@ public class MappingTests {
|
||||
template.insert(p);
|
||||
|
||||
addr.setCity("New Town");
|
||||
template.updateFirst(query(where("ssn").is(1111)), update("address", addr));
|
||||
template.updateFirst(Person.class, query(where("ssn").is(1111)), update("address", addr));
|
||||
|
||||
Person p2 = template.findOne(query(where("ssn").is(1111)), Person.class);
|
||||
assertThat(p2.getAddress().getCity(), is("New Town"));
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
|
||||
<constructor-arg ref="mongo"/>
|
||||
<constructor-arg name="databaseName" value="geospatial"/>
|
||||
<constructor-arg name="defaultCollectionName" value="newyork"/>
|
||||
<constructor-arg ref="mappingConverter"/>
|
||||
</bean>
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
|
||||
<constructor-arg ref="mongo"/>
|
||||
<constructor-arg value="database"/>
|
||||
<constructor-arg value="springdata"/>
|
||||
<constructor-arg ref="mongoConverter" />
|
||||
</bean>
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
|
||||
<constructor-arg ref="mongo"/>
|
||||
<constructor-arg name="databaseName" value="database"/>
|
||||
<constructor-arg name="defaultCollectionName" value="person"/>
|
||||
<constructor-arg ref="mappingConverter"/>
|
||||
</bean>
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
|
||||
<constructor-arg ref="mongo" />
|
||||
<constructor-arg value="database" />
|
||||
<constructor-arg value="collection" />
|
||||
<constructor-arg>
|
||||
<mongo:mapping-converter />
|
||||
</constructor-arg>
|
||||
|
||||
Reference in New Issue
Block a user