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:
@@ -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<DBCursor>() {
|
||||
|
||||
@@ -178,7 +182,7 @@ public class MongoQuery implements RepositoryQuery {
|
||||
|
||||
return collection.find(query);
|
||||
}
|
||||
});
|
||||
}, collectionName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T, ID extends Serializable> extends
|
||||
*/
|
||||
public T save(T entity) {
|
||||
|
||||
template.save(entity);
|
||||
template.save(getCollectionName(getDomainClass()), entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@@ -86,7 +88,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
|
||||
List<T> result = new ArrayList<T>();
|
||||
|
||||
for (T entity : entities) {
|
||||
template.save(entity);
|
||||
save(entity);
|
||||
result.add(entity);
|
||||
}
|
||||
|
||||
@@ -108,9 +110,8 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
|
||||
ObjectId objectId = converter.convertObjectId(id);
|
||||
|
||||
List<T> 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<T, ID extends Serializable> extends
|
||||
*/
|
||||
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() {
|
||||
|
||||
return template.getCollection(template.getDefaultCollectionName())
|
||||
return template.getCollection(getCollectionName(getDomainClass()))
|
||||
.count();
|
||||
}
|
||||
|
||||
@@ -162,7 +164,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> 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<T, ID extends Serializable> extends
|
||||
*/
|
||||
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();
|
||||
|
||||
List<T> list =
|
||||
template.find(QueryUtils.applyPagination(spec, pageable),
|
||||
template.find(getCollectionName(getDomainClass()),
|
||||
QueryUtils.applyPagination(spec, pageable),
|
||||
getDomainClass());
|
||||
|
||||
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) {
|
||||
|
||||
Query query = QueryUtils.applySorting(new Query(), sort);
|
||||
return template.find(query, getDomainClass());
|
||||
return template.find(getCollectionName(getDomainClass()), query,
|
||||
getDomainClass());
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user