From 93d22dbd617f204eb8ecffe48fff7063781fb507 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 10 Feb 2011 20:10:22 +0100 Subject: [PATCH] Let repositories use the domain class' name to select collection. We will now store each managed entity into a separate collection, so User class would be stored in the user collection, Account in account and so on. --- .../mongodb/repository/MongoQuery.java | 14 ++++++---- .../mongodb/repository/QueryUtils.java | 7 +++++ .../repository/SimpleMongoRepository.java | 28 +++++++++++-------- 3 files changed, 32 insertions(+), 17 deletions(-) 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 d6db5988a..3b210442c 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 @@ -98,7 +98,9 @@ public class MongoQuery implements RepositoryQuery { protected List readCollection(Query query) { - return template.find(query, method.getDomainClass()); + String collectionName = getCollectionName(method.getDomainClass()); + return template + .find(collectionName, query, method.getDomainClass()); } } @@ -160,17 +162,19 @@ public class MongoQuery implements RepositoryQuery { Object execute(Query query) { Query countQuery = creator.createQuery(); - int count = getCollectionCursor(countQuery.getQueryObject()).count(); + String collectionName = getCollectionName(method.getDomainClass()); + int count = + getCollectionCursor(collectionName, countQuery.getQueryObject()).count(); List result = - template.find(applyPagination(query, pageable), + template.find(collectionName, applyPagination(query, pageable), method.getDomainClass()); return new PageImpl(result, pageable, count); } - private DBCursor getCollectionCursor(final DBObject query) { + private DBCursor getCollectionCursor(String collectionName, final DBObject query) { return template.execute(new CollectionCallback() { @@ -178,7 +182,7 @@ public class MongoQuery implements RepositoryQuery { return collection.find(query); } - }); + }, collectionName); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/QueryUtils.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/QueryUtils.java index 23cac790b..4c4b2386c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/QueryUtils.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/QueryUtils.java @@ -19,6 +19,7 @@ import org.springframework.data.document.mongodb.query.Query; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Order; +import org.springframework.util.StringUtils; import com.mongodb.DBCursor; @@ -83,4 +84,10 @@ abstract class QueryUtils { return query; } + + + public static String getCollectionName(Class domainClass) { + + return StringUtils.uncapitalize(domainClass.getSimpleName()); + } } 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 d9f2c262f..b387cb767 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 @@ -15,7 +15,8 @@ */ package org.springframework.data.document.mongodb.repository; -import static org.springframework.data.document.mongodb.query.Criteria.where; +import static org.springframework.data.document.mongodb.query.Criteria.*; +import static org.springframework.data.document.mongodb.repository.QueryUtils.*; import java.io.Serializable; import java.util.ArrayList; @@ -34,6 +35,7 @@ import org.springframework.data.repository.support.IsNewAware; import org.springframework.data.repository.support.RepositorySupport; import org.springframework.util.Assert; + /** * Repository base implementation for Mongo. * @@ -70,7 +72,7 @@ public class SimpleMongoRepository extends */ public T save(T entity) { - template.save(entity); + template.save(getCollectionName(getDomainClass()), entity); return entity; } @@ -86,7 +88,7 @@ public class SimpleMongoRepository extends List result = new ArrayList(); for (T entity : entities) { - template.save(entity); + save(entity); result.add(entity); } @@ -108,9 +110,8 @@ public class SimpleMongoRepository extends ObjectId objectId = converter.convertObjectId(id); List result = - template.find( - new Query(where("_id").is(objectId)), - getDomainClass()); + template.find(getCollectionName(getDomainClass()), new Query( + where("_id").is(objectId)), getDomainClass()); return result.isEmpty() ? null : result.get(0); } @@ -135,7 +136,8 @@ public class SimpleMongoRepository extends */ public List findAll() { - return template.getCollection(getDomainClass()); + return template.getCollection(getCollectionName(getDomainClass()), + getDomainClass()); } @@ -146,7 +148,7 @@ public class SimpleMongoRepository extends */ public Long count() { - return template.getCollection(template.getDefaultCollectionName()) + return template.getCollection(getCollectionName(getDomainClass())) .count(); } @@ -162,7 +164,7 @@ public class SimpleMongoRepository extends Query query = new Query(where(entityInformation.getFieldName()).is( entityInformation.getId(entity))); - template.remove(query); + template.remove(getCollectionName(getDomainClass()), query); } @@ -187,7 +189,7 @@ public class SimpleMongoRepository extends */ public void deleteAll() { - template.dropCollection(template.getDefaultCollectionName()); + template.dropCollection(getCollectionName(getDomainClass())); } @@ -204,7 +206,8 @@ public class SimpleMongoRepository extends Query spec = new Query(); List list = - template.find(QueryUtils.applyPagination(spec, pageable), + template.find(getCollectionName(getDomainClass()), + QueryUtils.applyPagination(spec, pageable), getDomainClass()); return new PageImpl(list, pageable, count); @@ -221,7 +224,8 @@ public class SimpleMongoRepository extends public List findAll(final Sort sort) { Query query = QueryUtils.applySorting(new Query(), sort); - return template.find(query, getDomainClass()); + return template.find(getCollectionName(getDomainClass()), query, + getDomainClass()); }