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.
This commit is contained in:
Oliver Gierke
2011-02-10 20:10:22 +01:00
parent f192c4289b
commit 93d22dbd61
3 changed files with 32 additions and 17 deletions

View File

@@ -98,7 +98,9 @@ public class MongoQuery implements RepositoryQuery {
protected List<?> readCollection(Query query) { 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) { Object execute(Query query) {
Query countQuery = creator.createQuery(); Query countQuery = creator.createQuery();
int count = getCollectionCursor(countQuery.getQueryObject()).count(); String collectionName = getCollectionName(method.getDomainClass());
int count =
getCollectionCursor(collectionName, countQuery.getQueryObject()).count();
List<?> result = List<?> result =
template.find(applyPagination(query, pageable), template.find(collectionName, applyPagination(query, pageable),
method.getDomainClass()); method.getDomainClass());
return new PageImpl(result, pageable, count); 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<DBCursor>() { return template.execute(new CollectionCallback<DBCursor>() {
@@ -178,7 +182,7 @@ public class MongoQuery implements RepositoryQuery {
return collection.find(query); return collection.find(query);
} }
}); }, collectionName);
} }
} }

View File

@@ -19,6 +19,7 @@ import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order; import org.springframework.data.domain.Sort.Order;
import org.springframework.util.StringUtils;
import com.mongodb.DBCursor; import com.mongodb.DBCursor;
@@ -83,4 +84,10 @@ abstract class QueryUtils {
return query; return query;
} }
public static String getCollectionName(Class<?> domainClass) {
return StringUtils.uncapitalize(domainClass.getSimpleName());
}
} }

View File

@@ -15,7 +15,8 @@
*/ */
package org.springframework.data.document.mongodb.repository; 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.io.Serializable;
import java.util.ArrayList; 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.data.repository.support.RepositorySupport;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* Repository base implementation for Mongo. * Repository base implementation for Mongo.
* *
@@ -70,7 +72,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/ */
public T save(T entity) { public T save(T entity) {
template.save(entity); template.save(getCollectionName(getDomainClass()), entity);
return entity; return entity;
} }
@@ -86,7 +88,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
List<T> result = new ArrayList<T>(); List<T> result = new ArrayList<T>();
for (T entity : entities) { for (T entity : entities) {
template.save(entity); save(entity);
result.add(entity); result.add(entity);
} }
@@ -108,9 +110,8 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
ObjectId objectId = converter.convertObjectId(id); ObjectId objectId = converter.convertObjectId(id);
List<T> result = List<T> result =
template.find( template.find(getCollectionName(getDomainClass()), new Query(
new Query(where("_id").is(objectId)), where("_id").is(objectId)), getDomainClass());
getDomainClass());
return result.isEmpty() ? null : result.get(0); return result.isEmpty() ? null : result.get(0);
} }
@@ -135,7 +136,8 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/ */
public List<T> findAll() { public List<T> findAll() {
return template.getCollection(getDomainClass()); return template.getCollection(getCollectionName(getDomainClass()),
getDomainClass());
} }
@@ -146,7 +148,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/ */
public Long count() { public Long count() {
return template.getCollection(template.getDefaultCollectionName()) return template.getCollection(getCollectionName(getDomainClass()))
.count(); .count();
} }
@@ -162,7 +164,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
Query query = Query query =
new Query(where(entityInformation.getFieldName()).is( new Query(where(entityInformation.getFieldName()).is(
entityInformation.getId(entity))); entityInformation.getId(entity)));
template.remove(query); template.remove(getCollectionName(getDomainClass()), query);
} }
@@ -187,7 +189,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/ */
public void deleteAll() { public void deleteAll() {
template.dropCollection(template.getDefaultCollectionName()); template.dropCollection(getCollectionName(getDomainClass()));
} }
@@ -204,7 +206,8 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
Query spec = new Query(); Query spec = new Query();
List<T> list = List<T> list =
template.find(QueryUtils.applyPagination(spec, pageable), template.find(getCollectionName(getDomainClass()),
QueryUtils.applyPagination(spec, pageable),
getDomainClass()); getDomainClass());
return new PageImpl<T>(list, pageable, count); return new PageImpl<T>(list, pageable, count);
@@ -221,7 +224,8 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
public List<T> findAll(final Sort sort) { public List<T> findAll(final Sort sort) {
Query query = QueryUtils.applySorting(new Query(), sort); Query query = QueryUtils.applySorting(new Query(), sort);
return template.find(query, getDomainClass()); return template.find(getCollectionName(getDomainClass()), query,
getDomainClass());
} }