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 46b800dd0..268dad9c0 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 @@ -196,18 +196,6 @@ public interface MongoOperations { */ List getCollection(String collectionName, Class targetClass); - /** - * Query for a list of objects of type T from the specified collection, mapping the DBObject using - * the provided MongoReader. - * - * @param collectionName name of the collection to retrieve the objects from - * @param targetClass the parameterized type of the returned list. - * @param reader the MongoReader to convert from DBObject to an object. - * @return the converted collection - */ - List getCollection(String collectionName, Class targetClass, - MongoReader reader); - /** * Ensure that an index for the provided {@link IndexDefinition} exists for the default collection. * If not it will be created. @@ -243,25 +231,6 @@ public interface MongoOperations { */ T findOne(Query query, Class targetClass); - /** - * 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 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 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 - * @param targetClass the parameterized type of the returned list. - * @param reader the MongoReader to convert from DBObject to an object. - * @return the converted object - */ - T findOne(Query query, Class targetClass, - MongoReader reader); - /** * Map the results of an ad-hoc query on the specified collection to a single instance of an object * of the specified type. @@ -281,26 +250,6 @@ public interface MongoOperations { T findOne(String collectionName, Query query, Class targetClass); - /** - * Map the results of an ad-hoc query on the specified collection 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. - *

- * 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 targetClass the parameterized type of the returned list. - * @param reader the MongoReader to convert from DBObject to an object. - * @return the converted object - */ - T findOne(String collectionName, Query query, - Class targetClass, MongoReader reader); - /** * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. *

@@ -317,24 +266,6 @@ public interface MongoOperations { */ List find(Query query, Class targetClass); - /** - * Map the results of an ad-hoc query on the default MongoDB collection 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. - *

- * 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 - * @param targetClass the parameterized type of the returned list. - * @param reader the MongoReader to convert from DBObject to an object. - * @return the List of converted objects - */ - List find(Query query, Class targetClass, - MongoReader reader); - /** * Map the results of an ad-hoc query on the specified collection to a List of the specified type. *

@@ -353,26 +284,6 @@ public interface MongoOperations { List find(String collectionName, Query query, Class targetClass); - /** - * Map the results of an ad-hoc query on the specified collection 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. - *

- * 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 targetClass the parameterized type of the returned list. - * @param reader the MongoReader to convert from DBObject to an object. - * @return the List of converted objects - */ - List find(String collectionName, Query query, - Class targetClass, MongoReader reader); - - /** * Map the results of an ad-hoc query on the specified collection to a List of the specified type. *

@@ -410,26 +321,6 @@ public interface MongoOperations { */ T findAndRemove(Query query, Class targetClass); - /** - * 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. - *

- * 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 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 - * @param targetClass the parameterized type of the returned list. - * @param reader the MongoReader to convert from DBObject to an object. - * @return the converted object - */ - T findAndRemove(Query query, Class targetClass, - MongoReader reader); - /** * Map the results of an ad-hoc query on the specified 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 @@ -450,27 +341,6 @@ public interface MongoOperations { T findAndRemove(String collectionName, Query query, Class targetClass); - /** - * Map the results of an ad-hoc query on the specified 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. - *

- * 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 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 targetClass the parameterized type of the returned list. - * @param reader the MongoReader to convert from DBObject to an object. - * @return the converted object - */ - T findAndRemove(String collectionName, Query query, - Class targetClass, MongoReader reader); - /** * Insert the object into the default collection. *

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 fe9630a04..7f404b549 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 @@ -438,19 +438,9 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher return findOne(determineCollectionName(targetClass), query, targetClass); } - public T findOne(Query query, Class targetClass, - MongoReader reader) { - return findOne(determineCollectionName(targetClass), query, targetClass, reader); - } - public T findOne(String collectionName, Query query, Class targetClass) { - return findOne(collectionName, query, targetClass, null); - } - - public T findOne(String collectionName, Query query, - Class targetClass, MongoReader reader) { - return doFindOne(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, reader); + return doFindOne(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass); } // Find methods that take a Query to express the query and that return a List of objects. @@ -459,10 +449,6 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher return find(determineCollectionName(targetClass), query, targetClass); } - public List find(Query query, Class targetClass, MongoReader reader) { - return find(determineCollectionName(targetClass), query, targetClass, reader); - } - public List find(String collectionName, final Query query, Class targetClass) { CursorPreparer cursorPreparer = null; if (query.getSkip() > 0 || query.getLimit() > 0 || query.getSortObject() != null) { @@ -490,10 +476,6 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, cursorPreparer); } - public List find(String collectionName, Query query, Class targetClass, MongoReader reader) { - return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, reader); - } - public List find(String collectionName, Query query, Class targetClass, CursorPreparer preparer) { return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, preparer); @@ -506,19 +488,9 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher return findAndRemove(determineCollectionName(targetClass), query, targetClass); } - public T findAndRemove(Query query, Class targetClass, - MongoReader reader) { - return findAndRemove(determineCollectionName(targetClass), query, targetClass, reader); - } - public T findAndRemove(String collectionName, Query query, Class targetClass) { - return findAndRemove(collectionName, query, targetClass, null); - } - - public T findAndRemove(String collectionName, Query query, - Class targetClass, MongoReader reader) { - return doFindAndRemove(collectionName, query.getQueryObject(), query.getFieldsObject(), query.getSortObject(), targetClass, reader); + return doFindAndRemove(collectionName, query.getQueryObject(), query.getFieldsObject(), query.getSortObject(), targetClass); } /* (non-Javadoc) @@ -904,11 +876,6 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher }); } - public List getCollection(String collectionName, Class targetClass, MongoReader reader) { - return executeEach(new FindCallback(null), null, new ReadDbObjectCallback(reader, targetClass), - collectionName); - } - public DB getDb() { return MongoDbUtils.getDB(mongo, databaseName, username, password == null ? null : password.toCharArray()); } @@ -937,7 +904,7 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher } /** - * Map the results of an ad-hoc query on the default MongoDB collection to an object using the provided MongoReader + * Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter *

* The query document is specified as a standard DBObject and so is the fields specification. * @@ -945,14 +912,10 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher * @param query the query document that specifies the criteria used to find a record * @param fields the document that specifies the fields to be returned * @param targetClass the parameterized type of the returned list. - * @param reader the MongoReader to convert from DBObject to an object. * @return the List of converted objects. */ - protected T doFindOne(String collectionName, DBObject query, DBObject fields, Class targetClass, MongoReader reader) { - MongoReader readerToUse = reader; - if (readerToUse == null) { - readerToUse = this.mongoConverter; - } + protected T doFindOne(String collectionName, DBObject query, DBObject fields, Class targetClass) { + MongoReader readerToUse = this.mongoConverter; PersistentEntity entity = mappingContext.getPersistentEntity(targetClass); DBObject mappedQuery = mapper.getMappedObject(query, entity); @@ -992,7 +955,7 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher } /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader + * Map the results of an ad-hoc query on the default MongoDB collection to a List using the template's converter. *

* The query document is specified as a standard DBObject and so is the fields specification. * @@ -1003,14 +966,15 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher * @param reader the MongoReader to convert from DBObject to an object. * @return the List of converted objects. */ - protected List doFind(String collectionName, DBObject query, DBObject fields, Class targetClass, MongoReader reader) { + protected List doFind(String collectionName, DBObject query, DBObject fields, Class targetClass) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("find using query: " + query + " fields: " + fields + " for class: " + targetClass + " in collection: " + collectionName); } + MongoReader readerToUse = this.mongoConverter; PersistentEntity entity = mappingContext.getPersistentEntity(targetClass); return executeEach(new FindCallback(mapper.getMappedObject(query, entity), fields), null, - new ReadDbObjectCallback(reader, targetClass), + new ReadDbObjectCallback(readerToUse, targetClass), collectionName); } @@ -1031,7 +995,7 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher } /** - * Map the results of an ad-hoc query on the default MongoDB collection to an object using the provided MongoReader + * Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter. * The first document that matches the query is returned and also removed from the collection in the database. *

* The query document is specified as a standard DBObject and so is the fields specification. @@ -1042,11 +1006,8 @@ public class MongoTemplate implements MongoOperations, ApplicationEventPublisher * @param reader the MongoReader to convert from DBObject to an object. * @return the List of converted objects. */ - protected T doFindAndRemove(String collectionName, DBObject query, DBObject fields, DBObject sort, Class targetClass, MongoReader reader) { - MongoReader readerToUse = reader; - if (readerToUse == null) { - readerToUse = this.mongoConverter; - } + protected T doFindAndRemove(String collectionName, DBObject query, DBObject fields, DBObject sort, Class targetClass) { + MongoReader readerToUse = this.mongoConverter; if (LOGGER.isDebugEnabled()) { LOGGER.debug("findAndRemove using query: " + query + " fields: " + fields + " sort: " + sort + " for class: " + targetClass + " in collection: " + collectionName); } 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 badb97780..1ca9842da 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 @@ -238,7 +238,7 @@ public abstract class MongoOperationsUnitTests { new Execution() { @Override public void doWith(MongoOperations operations) { - operations.getCollection("collection", Object.class, converter); + operations.getCollection("collection", Object.class); } }.assertDataAccessException(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java index 90554f740..0dcb34455 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java @@ -5,6 +5,8 @@ import java.util.Date; import java.util.List; import com.mongodb.*; + +import org.bson.types.ObjectId; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -13,17 +15,45 @@ import org.springframework.data.document.analytics.MvcEvent; import org.springframework.data.document.analytics.Parameters; import org.springframework.data.document.mongodb.MongoReader; import org.springframework.data.document.mongodb.MongoTemplate; +import org.springframework.data.document.mongodb.convert.MongoConverter; +import org.springframework.data.document.mongodb.mapping.MongoPersistentEntity; import org.springframework.data.document.mongodb.query.BasicQuery; +import org.springframework.data.mapping.model.MappingContext; public class MvcAnalyticsTests { private MongoTemplate mongoTemplate; + private MongoTemplate mongoDummyTemplate; @Before public void setUp() throws Exception { Mongo m = new Mongo(); mongoTemplate = new MongoTemplate(m, "mvc"); + mongoDummyTemplate = new MongoTemplate( + m, + "mvc", + new MongoConverter() { + public void write(Object t, DBObject dbo) { + } + + public S read(Class clazz, DBObject dbo) { + return null; + } + + public T convertObjectId(ObjectId id, Class targetType) { + return null; + } + + public ObjectId convertObjectId(Object id) { + return null; + } + + public MappingContext> getMappingContext() { + return null; + } + + }); } @Test @@ -49,12 +79,7 @@ public class MvcAnalyticsTests { MvcEvent.class); Assert.assertEquals(22, mvcEvents.size()); - mongoTemplate.getCollection("mvc", MvcEvent.class, - new MongoReader() { - public S read(Class clazz, DBObject dbo) { - return null; - } - }); + mongoDummyTemplate.getCollection("mvc", MvcEvent.class); }