DATADOC-18 - Let Repository subsystem use MongoOperations instead of MongoTemplate.

This commit is contained in:
Oliver Gierke
2010-12-16 13:05:50 +01:00
parent 95e5f703b5
commit 95ac7e638c
6 changed files with 81 additions and 72 deletions

View File

@@ -20,7 +20,7 @@ import static org.springframework.data.document.mongodb.repository.MongoCursorUt
import java.util.List;
import org.springframework.data.document.mongodb.CollectionCallback;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.QueryMethod;
@@ -42,24 +42,24 @@ import com.mongodb.DBObject;
public class MongoQuery implements RepositoryQuery {
private final QueryMethod method;
private final MongoTemplate template;
private final MongoOperations operations;
private final PartTree tree;
/**
* Creates a new {@link MongoQuery} from the given {@link QueryMethod} and
* {@link MongoTemplate}.
* {@link MongoOperations}.
*
* @param method
* @param template
* @param operations
*/
public MongoQuery(QueryMethod method, MongoTemplate template) {
public MongoQuery(QueryMethod method, MongoOperations operations) {
Assert.notNull(template);
Assert.notNull(operations);
Assert.notNull(method);
this.method = method;
this.template = template;
this.operations = operations;
this.tree = new PartTree(method.getName(), method.getDomainClass());
}
@@ -95,7 +95,7 @@ public class MongoQuery implements RepositoryQuery {
protected List<?> readCollection(DBObject query) {
return template.query(template.getDefaultCollectionName(), query,
return operations.query(operations.getDefaultCollectionName(), query,
method.getDomainClass());
}
}
@@ -147,7 +147,7 @@ public class MongoQuery implements RepositoryQuery {
int count = getCollectionCursor(creator.createQuery()).count();
List<?> result =
template.query(query, method.getDomainClass(),
operations.query(query, method.getDomainClass(),
withPagination(pageable));
return new PageImpl(result, pageable, count);
@@ -156,7 +156,7 @@ public class MongoQuery implements RepositoryQuery {
private DBCursor getCollectionCursor(final DBObject query) {
return template.execute(new CollectionCallback<DBCursor>() {
return operations.execute(new CollectionCallback<DBCursor>() {
public DBCursor doInCollection(DBCollection collection) {

View File

@@ -19,7 +19,7 @@ import java.io.Serializable;
import java.lang.reflect.Method;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
@@ -39,17 +39,17 @@ import org.springframework.util.Assert;
public class MongoRepositoryFactoryBean extends
RepositoryFactoryBeanSupport<Repository<?, ?>> {
private MongoTemplate template;
private MongoOperations operations;
/**
* Configures the {@link MongoTemplate} to be used.
* Configures the {@link MongoOperations} to be used.
*
* @param template the template to set
* @param operations the template to set
*/
public void setTemplate(MongoTemplate template) {
public void setOperations(MongoOperations operations) {
this.template = template;
this.operations = operations;
}
@@ -78,7 +78,7 @@ public class MongoRepositoryFactoryBean extends
public void afterPropertiesSet() {
super.afterPropertiesSet();
Assert.notNull(template, "MongoTemplate must not be null!");
Assert.notNull(operations, "MongoTemplate must not be null!");
}
/**
@@ -92,7 +92,7 @@ public class MongoRepositoryFactoryBean extends
protected <T, ID extends Serializable> RepositorySupport<T, ID> getTargetRepository(
Class<T> domainClass) {
return new SimpleMongoRepository<T, ID>(domainClass, template);
return new SimpleMongoRepository<T, ID>(domainClass, operations);
}
@@ -119,7 +119,7 @@ public class MongoRepositoryFactoryBean extends
public RepositoryQuery resolveQuery(Method method) {
return new MongoQuery(new QueryMethod(method), template);
return new MongoQuery(new QueryMethod(method), operations);
}
}
}

View File

@@ -21,7 +21,7 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
@@ -43,20 +43,20 @@ import com.mongodb.QueryBuilder;
public class SimpleMongoRepository<T, ID extends Serializable> extends
RepositorySupport<T, ID> implements PagingAndSortingRepository<T, ID> {
private final MongoTemplate template;
private final MongoOperations operations;
private MongoEntityInformation entityInformation;
/**
* @param domainClass
* @param template
* @param operations
*/
public SimpleMongoRepository(Class<T> domainClass, MongoTemplate template) {
public SimpleMongoRepository(Class<T> domainClass, MongoOperations operations) {
super(domainClass);
Assert.notNull(template);
this.template = template;
Assert.notNull(operations);
this.operations = operations;
}
@@ -68,7 +68,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/
public T save(T entity) {
template.save(entity);
operations.save(entity);
return entity;
}
@@ -84,7 +84,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
List<T> result = new ArrayList<T>();
for (T entity : entities) {
template.save(entity);
operations.save(entity);
result.add(entity);
}
@@ -102,7 +102,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
public T findById(ID id) {
List<T> result =
template.query(template.getDefaultCollectionName(),
operations.query(operations.getDefaultCollectionName(),
QueryBuilder.start("_id").get(), getDomainClass());
return result.isEmpty() ? null : result.get(0);
@@ -129,7 +129,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/
public List<T> findAll() {
return template.getCollection(getDomainClass());
return operations.getCollection(getDomainClass());
}
@@ -140,7 +140,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/
public Long count() {
return template.getCollection(template.getDefaultCollectionName())
return operations.getCollection(operations.getDefaultCollectionName())
.count();
}
@@ -156,7 +156,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
QueryBuilder builder =
QueryBuilder.start(entityInformation.getFieldName()).is(
entityInformation.getId(entity));
template.remove(builder.get());
operations.remove(builder.get());
}
@@ -181,7 +181,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/
public void deleteAll() {
template.dropCollection(template.getDefaultCollectionName());
operations.dropCollection(operations.getDefaultCollectionName());
}
@@ -197,7 +197,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
Long count = count();
List<T> list =
template.query(new BasicDBObject(), getDomainClass(),
operations.query(new BasicDBObject(), getDomainClass(),
withPagination(pageable));
return new PageImpl<T>(list, pageable, count);
@@ -213,7 +213,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/
public List<T> findAll(final Sort sort) {
return template.query(new BasicDBObject(), getDomainClass(),
return operations.query(new BasicDBObject(), getDomainClass(),
withSorting(sort));
}

View File

@@ -46,6 +46,6 @@ public class MongoRepositoryConfigDefinitionParser
MongoRepositoryConfiguration context,
BeanDefinitionBuilder builder, Object beanSource) {
builder.addPropertyReference("template", context.getMongoTemplateRef());
builder.addPropertyReference("operations", context.getMongoTemplateRef());
}
}

View File

@@ -6,7 +6,7 @@
<import resource="infrastructure.xml" />
<bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean">
<property name="template" ref="mongoTemplate" />
<property name="operations" ref="mongoTemplate" />
<property name="repositoryInterface" value="org.springframework.data.document.mongodb.repository.PersonRepository" />
</bean>