diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/AbstractMongoQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/AbstractMongoQuery.java index f312d3049..617f538ba 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/AbstractMongoQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/AbstractMongoQuery.java @@ -26,7 +26,6 @@ import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.data.repository.query.ParameterAccessor; import org.springframework.data.repository.query.ParametersParameterAccessor; -import org.springframework.data.repository.query.QueryMethod; import org.springframework.data.repository.query.RepositoryQuery; import org.springframework.util.Assert; @@ -34,7 +33,6 @@ import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; - /** * Base class for {@link RepositoryQuery} implementations for Mongo. * @@ -42,167 +40,159 @@ import com.mongodb.DBObject; */ public abstract class AbstractMongoQuery implements RepositoryQuery { - private final MongoQueryMethod method; - private final MongoTemplate template; + private final MongoQueryMethod method; + private final MongoTemplate template; + /** + * Creates a new {@link AbstractMongoQuery} from the given {@link MongoQueryMethod} and {@link MongoTemplate}. + * + * @param method + * @param template + */ + public AbstractMongoQuery(MongoQueryMethod method, MongoTemplate template) { - /** - * Creates a new {@link AbstractMongoQuery} from the given {@link QueryMethod} and - * {@link MongoTemplate}. - * - * @param method - * @param template - */ - public AbstractMongoQuery(MongoQueryMethod method, MongoTemplate template) { + Assert.notNull(template); + Assert.notNull(method); - Assert.notNull(template); - Assert.notNull(method); + this.method = method; + this.template = template; + } + + /* (non-Javadoc) + * @see org.springframework.data.repository.query.RepositoryQuery#getQueryMethod() + */ + public MongoQueryMethod getQueryMethod() { + + return method; + } - this.method = method; - this.template = template; - } + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.query.RepositoryQuery#execute(java .lang.Object[]) + */ + public Object execute(Object[] parameters) { + ParameterAccessor accessor = new ParametersParameterAccessor(method.getParameters(), parameters); + Query query = createQuery(new ConvertingParameterAccessor(template.getConverter(), accessor)); - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.query.RepositoryQuery#execute(java - * .lang.Object[]) - */ - public Object execute(Object[] parameters) { + switch (method.getType()) { + case COLLECTION: + return new CollectionExecution().execute(query); + case PAGING: + return new PagedExecution(accessor.getPageable()).execute(query); + default: + return new SingleEntityExecution().execute(query); + } + } - ParameterAccessor accessor = - new ParametersParameterAccessor(method.getParameters(), parameters); - Query query = createQuery(new ConvertingParameterAccessor(template.getConverter(), accessor)); + /** + * Create a {@link Query} instance using the given {@link ParameterAccessor} + * @param accessor + * @param converter + * @return + */ + protected abstract Query createQuery(ConvertingParameterAccessor accessor); - if (method.isCollectionQuery()) { - return new CollectionExecution().execute(query); - } else if (method.isPageQuery()) { - return new PagedExecution(accessor.getPageable()).execute(query); - } else { - return new SingleEntityExecution().execute(query); - } - } - - /** - * Create a {@link Query} instance using the given {@link ParameterAccessor} - * @param accessor - * @param converter - * @return - */ - protected abstract Query createQuery(ConvertingParameterAccessor accessor); - + private abstract class Execution { - private abstract class Execution { + abstract Object execute(Query query); - abstract Object execute(Query query); + protected List readCollection(Query query) { + MongoEntityInformation metadata = method.getEntityMetadata(); - protected List readCollection(Query query) { + String collectionName = metadata.getCollectionName(); + return template.find(collectionName, query, metadata.getJavaType()); + } + } - String collectionName = getCollectionName(method.getDomainClass()); - return template - .find(collectionName, query, method.getReturnedDomainClass()); - } - } + /** + * {@link Execution} for collection returning queries. + * + * @author Oliver Gierke + */ + class CollectionExecution extends Execution { - /** - * {@link Execution} for collection returning queries. - * - * @author Oliver Gierke - */ - class CollectionExecution extends Execution { + /* + * (non-Javadoc) + * + * @see org.springframework.data.document.mongodb.repository.MongoQuery.Execution #execute(com.mongodb.DBObject) + */ + @Override + public Object execute(Query query) { - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.document.mongodb.repository.MongoQuery.Execution - * #execute(com.mongodb.DBObject) - */ - @Override - public Object execute(Query query) { + return readCollection(query); + } + } - return readCollection(query); - } - } + /** + * {@link Execution} for pagination queries. + * + * @author Oliver Gierke + */ + class PagedExecution extends Execution { - /** - * {@link Execution} for pagination queries. - * - * @author Oliver Gierke - */ - class PagedExecution extends Execution { + private final Pageable pageable; - private final Pageable pageable; + /** + * Creates a new {@link PagedExecution}. + * + * @param pageable + */ + public PagedExecution(Pageable pageable) { + Assert.notNull(pageable); + this.pageable = pageable; + } - /** - * Creates a new {@link PagedExecution}. - * - * @param pageable - */ - public PagedExecution(Pageable pageable) { + /* + * (non-Javadoc) + * + * @see org.springframework.data.document.mongodb.repository.MongoQuery.Execution #execute(com.mongodb.DBObject) + */ + @Override + @SuppressWarnings({ "rawtypes", "unchecked" }) + Object execute(Query query) { - Assert.notNull(pageable); - this.pageable = pageable; - } + MongoEntityInformation metadata = method.getEntityMetadata(); + int count = getCollectionCursor(metadata.getCollectionName(), query.getQueryObject()).count(); + List result = template.find(metadata.getCollectionName(), applyPagination(query, pageable), + metadata.getJavaType()); - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.document.mongodb.repository.MongoQuery.Execution - * #execute(com.mongodb.DBObject) - */ - @Override - @SuppressWarnings({ "rawtypes", "unchecked" }) - Object execute(Query query) { + return new PageImpl(result, pageable, count); + } - String collectionName = getCollectionName(method.getDomainClass()); - int count = getCollectionCursor(collectionName, query.getQueryObject()).count(); + private DBCursor getCollectionCursor(String collectionName, final DBObject query) { - List result = - template.find(collectionName, applyPagination(query, pageable), - method.getReturnedDomainClass()); + return template.execute(collectionName, new CollectionCallback() { - return new PageImpl(result, pageable, count); - } + public DBCursor doInCollection(DBCollection collection) { + return collection.find(query); + } + }); + } + } - private DBCursor getCollectionCursor(String collectionName, final DBObject query) { + /** + * {@link Execution} to return a single entity. + * + * @author Oliver Gierke + */ + class SingleEntityExecution extends Execution { - return template.execute(collectionName, new CollectionCallback() { + /* + * (non-Javadoc) + * + * @see org.springframework.data.document.mongodb.repository.MongoQuery.Execution #execute(com.mongodb.DBObject) + */ + @Override + Object execute(Query query) { - public DBCursor doInCollection(DBCollection collection) { - - return collection.find(query); - } - }); - } - } - - /** - * {@link Execution} to return a single entity. - * - * @author Oliver Gierke - */ - class SingleEntityExecution extends Execution { - - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.document.mongodb.repository.MongoQuery.Execution - * #execute(com.mongodb.DBObject) - */ - @Override - Object execute(Query query) { - - List result = readCollection(query); - return result.isEmpty() ? null : result.get(0); - } - } + List result = readCollection(query); + return result.isEmpty() ? null : result.get(0); + } + } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoEntityMetadata.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoEntityInformation.java similarity index 81% rename from spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoEntityMetadata.java rename to spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoEntityInformation.java index 439f6c2e2..1ace73e3e 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoEntityMetadata.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoEntityInformation.java @@ -19,8 +19,9 @@ import java.lang.reflect.Field; import java.util.Arrays; import java.util.List; -import org.springframework.data.repository.support.AbstractEntityMetadata; +import org.springframework.data.repository.support.AbstractEntityInformation; import org.springframework.util.ReflectionUtils; +import org.springframework.util.StringUtils; /** @@ -29,18 +30,18 @@ import org.springframework.util.ReflectionUtils; * * @author Oliver Gierke */ -class MongoEntityMetadata extends AbstractEntityMetadata { +class MongoEntityInformation extends AbstractEntityInformation { private static final List FIELD_NAMES = Arrays.asList("ID", "id", "_id"); private Field field; /** - * Creates a new {@link MongoEntityMetadata}. + * Creates a new {@link MongoEntityInformation}. * * @param domainClass */ - public MongoEntityMetadata(Class domainClass) { + public MongoEntityInformation(Class domainClass) { super(domainClass); @@ -61,16 +62,16 @@ class MongoEntityMetadata extends AbstractEntityMetadata { domainClass.getName())); } } - - - /** - * Returns the actual field name containing the id. - * - * @return - */ - public String getFieldName() { - - return field.getName(); + + + public String getCollectionName() { + + return StringUtils.uncapitalize(getJavaType().getSimpleName()); + } + + public String getIdAttribute() { + + return "_id"; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryMethod.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryMethod.java index ec6777e0a..9d6da84b6 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryMethod.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryMethod.java @@ -31,7 +31,7 @@ import org.springframework.util.StringUtils; class MongoQueryMethod extends QueryMethod { private final Method method; - private final Class domainClass; + private final MongoEntityInformation entityInformation; /** * Creates a new {@link MongoQueryMethod} from the given {@link Method}. @@ -41,27 +41,9 @@ class MongoQueryMethod extends QueryMethod { public MongoQueryMethod(Method method, Class domainClass) { super(method); this.method = method; - this.domainClass = domainClass; + this.entityInformation = new MongoEntityInformation(ClassUtils.getReturnedDomainClass(method)); } - - /* (non-Javadoc) - * @see org.springframework.data.repository.query.QueryMethod#getDomainClass() - */ - @Override - public Class getDomainClass() { - return this.domainClass; - } - - - /** - * Returns the type that will be returned by the query method. - * - * @return - */ - public Class getReturnedDomainClass() { - return ClassUtils.getReturnedDomainClass(method); - } - + /** * Returns whether the method has an annotated query. @@ -95,6 +77,15 @@ class MongoQueryMethod extends QueryMethod { return StringUtils.hasText(value) ? value : null; } + + /* (non-Javadoc) + * @see org.springframework.data.repository.query.QueryMethod#getEntityMetadata() + */ + @Override + public MongoEntityInformation getEntityMetadata() { + + return entityInformation; + } /** * Returns the {@link Query} annotation that is applied to the method or {@code null} if none available. diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepositoryFactoryBean.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepositoryFactoryBean.java index b03370f32..ca4be6d09 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepositoryFactoryBean.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepositoryFactoryBean.java @@ -18,12 +18,20 @@ package org.springframework.data.document.mongodb.repository; import java.io.Serializable; import java.lang.reflect.Method; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.document.mongodb.MongoOperations; import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor; import org.springframework.data.document.mongodb.MongoTemplate; +import org.springframework.data.document.mongodb.query.Index; +import org.springframework.data.document.mongodb.query.Order; +import org.springframework.data.domain.Sort; import org.springframework.data.repository.query.QueryLookupStrategy; import org.springframework.data.repository.query.QueryLookupStrategy.Key; import org.springframework.data.repository.query.RepositoryQuery; -import org.springframework.data.repository.support.EntityMetadata; +import org.springframework.data.repository.query.parser.Part; +import org.springframework.data.repository.query.parser.PartTree; +import org.springframework.data.repository.support.QueryCreationListener; import org.springframework.data.repository.support.RepositoryFactoryBeanSupport; import org.springframework.data.repository.support.RepositoryFactorySupport; import org.springframework.data.repository.support.RepositoryMetadata; @@ -57,7 +65,9 @@ public class MongoRepositoryFactoryBean extends RepositoryFactoryBeanSupport info = new MongoEntityMetadata((Class) metadata.getDomainClass()); + MongoEntityInformation info = new MongoEntityInformation( + (Class) metadata.getDomainClass()); return new SimpleMongoRepository(info, template); } @@ -152,4 +163,55 @@ public class MongoRepositoryFactoryBean extends RepositoryFactoryBeanSupport { + + private static final Logger LOG = LoggerFactory.getLogger(IndexEnsuringQueryCreationListener.class); + private final MongoOperations operations; + + public IndexEnsuringQueryCreationListener(MongoOperations operations) { + this.operations = operations; + } + + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.support.QueryCreationListener#onCreation(org.springframework.data.repository + * .query.RepositoryQuery) + */ + public void onCreation(PartTreeMongoQuery query) { + + PartTree tree = query.getTree(); + Index index = new Index(); + index.named(query.getQueryMethod().getName()); + Sort sort = tree.getSort(); + + for (Part part : tree.getParts()) { + String property = part.getProperty().toDotPath(); + Order order = toOrder(sort, property); + index.on(property, order); + } + + MongoEntityInformation metadata = query.getQueryMethod().getEntityMetadata(); + operations.ensureIndex(metadata.getCollectionName(), index); + LOG.debug(String.format("Created index %s!", index.toString())); + } + + private static Order toOrder(Sort sort, String property) { + + if (sort == null) { + return Order.DESCENDING; + } + + org.springframework.data.domain.Sort.Order order = sort.getOrderFor(property); + return order == null ? Order.DESCENDING : order.isAscending() ? Order.ASCENDING : Order.DESCENDING; + } + } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/PartTreeMongoQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/PartTreeMongoQuery.java index a9914874a..1ea20789e 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/PartTreeMongoQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/PartTreeMongoQuery.java @@ -39,7 +39,14 @@ public class PartTreeMongoQuery extends AbstractMongoQuery { public PartTreeMongoQuery(MongoQueryMethod method, MongoTemplate template) { super(method, template); - this.tree = new PartTree(method.getName(), method.getDomainClass()); + this.tree = new PartTree(method.getName(), method.getEntityMetadata().getJavaType()); + } + + /** + * @return the tree + */ + public PartTree getTree() { + return tree; } /* 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 3ca3eae66..39e6ba567 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,7 +19,6 @@ 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; @@ -84,10 +83,4 @@ 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 56d107360..1d7b07544 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 @@ -16,25 +16,23 @@ package org.springframework.data.document.mongodb.repository; 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; +import java.util.Collections; import java.util.List; import org.bson.types.ObjectId; -import org.springframework.data.document.mongodb.MongoConverter; import org.springframework.data.document.mongodb.MongoTemplate; +import org.springframework.data.document.mongodb.query.Criteria; import org.springframework.data.document.mongodb.query.Query; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.repository.PagingAndSortingRepository; -import org.springframework.data.repository.support.EntityMetadata; import org.springframework.util.Assert; - /** * Repository base implementation for Mongo. * @@ -43,190 +41,181 @@ import org.springframework.util.Assert; public class SimpleMongoRepository implements PagingAndSortingRepository { private final MongoTemplate template; - private final EntityMetadata entityInformation; + private final MongoEntityInformation entityInformation; + + /** + * Creates a ew {@link SimpleMongoRepository} for the given {@link MongoInformation} and {@link MongoTemplate}. + * + * @param metadata + * @param template + */ + public SimpleMongoRepository(MongoEntityInformation metadata, MongoTemplate template) { + + Assert.notNull(template); + Assert.notNull(metadata); + this.entityInformation = metadata; + this.template = template; + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#save(java.lang.Object) + */ + public T save(T entity) { + + template.save(entityInformation.getCollectionName(), entity); + return entity; + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#save(java.lang.Iterable) + */ + public List save(Iterable entities) { + + List result = new ArrayList(); + + for (T entity : entities) { + save(entity); + result.add(entity); + } + + return result; + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#findById(java.io.Serializable ) + */ + public T findById(ID id) { + + return template.findOne(entityInformation.getCollectionName(), getIdQuery(id), entityInformation.getJavaType()); + } + + private Query getIdQuery(Object id) { + + return new Query(getIdCriteria(id)); + } + + private Criteria getIdCriteria(Object id) { + ObjectId objectId = template.getConverter().convertObjectId(id); + return where(entityInformation.getIdAttribute()).is(objectId); + } - /** - * Creates a ew {@link SimpleMongoRepository} for the given domain class and - * {@link MongoTemplate}. - * - * @param domainClass - * @param template - */ - public SimpleMongoRepository(EntityMetadata entityInformation, MongoTemplate template) { + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#exists(java.io.Serializable ) + */ + public boolean exists(ID id) { - Assert.notNull(entityInformation); - Assert.notNull(template); - this.entityInformation = entityInformation; - this.template = template; - } - - private Class getDomainClass() { - return entityInformation.getJavaType(); - } + return findById(id) != null; + } + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#count() + */ + public Long count() { - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.Repository#save(java.lang.Object) - */ - public T save(T entity) { + return template.getCollection(entityInformation.getCollectionName()).count(); + } - template.save(getCollectionName(getDomainClass()), entity); - return entity; - } + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#delete(java.lang.Object) + */ + public void delete(T entity) { + template.remove(entityInformation.getCollectionName(), getIdQuery(entityInformation.getId(entity))); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.Repository#save(java.lang.Iterable) - */ - public List save(Iterable entities) { + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#delete(java.lang.Iterable) + */ + public void delete(Iterable entities) { - List result = new ArrayList(); + for (T entity : entities) { + delete(entity); + } + } - for (T entity : entities) { - save(entity); - result.add(entity); - } + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#deleteAll() + */ + public void deleteAll() { - return result; - } + template.dropCollection(entityInformation.getCollectionName()); + } + + /* (non-Javadoc) + * @see org.springframework.data.repository.Repository#findAll() + */ + public List findAll() { + return findAll(new Query()); + } + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.PagingAndSortingRepository#findAll + * (org.springframework.data.domain.Pageable) + */ + public Page findAll(final Pageable pageable) { - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.Repository#findById(java.io.Serializable - * ) - */ - public T findById(ID id) { + Long count = count(); + List list = findAll(QueryUtils.applyPagination(new Query(), pageable)); - MongoConverter converter = template.getConverter(); - ObjectId objectId = converter.convertObjectId(id); + return new PageImpl(list, pageable, count); + } - return template.findOne(getCollectionName(getDomainClass()), new Query( - where("_id").is(objectId)), getDomainClass()); - } + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.PagingAndSortingRepository#findAll + * (org.springframework.data.domain.Sort) + */ + public List findAll(final Sort sort) { + return findAll(QueryUtils.applySorting(new Query(), sort)); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.Repository#exists(java.io.Serializable - * ) - */ - public boolean exists(ID id) { + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.Repository#findAll(java.lang.Iterable) + */ + public List findAll(Iterable ids) { - return findById(id) != null; - } + Query query = null; + for (ID id : ids) { + if (query == null) { + query = getIdQuery(id); + } else { + query = new Query().or(getIdQuery(id)); + } + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.Repository#findAll() - */ - public List findAll() { + return findAll(query); + } - return template.getCollection(getCollectionName(getDomainClass()), - getDomainClass()); - } + private List findAll(Query query) { + if (query == null) { + return Collections.emptyList(); + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.Repository#count() - */ - public Long count() { - - return template.getCollection(getCollectionName(getDomainClass())) - .count(); - } - - - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.Repository#delete(java.lang.Object) - */ - public void delete(T entity) { - - Object id = entityInformation.getId(entity); - ObjectId objectId = template.getConverter().convertObjectId(id); - - Query query = - new Query(where("_id").is( - objectId)); - template.remove(getCollectionName(getDomainClass()), query); - } - - - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.Repository#delete(java.lang.Iterable) - */ - public void delete(Iterable entities) { - - for (T entity : entities) { - delete(entity); - } - } - - - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.Repository#deleteAll() - */ - public void deleteAll() { - - template.dropCollection(getCollectionName(getDomainClass())); - } - - - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.PagingAndSortingRepository#findAll - * (org.springframework.data.domain.Pageable) - */ - public Page findAll(final Pageable pageable) { - - Long count = count(); - Query spec = new Query(); - - List list = - template.find(getCollectionName(getDomainClass()), - QueryUtils.applyPagination(spec, pageable), - getDomainClass()); - - return new PageImpl(list, pageable, count); - } - - - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.PagingAndSortingRepository#findAll - * (org.springframework.data.domain.Sort) - */ - public List findAll(final Sort sort) { - - Query query = QueryUtils.applySorting(new Query(), sort); - return template.find(getCollectionName(getDomainClass()), query, - getDomainClass()); - } + return template.find(entityInformation.getCollectionName(), query, entityInformation.getJavaType()); + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/MongoEntityMetadataUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/MongoEntityMetadataUnitTests.java index 4ee90c0c8..7446377ab 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/MongoEntityMetadataUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/MongoEntityMetadataUnitTests.java @@ -22,7 +22,7 @@ import org.junit.Test; /** - * Unit test for {@link MongoEntityMetadata}. + * Unit test for {@link MongoEntityInformation}. * * @author Oliver Gierke */ @@ -31,8 +31,8 @@ public class MongoEntityMetadataUnitTests { @Test public void findsIdField() throws Exception { - MongoEntityMetadata isNewAware = - new MongoEntityMetadata(Person.class); + MongoEntityInformation isNewAware = + new MongoEntityInformation(Person.class); Person person = new Person(); assertThat(isNewAware.isNew(person), is(true)); @@ -44,7 +44,7 @@ public class MongoEntityMetadataUnitTests { @Test(expected = IllegalArgumentException.class) public void rejectsClassIfNoIdField() throws Exception { - new MongoEntityMetadata(InvalidPerson.class); + new MongoEntityInformation(InvalidPerson.class); } class Person {