From e2146bf997e36e4350e1eddd11df44536ad3b6db Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Wed, 2 Feb 2011 17:54:52 -0500 Subject: [PATCH] renamed query methods to find; changed query methods taking JSON to accept the new Query object instead; added support for fields specification; removed write concern methods for collection and DB; --- .../document/mongodb/MongoOperations.java | 233 ++++++++++++++---- .../data/document/mongodb/MongoTemplate.java | 123 ++++++--- .../mongodb/repository/MongoQuery.java | 4 +- .../repository/SimpleMongoRepository.java | 6 +- .../document/mongodb/MongoTemplateTests.java | 2 +- .../MongoTemplateWriteConcernTests.java | 86 ------- .../mongodb/analytics/MvcAnalyticsTests.java | 3 +- 7 files changed, 274 insertions(+), 183 deletions(-) delete mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateWriteConcernTests.java 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 78fc81a31..b75586fcc 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 @@ -18,6 +18,8 @@ package org.springframework.data.document.mongodb; import java.util.List; import java.util.Set; +import org.springframework.data.document.mongodb.builder.Query; + import com.mongodb.DBCollection; import com.mongodb.DBObject; import com.mongodb.WriteConcern; @@ -44,32 +46,6 @@ public interface MongoOperations { * @return The default collection used by this template */ DBCollection getDefaultCollection(); - - /** - * Set the {@link com.mongodb.WriteConcern} to be used for the Database. - * @param writeConcern - */ - void setDatabaseWriteConcern(WriteConcern writeConcern); - - /** - * Get the {@link com.mongodb.WriteConcern} currently used by the Database. - * @return the WriteConcern - */ - WriteConcern getDatabaseWriteConcern(); - - /** - * Set the {@link com.mongodb.WriteConcern} to be used for the Collection. - * @param collectionName - * @param writeConcern - */ - void setCollectionWriteConcern(String collectionName, WriteConcern writeConcern); - - /** - * Get the {@link com.mongodb.WriteConcern} currently used by the Collection. - * @param collectionName - * @return the WriteConcern - */ - WriteConcern getCollectionWriteConcern(String collectionName); /** * Execute the a MongoDB command expressed as a JSON string. This will call the method @@ -406,7 +382,6 @@ public interface MongoOperations { * 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. @@ -415,15 +390,75 @@ public interface MongoOperations { List getCollection(String collectionName, Class targetClass, MongoReader reader); - List queryUsingJavaScript(String 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 QuerySpec}. + * + * @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. + * @return the List of converted objects + */ + List query(Query query, Class targetClass); - List queryUsingJavaScript(String 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 QuerySpec}. + * + * @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 query(Query query, Class targetClass, MongoReader reader); - List queryUsingJavaScript(String collectionName, String query, + /** + * 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 QuerySpec}. + * + * @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. + * @return the List of converted objects + */ + List query(String collectionName, Query query, Class targetClass); - List queryUsingJavaScript(String collectionName, String query, + /** + * 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 QuerySpec}. + * + * @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 query(String collectionName, Query query, Class targetClass, MongoReader reader); /** @@ -433,8 +468,108 @@ public interface MongoOperations { * {@see MongoConverter}. Unless configured otherwise, an * instance of SimpleMongoConverter will be used. * - * The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to - * more easily create a query document. + * The query document is specified as a standard DBObject. + * + * NOTE: A generic criteria API will be introduced in a future release. You can see the + * inconsequential project for an example of how that + * may look. + * + * @param query the query document that specifies the criteria used to find a record + * @param targetClass the parameterized type of the returned list. + * @return the List of converted objects + */ + List find(DBObject 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 document is specified as a standard DBObject. + * + * @param query the query document that specifies the criteria used to find a record + * @param targetClass the parameterized type of the returned list. + * @param preparer allows for customization of the DBCursor used when iterating over the result set, + * (apply limits, skips and so on). + * @return the List of converted objects. + */ + List find(DBObject query, Class targetClass, + CursorPreparer preparer); + + /** + * Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader + * + * The query document is specified as a standard DBObject. + * + * @param query the query document that specifies the criteria used to find a record + * @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(DBObject 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. + * + * 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 document is specified as a standard DBObject. + * + * NOTE: A generic criteria API will be introduced in a future release. You can see the + * inconsequential project for an example of how that + * may look. + * + * @param collectionName name of the collection to retrieve the objects from + * @param query the query document that specifies the criteria used to find a record + * @param targetClass the parameterized type of the returned list. + * @return the List of converted objects + */ + List find(String collectionName, DBObject 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 document is specified as a standard DBObject. + * + * @param collectionName name of the collection to retrieve the objects from + * @param query the query document that specifies the criteria used to find a record + * @param targetClass the parameterized type of the returned list. + * @param preparer allows for customization of the DBCursor used when iterating over the result set, + * (apply limits, skips and so on). + * @return the List of converted objects. + */ + List find(String collectionName, DBObject query, Class targetClass, CursorPreparer preparer); + + /** + * Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader + * + * The query document is specified as a standard DBObject. + * + * @param collectionName name of the collection to retrieve the objects from + * @param query the query document that specifies the criteria used to find a record + * @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, DBObject 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. + * + * 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 document is specified as a standard DBObject and so is the fields specification. * * NOTE: A generic criteria API will be introduced in a future release. You can see the * inconsequential project for an example of how that @@ -444,7 +579,7 @@ public interface MongoOperations { * @param targetClass the parameterized type of the returned list. * @return the List of converted objects */ - List query(DBObject query, Class targetClass); + List find(DBObject query, DBObject fields, Class targetClass); /** * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. @@ -453,30 +588,30 @@ public interface MongoOperations { * {@see MongoConverter}. Unless configured otherwise, an * instance of SimpleMongoConverter will be used. * - * The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to - * more easily create a query document. + * The query document is specified as a standard DBObject and so is the fields specification. * * @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 preparer allows for customization of the DBCursor used when iterating over the result set, * (apply limits, skips and so on). * @return the List of converted objects. */ - List query(DBObject query, Class targetClass, + List find(DBObject query, DBObject fields, Class targetClass, CursorPreparer preparer); /** * Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader * - * The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to - * more easily create a query document. + * The query document is specified as a standard DBObject and so is the fields specification. * * @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. */ - List query(DBObject query, Class targetClass, + List find(DBObject query, DBObject fields, Class targetClass, MongoReader reader); /** @@ -486,8 +621,7 @@ public interface MongoOperations { * {@see MongoConverter}. Unless configured otherwise, an * instance of SimpleMongoConverter will be used. * - * The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to - * more easily create a query document. + * The query document is specified as a standard DBObject and so is the fields specification. * * NOTE: A generic criteria API will be introduced in a future release. You can see the * inconsequential project for an example of how that @@ -495,10 +629,11 @@ public interface MongoOperations { * * @param collectionName name of the collection to retrieve the objects from * @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. * @return the List of converted objects */ - List query(String collectionName, DBObject query, Class targetClass); + List find(String collectionName, DBObject fields, DBObject query, Class targetClass); /** * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. @@ -507,29 +642,29 @@ public interface MongoOperations { * {@see MongoConverter}. Unless configured otherwise, an * instance of SimpleMongoConverter will be used. * - * The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to - * more easily create a query document. + * The query document is specified as a standard DBObject and so is the fields specification. * * @param collectionName name of the collection to retrieve the objects from * @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 preparer allows for customization of the DBCursor used when iterating over the result set, * (apply limits, skips and so on). * @return the List of converted objects. */ - List query(String collectionName, DBObject query, Class targetClass, CursorPreparer preparer); + List find(String collectionName, DBObject fields, DBObject query, Class targetClass, CursorPreparer preparer); /** * Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader * - * The query document is specified as a standard DBObject. You can use the driver's QueryBuilder to - * more easily create a query document. + * The query document is specified as a standard DBObject and so is the fields specification. * * @param collectionName name of the collection to retrieve the objects from * @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. */ - List query(String collectionName, DBObject query, Class targetClass, MongoReader reader); + List find(String collectionName, DBObject fields, DBObject query, Class targetClass, MongoReader reader); } \ No newline at end of file diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java index e0c2c1b82..6b38ca93f 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 @@ -28,6 +28,7 @@ import org.springframework.beans.factory.InitializingBean; import org.springframework.dao.DataAccessException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor; +import org.springframework.data.document.mongodb.builder.Query; import org.springframework.jca.cci.core.ConnectionCallback; import org.springframework.util.Assert; @@ -211,23 +212,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations { }); } - // TODO: - public void setDatabaseWriteConcern(WriteConcern writeConcern) { - getDb().setWriteConcern(writeConcern); - } - - public WriteConcern getDatabaseWriteConcern() { - return getDb().getWriteConcern(); - } - - public void setCollectionWriteConcern(String collectionName, WriteConcern writeConcern) { - getCollection(collectionName).setWriteConcern(writeConcern); - } - - public WriteConcern getCollectionWriteConcern(String collectionName) { - return getCollection(collectionName).getWriteConcern(); - } - /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#executeCommand(java.lang.String) */ @@ -683,75 +667,119 @@ public class MongoTemplate implements InitializingBean, MongoOperations { /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.Class) */ - public List queryUsingJavaScript(String query, Class targetClass) { - return query(getDefaultCollectionName(), (DBObject)JSON.parse(query), targetClass); // + public List query(Query query, Class targetClass) { + return query(getDefaultCollectionName(), query, targetClass); // } /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) */ - public List queryUsingJavaScript(String query, Class targetClass, MongoReader reader) { - return query(getDefaultCollectionName(), (DBObject)JSON.parse(query), targetClass, reader); + public List query(Query query, Class targetClass, MongoReader reader) { + return query(getDefaultCollectionName(), query, targetClass, reader); } /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.String, java.lang.Class) */ - public List queryUsingJavaScript(String collectionName, String query, Class targetClass) { - return query(collectionName, (DBObject)JSON.parse(query), targetClass); // + public List query(String collectionName, Query query, Class targetClass) { + return find(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass); // } /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.String, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) */ - public List queryUsingJavaScript(String collectionName, String query, Class targetClass, MongoReader reader) { - return query(collectionName, (DBObject)JSON.parse(query), targetClass, reader); + public List query(String collectionName, Query query, Class targetClass, MongoReader reader) { + return find(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, reader); } - // Queries that take DBObject to express the query + // Find methods that take DBObject to express the query /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class) */ - public List query(DBObject query, Class targetClass) { - return query(getDefaultCollectionName(), query, targetClass); // + public List find(DBObject query, Class targetClass) { + return find(query, null, targetClass); // } /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer) */ - public List query(DBObject query, Class targetClass, CursorPreparer preparer) { - return query(getDefaultCollectionName(), query, targetClass, preparer); // + public List find(DBObject query, Class targetClass, CursorPreparer preparer) { + return find(query, null, targetClass, preparer); // } /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) */ - public List query(DBObject query, Class targetClass, MongoReader reader) { - return query(getDefaultCollectionName(), query, targetClass, reader); + public List find(DBObject query, Class targetClass, MongoReader reader) { + return find(query, null, targetClass, reader); } /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class) */ - public List query(String collectionName, DBObject query, Class targetClass) { - return query(collectionName, query, targetClass, (CursorPreparer) null); + public List find(String collectionName, DBObject query, Class targetClass) { + return find(collectionName, query, null, targetClass); } /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer) */ - public List query(String collectionName, DBObject query, Class targetClass, CursorPreparer preparer) { - return executeEach(new FindCallback(query), preparer, new ReadDbObjectCallback(mongoConverter, targetClass), + public List find(String collectionName, DBObject query, Class targetClass, CursorPreparer preparer) { + return find(collectionName, query, null, targetClass, preparer); + } + + /* (non-Javadoc) + * @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) + */ + public List find(String collectionName, DBObject query, Class targetClass, MongoReader reader) { + return find(collectionName, query, null, targetClass, reader); + } + + // Find methods that take DBObject to express the query and a DBObject to express the fields specification + + /* (non-Javadoc) + * @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class) + */ + public List find(DBObject query, DBObject fields, Class targetClass) { + return find(getDefaultCollectionName(), query, fields, targetClass); // + } + + /* (non-Javadoc) + * @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer) + */ + public List find(DBObject query, DBObject fields, Class targetClass, CursorPreparer preparer) { + return find(getDefaultCollectionName(), query, fields, targetClass, preparer); // + } + + /* (non-Javadoc) + * @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) + */ + public List find(DBObject query, DBObject fields, Class targetClass, MongoReader reader) { + return find(getDefaultCollectionName(), query, fields, targetClass, reader); + } + + /* (non-Javadoc) + * @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class) + */ + public List find(String collectionName, DBObject query, DBObject fields, Class targetClass) { + return find(collectionName, query, fields, targetClass, (CursorPreparer) null); + } + + /* (non-Javadoc) + * @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer) + */ + public List find(String collectionName, DBObject query, DBObject fields, Class targetClass, CursorPreparer preparer) { + return executeEach(new FindCallback(query, fields), preparer, new ReadDbObjectCallback(mongoConverter, targetClass), collectionName); } /* (non-Javadoc) * @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) */ - public List query(String collectionName, DBObject query, Class targetClass, MongoReader reader) { - return executeEach(new FindCallback(query), null, new ReadDbObjectCallback(reader, targetClass), + public List find(String collectionName, DBObject query, DBObject fields, Class targetClass, MongoReader reader) { + return executeEach(new FindCallback(query, fields), null, new ReadDbObjectCallback(reader, targetClass), collectionName); } @@ -829,21 +857,34 @@ public class MongoTemplate implements InitializingBean, MongoOperations { /** - * Simple {@link CollectionCallback} that takes a query {@link DBObject} and executes that against the - * {@link DBCollection}. + * Simple {@link CollectionCallback} that takes a query {@link DBObject} plus an optional fields specification + * {@link DBObject} and executes that against the {@link DBCollection}. * * @author Oliver Gierke + * @author Thomas Risberg */ private static class FindCallback implements CollectionCallback { private final DBObject query; + + private final DBObject fields; public FindCallback(DBObject query) { + this(query, null); + } + + public FindCallback(DBObject query, DBObject fields) { this.query = query; + this.fields = fields; } public DBCursor doInCollection(DBCollection collection) throws MongoException, DataAccessException { - return collection.find(query); + if (fields == null) { + return collection.find(query); + } + else { + return collection.find(query, fields); + } } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java index 80013f2b1..12ab095d6 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java @@ -95,7 +95,7 @@ public class MongoQuery implements RepositoryQuery { protected List readCollection(DBObject query) { - return template.query(template.getDefaultCollectionName(), + return template.find(template.getDefaultCollectionName(), query, method.getDomainClass()); } } @@ -159,7 +159,7 @@ public class MongoQuery implements RepositoryQuery { int count = getCollectionCursor(creator.createQuery()).count(); List result = - template.query(query, method.getDomainClass(), + template.find(query, method.getDomainClass(), withPagination(pageable)); return new PageImpl(result, pageable, count); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java index c69af05cd..cbf73598b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java @@ -108,7 +108,7 @@ public class SimpleMongoRepository extends MongoConverter converter = template.getConverter(); - List result = template.query(QueryBuilder.start("_id").is(converter.convertObjectId(id)).get(), + List result = template.find(QueryBuilder.start("_id").is(converter.convertObjectId(id)).get(), getDomainClass()); return result.isEmpty() ? null : result.get(0); } @@ -202,7 +202,7 @@ public class SimpleMongoRepository extends Long count = count(); List list = - template.query(new BasicDBObject(), getDomainClass(), + template.find(new BasicDBObject(), getDomainClass(), withPagination(pageable)); return new PageImpl(list, pageable, count); @@ -218,7 +218,7 @@ public class SimpleMongoRepository extends */ public List findAll(final Sort sort) { - return template.query(new BasicDBObject(), getDomainClass(), + return template.find(new BasicDBObject(), getDomainClass(), withSorting(sort)); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java index 426e81094..21bbfa3fa 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java @@ -54,7 +54,7 @@ public class MongoTemplateTests { MongoConverter converter = template.getConverter(); - List result = template.query(QueryBuilder.start("_id").is(converter.convertObjectId(person.getId())).get(), Person.class); + List result = template.find(QueryBuilder.start("_id").is(converter.convertObjectId(person.getId())).get(), Person.class); assertThat(result.size(), is(1)); assertThat(result, hasItem(person)); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateWriteConcernTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateWriteConcernTests.java deleted file mode 100644 index 540666df0..000000000 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateWriteConcernTests.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.document.mongodb; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.mongodb.WriteConcern; - -/** - * Integration test of the WriteConcern features for {@link MongoTemplate}. - * - * @author Thomas Risberg - */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("classpath:infrastructure.xml") -public class MongoTemplateWriteConcernTests { - - @Autowired - MongoTemplate template; - - @Before - public void setUp() { - template.dropCollection(template.getDefaultCollectionName()); - } - - @After - public void tearDown() { - template.setCollectionWriteConcern(template.getDefaultCollectionName(), WriteConcern.NORMAL); - template.setDatabaseWriteConcern(WriteConcern.NORMAL); - } - - @Test - public void testRetrievingTheDatabaseWriteConcern() throws Exception { - WriteConcern wc = template.getDatabaseWriteConcern(); - assertNotNull(wc); - } - - @Test - public void testRetrievingTheCollectionWriteConcern() throws Exception { - WriteConcern wc = template.getCollectionWriteConcern(template.getDefaultCollectionName()); - assertNotNull(wc); - } - - @Test - public void testSettingTheDatabaseWriteConcern() throws Exception { - WriteConcern wc = template.getDatabaseWriteConcern(); - WriteConcern safe = WriteConcern.SAFE; - template.setDatabaseWriteConcern(safe); - assertEquals(safe.getW(), template.getDatabaseWriteConcern().getW()); - assertNotSame(wc.getW(), template.getDatabaseWriteConcern().getW()); - } - - @Test - public void testSettingTheCollectionWriteConcern() throws Exception { - String coll = template.getDefaultCollectionName(); - WriteConcern replicasSafe = WriteConcern.REPLICAS_SAFE; - WriteConcern fsyncSafe = WriteConcern.FSYNC_SAFE; - template.setDatabaseWriteConcern(fsyncSafe); - template.setCollectionWriteConcern(coll, replicasSafe); - assertEquals(replicasSafe.getW(), template.getCollectionWriteConcern(coll).getW()); - assertEquals(replicasSafe.fsync(), template.getCollectionWriteConcern(coll).fsync()); - } -} 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 774fe8587..2b938e12a 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 @@ -12,6 +12,7 @@ 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.builder.BasicQuery; import com.mongodb.BasicDBList; import com.mongodb.BasicDBObject; @@ -87,7 +88,7 @@ public class MvcAnalyticsTests { for (DBObject dbo : mongoTemplate.getCollection("counters").find(query)) { System.out.println(dbo); } - List counters = mongoTemplate.queryUsingJavaScript("counters", "{ 'name' : 'SignUpController'} ", ControllerCounter.class); + List counters = mongoTemplate.query("counters", new BasicQuery("{ 'name' : 'SignUpController'} "), ControllerCounter.class); for (ControllerCounter controllerCounter : counters) { System.out.println(controllerCounter); }