Browse Source

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.
pull/1/head
Oliver Gierke 15 years ago
parent
commit
93d22dbd61
  1. 14
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java
  2. 7
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/QueryUtils.java
  3. 28
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java

14
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java

@ -98,7 +98,9 @@ public class MongoQuery implements RepositoryQuery { @@ -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 { @@ -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 { @@ -178,7 +182,7 @@ public class MongoQuery implements RepositoryQuery {
return collection.find(query);
}
});
}, collectionName);
}
}

7
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; @@ -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 { @@ -83,4 +84,10 @@ abstract class QueryUtils {
return query;
}
public static String getCollectionName(Class<?> domainClass) {
return StringUtils.uncapitalize(domainClass.getSimpleName());
}
}

28
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java

@ -15,7 +15,8 @@ @@ -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; @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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());
}

Loading…
Cancel
Save