DATADOC-41 - Initial version of QueryDsl integration for Mongo DB.

Added necessary dependencies, repository and APT processor plugin. Added QueryDslPredicateExecutor and a QueryDsl specific sub-class of SimpleMongoRepository.
Adapted MongoRepositoryFactory to use new QueryDslRepository implementation when a repository interface extends QueryDslPredicateExecutor. Added AnnotationProcessor to create query classes from QueryDsl annotations.

Todo's left open:

- move common QueryDsl integration code into Spring Data commons (see TODOs in the sourcecode)
This commit is contained in:
Oliver Gierke
2011-02-14 14:22:15 +01:00
parent e3bee8ab0e
commit 673c70b7de
14 changed files with 910 additions and 230 deletions

View File

@@ -44,44 +44,70 @@
<version>${data.commons.version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-mongodb</artifactId>
<version>2.1.1</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.google.code.morphia</groupId>
<artifactId>morphia</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>2.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
@@ -94,8 +120,9 @@
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
@@ -107,27 +134,49 @@
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- MongoDB -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.springsource.bundlor</groupId>
<artifactId>com.springsource.bundlor.maven</artifactId>
</plugin>
</plugins>
</build>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.springsource.bundlor</groupId>
<artifactId>com.springsource.bundlor.maven</artifactId>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>maven-apt-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>generate-test-sources</phase>
<goals>
<goal>test-process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/test-annotations</outputDirectory>
<processor>org.springframework.data.document.mongodb.repository.MongoAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>querydsl</id>
<name>Mysema QueryDsl</name>
<url>http://source.mysema.com/maven2/releases</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.document.mongodb.repository;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import com.mysema.query.annotations.QueryEmbeddable;
import com.mysema.query.annotations.QueryEmbedded;
import com.mysema.query.annotations.QueryEntities;
import com.mysema.query.annotations.QueryEntity;
import com.mysema.query.annotations.QuerySupertype;
import com.mysema.query.annotations.QueryTransient;
import com.mysema.query.apt.DefaultConfiguration;
import com.mysema.query.apt.Processor;
/**
* Annotation processor to create Querydsl query types for QueryDsl annoated
* classes
*
* @author Oliver Gierke
*/
@SupportedAnnotationTypes({ "com.mysema.query.annotations.*" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class MongoAnnotationProcessor extends AbstractProcessor {
private Class<? extends Annotation> entities, entity, embedded, skip;
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,
"Running " + getClass().getSimpleName());
DefaultConfiguration configuration =
new DefaultConfiguration(roundEnv, processingEnv.getOptions(),
Collections.<String> emptySet(), QueryEntities.class,
QueryEntity.class, QuerySupertype.class,
QueryEmbeddable.class, QueryEmbedded.class,
QueryTransient.class);
Processor processor =
new Processor(processingEnv, roundEnv, configuration);
processor.process();
return true;
}
}

View File

@@ -25,7 +25,7 @@ import org.springframework.util.StringUtils;
/**
* TODO - Extract methods for {@link #getAnnotatedQuery()} into superclass as it is currently copied from Spring Data
* JPA
*
*
* @author Oliver Gierke
*/
class MongoQueryMethod extends QueryMethod {
@@ -35,7 +35,7 @@ class MongoQueryMethod extends QueryMethod {
/**
* Creates a new {@link MongoQueryMethod} from the given {@link Method}.
*
*
* @param method
*/
public MongoQueryMethod(Method method, Class<?> domainClass) {
@@ -44,10 +44,9 @@ class MongoQueryMethod extends QueryMethod {
this.entityInformation = new MongoEntityInformation(ClassUtils.getReturnedDomainClass(method));
}
/**
* Returns whether the method has an annotated query.
*
*
* @return
*/
boolean hasAnnotatedQuery() {
@@ -55,9 +54,9 @@ class MongoQueryMethod extends QueryMethod {
}
/**
* Returns the query string declared in a {@link Query} annotation or {@literal null} if neither the annotation
* found nor the attribute was specified.
*
* Returns the query string declared in a {@link Query} annotation or {@literal null} if neither the annotation found
* nor the attribute was specified.
*
* @return
*/
String getAnnotatedQuery() {
@@ -68,7 +67,7 @@ class MongoQueryMethod extends QueryMethod {
/**
* Returns the field specification to be used for the query.
*
*
* @return
*/
String getFieldSpecification() {
@@ -77,7 +76,6 @@ class MongoQueryMethod extends QueryMethod {
return StringUtils.hasText(value) ? value : null;
}
/* (non-Javadoc)
* @see org.springframework.data.repository.query.QueryMethod#getEntityMetadata()
*/
@@ -89,7 +87,7 @@ class MongoQueryMethod extends QueryMethod {
/**
* Returns the {@link Query} annotation that is applied to the method or {@code null} if none available.
*
*
* @return
*/
private Query getQueryAnnotation() {

View File

@@ -40,17 +40,19 @@ import org.springframework.util.StringUtils;
/**
* {@link org.springframework.beans.factory.FactoryBean} to create {@link MongoRepository} instances.
*
*
* @author Oliver Gierke
*/
public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID extends Serializable> extends RepositoryFactoryBeanSupport<T, S, ID> {
public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID extends Serializable> extends
RepositoryFactoryBeanSupport<T, S, ID> {
private MongoTemplate template;
/**
* Configures the {@link MongoTemplate} to be used.
*
* @param template the template to set
*
* @param template
* the template to set
*/
public void setTemplate(MongoTemplate template) {
@@ -58,10 +60,12 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.support.RepositoryFactoryBeanSupport #createRepositoryFactory()
*/
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.support.RepositoryFactoryBeanSupport
* #createRepositoryFactory()
*/
@Override
protected RepositoryFactorySupport createRepositoryFactory() {
@@ -71,10 +75,12 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.support.RepositoryFactoryBeanSupport #afterPropertiesSet()
*/
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.support.RepositoryFactoryBeanSupport
* #afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() {
@@ -84,16 +90,19 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
/**
* Repository to create {@link MongoRepository} instances.
*
*
* @author Oliver Gierke
*/
public static class MongoRepositoryFactory extends RepositoryFactorySupport {
private static final boolean QUERY_DSL_PRESENT = org.springframework.util.ClassUtils.isPresent(
"com.mysema.query.types.Predicate", MongoRepositoryFactory.class.getClassLoader());
private final MongoTemplate template;
/**
* Creates a new {@link MongoRepositoryFactory} fwith the given {@link MongoTemplate}.
*
*
* @param template
*/
public MongoRepositoryFactory(MongoTemplate template) {
@@ -101,17 +110,54 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
this.template = template;
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.support.RepositoryFactorySupport#getRepositoryBaseClass()
*/
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.support.RepositoryFactorySupport
* #getRepositoryBaseClass()
*/
@Override
protected Class<?> getRepositoryBaseClass(Class<?> repositoryInterface) {
return SimpleMongoRepository.class;
return isQueryDslRepository(repositoryInterface) ? QueryDslMongoRepository.class : SimpleMongoRepository.class;
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.support.RepositoryFactorySupport
* #getTargetRepository
* (org.springframework.data.repository.support.RepositoryMetadata)
*/
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object getTargetRepository(RepositoryMetadata metadata) {
Class<?> repositoryInterface = metadata.getRepositoryInterface();
MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainClass());
if (isQueryDslRepository(repositoryInterface)) {
return new QueryDslMongoRepository(entityInformation, template);
} else {
return new SimpleMongoRepository(entityInformation, template);
}
}
private static boolean isQueryDslRepository(Class<?> repositoryInterface) {
return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface);
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.support.RepositoryFactorySupport
* #getQueryLookupStrategy
* (org.springframework.data.repository.query.QueryLookupStrategy.Key)
*/
@Override
protected QueryLookupStrategy getQueryLookupStrategy(Key key) {
@@ -120,11 +166,18 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
/**
* {@link QueryLookupStrategy} to create {@link PartTreeMongoQuery} instances.
*
*
* @author Oliver Gierke
*/
private class MongoQueryLookupStrategy implements QueryLookupStrategy {
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.query.QueryLookupStrategy
* #resolveQuery(java.lang.reflect.Method, java.lang.Class)
*/
public RepositoryQuery resolveQuery(Method method, Class<?> domainClass) {
MongoQueryMethod queryMethod = new MongoQueryMethod(method, domainClass);
@@ -138,11 +191,12 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.support.RepositoryFactorySupport#validate(java.lang.Class,
* java.lang.Object)
*/
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.support.RepositoryFactorySupport
* #validate(java.lang.Class, java.lang.Object)
*/
@Override
protected void validate(RepositoryMetadata metadata, Object customImplementation) {
@@ -155,36 +209,24 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
super.validate(metadata, customImplementation);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.support.RepositoryFactorySupport#getEntityInformation(java.lang.Class)
*/
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.support.RepositoryFactorySupport
* #getEntityInformation(java.lang.Class)
*/
@Override
public <T, ID extends Serializable> MongoEntityInformation<T, ID> getEntityInformation(
Class<T> domainClass) {
public <T, ID extends Serializable> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
return new MongoEntityInformation<T, ID>(domainClass);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.support.RepositoryFactorySupport#getTargetRepository(org.springframework.data.repository.support.RepositoryMetadata)
*/
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
protected Object getTargetRepository(RepositoryMetadata metadata) {
MongoEntityInformation<?, ?> info = getEntityInformation(
metadata.getDomainClass());
return new SimpleMongoRepository(info, template);
}
}
/**
* {@link QueryCreationListener} inspecting {@link PartTreeMongoQuery}s and creating an index for the properties it
* refers to.
*
*
* @author Oliver Gierke
*/
private static class IndexEnsuringQueryCreationListener implements QueryCreationListener<PartTreeMongoQuery> {
@@ -193,16 +235,18 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
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)
*/
* (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();

View File

@@ -0,0 +1,413 @@
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.document.mongodb.repository;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import org.apache.commons.collections15.Transformer;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.convert.MongoConverter;
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.domain.Sort.Order;
import org.springframework.data.repository.support.EntityMetadata;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import com.mongodb.DBObject;
import com.mysema.query.mongodb.MongodbQuery;
import com.mysema.query.mongodb.MongodbSerializer;
import com.mysema.query.types.EntityPath;
import com.mysema.query.types.Expression;
import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.Predicate;
import com.mysema.query.types.path.PathBuilder;
/**
* Special QueryDsl based repository implementation that allows execution
* {@link Predicate}s in various forms. TODO: Extract {@link EntityPathResolver}
* into Spring Data Commons TODO: Refactor Spring Data JPA to use this common
* infrastructure
*
* @author Oliver Gierke
*/
public class QueryDslMongoRepository<T, ID extends Serializable> extends
SimpleMongoRepository<T, ID> implements QueryDslPredicateExecutor<T> {
private final MongoConverterTransformer transformer;
private final MongodbSerializer serializer;
private final PathBuilder<T> builder;
/**
* Creates a new {@link QueryDslMongoRepository} for the given
* {@link EntityMetadata} and {@link MongoTemplate}. Uses the
* {@link SimpleEntityPathResolver} to create an {@link EntityPath} for the
* given domain class.
*
* @param entityInformation
* @param template
*/
public QueryDslMongoRepository(
MongoEntityInformation<T, ID> entityInformation, MongoTemplate template) {
this(entityInformation, template, SimpleEntityPathResolver.INSTANCE);
}
/**
* Creates a new {@link QueryDslMongoRepository} for the given domain class,
* {@link MongoTemplate} and {@link EntityPathResolver}.
*
* @param entityInformation
* @param template
* @param resolver
*/
public QueryDslMongoRepository(
MongoEntityInformation<T, ID> entityInformation,
MongoTemplate template, EntityPathResolver resolver) {
super(entityInformation, template);
this.transformer = new MongoConverterTransformer(template.getConverter());
this.serializer = new MongodbSerializer();
EntityPath<T> path = resolver.createPath(entityInformation.getJavaType());
this.builder = new PathBuilder<T>(path.getType(), path.getMetadata());
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.document.mongodb.repository.QueryDslExecutor
* #findOne(com.mysema.query.types.Predicate)
*/
public T findOne(Predicate predicate) {
return createQueryFor(predicate).uniqueResult();
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.document.mongodb.repository.QueryDslExecutor
* #findAll(com.mysema.query.types.Predicate)
*/
public List<T> findAll(Predicate predicate) {
return createQueryFor(predicate).list();
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.document.mongodb.repository.QueryDslExecutor
* #findAll(com.mysema.query.types.Predicate,
* com.mysema.query.types.OrderSpecifier<?>[])
*/
public List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
return createQueryFor(predicate).orderBy(orders).list();
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.document.mongodb.repository.QueryDslExecutor
* #findAll(com.mysema.query.types.Predicate,
* org.springframework.data.domain.Pageable)
*/
public Page<T> findAll(Predicate predicate, Pageable pageable) {
MongodbQuery<T> countQuery = createQueryFor(predicate);
MongodbQuery<T> query = createQueryFor(predicate);
return new PageImpl<T>(applyPagination(query, pageable).list(),
pageable, countQuery.count());
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.document.mongodb.repository.QueryDslExecutor
* #count(com.mysema.query.types.Predicate)
*/
public Long count(Predicate predicate) {
return createQueryFor(predicate).count();
}
/**
* Creates a {@link MongodbQuery} for the given {@link Predicate}.
*
* @param predicate
* @return
*/
private MongodbQuery<T> createQueryFor(Predicate predicate) {
MongodbQuery<T> query = new MongoTemplateQuery(getMongoOperations());
return query.where(predicate);
}
/**
* Applies the given {@link Pageable} to the given {@link MongodbQuery}.
*
* @param query
* @param pageable
* @return
*/
private MongodbQuery<T> applyPagination(MongodbQuery<T> query,
Pageable pageable) {
if (pageable == null) {
return query;
}
query =
query.offset(pageable.getOffset())
.limit(pageable.getPageSize());
return applySorting(query, pageable.getSort());
}
/**
* Applies the given {@link Sort} to the given {@link MongodbQuery}.
*
* @param query
* @param sort
* @return
*/
private MongodbQuery<T> applySorting(MongodbQuery<T> query, Sort sort) {
if (sort == null) {
return query;
}
for (Order order : sort) {
query.orderBy(toOrder(order));
}
return query;
}
/**
* Transforms a plain {@link Order} into a QueryDsl specific
* {@link OrderSpecifier}.
*
* @param order
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private OrderSpecifier<?> toOrder(Order order) {
Expression<Object> property = builder.get(order.getProperty());
return new OrderSpecifier(
order.isAscending() ? com.mysema.query.types.Order.ASC
: com.mysema.query.types.Order.DESC, property);
}
/**
* Special {@link MongodbQuery} implementation to use our
* {@link MongoOperations} for actually accessing Mongo.
*
* @author Oliver Gierke
*/
private class MongoTemplateQuery extends MongodbQuery<T> {
public MongoTemplateQuery(MongoOperations operations) {
super(operations.getCollection(getEntityInformation()
.getCollectionName()), transformer, serializer);
}
}
/**
* {@link Transformer} implementation to delegate to a
* {@link MongoConverter}.
*
* @author Oliver Gierke
*/
private class MongoConverterTransformer implements Transformer<DBObject, T> {
private final MongoConverter converter;
/**
* Creates a new {@link MongoConverterTransformer} with the given
* {@link MongoConverter}.
*
* @param converter
*/
public MongoConverterTransformer(MongoConverter converter) {
this.converter = converter;
}
/*
* (non-Javadoc)
*
* @see
* org.apache.commons.collections15.Transformer#transform(java.lang.
* Object)
*/
public T transform(DBObject input) {
return converter.read(getEntityInformation().getJavaType(), input);
}
}
/**
* Strategy interface to abstract the ways to translate an plain domain
* class into a {@link EntityPath}.
*
* @author Oliver Gierke
*/
public static interface EntityPathResolver {
<T> EntityPath<T> createPath(Class<T> domainClass);
}
/**
* Simple implementation of {@link EntityPathResolver} to lookup a query
* class by reflection and using the static field of the same type.
*
* @author Oliver Gierke
*/
static enum SimpleEntityPathResolver implements EntityPathResolver {
INSTANCE;
private static final String NO_CLASS_FOUND_TEMPLATE =
"Did not find a query class %s for domain class %s!";
private static final String NO_FIELD_FOUND_TEMPLATE =
"Did not find a static field of the same type in %s!";
/**
* Creates an {@link EntityPath} instance for the given domain class.
* Tries to lookup a class matching the naming convention (prepend Q to
* the simple name of the class, same package) and find a static field
* of the same type in it.
*
* @param domainClass
* @return
*/
@SuppressWarnings("unchecked")
public <T> EntityPath<T> createPath(Class<T> domainClass) {
String pathClassName = getQueryClassName(domainClass);
try {
Class<?> pathClass =
ClassUtils.forName(pathClassName,
QueryDslMongoRepository.class.getClassLoader());
Field field = getStaticFieldOfType(pathClass);
if (field == null) {
throw new IllegalStateException(String.format(
NO_FIELD_FOUND_TEMPLATE, pathClass));
} else {
return (EntityPath<T>) ReflectionUtils
.getField(field, null);
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(String.format(
NO_CLASS_FOUND_TEMPLATE, pathClassName,
domainClass.getName()), e);
}
}
/**
* Returns the first static field of the given type inside the given
* type.
*
* @param type
* @return
*/
private Field getStaticFieldOfType(Class<?> type) {
for (Field field : type.getDeclaredFields()) {
boolean isStatic = Modifier.isStatic(field.getModifiers());
boolean hasSameType = type.equals(field.getType());
if (isStatic && hasSameType) {
return field;
}
}
Class<?> superclass = type.getSuperclass();
return Object.class.equals(superclass) ? null
: getStaticFieldOfType(superclass);
}
/**
* Returns the name of the query class for the given domain class.
*
* @param domainClass
* @return
*/
private String getQueryClassName(Class<?> domainClass) {
String simpleClassName = ClassUtils.getShortName(domainClass);
return String.format("%s.Q%s%s",
domainClass.getPackage().getName(),
getClassBase(simpleClassName), domainClass.getSimpleName());
}
/**
* Analyzes the short class name and potentially returns the outer
* class.
*
* @param shortName
* @return
*/
private String getClassBase(String shortName) {
String[] parts = shortName.split("\\.");
if (parts.length < 2) {
return "";
}
return parts[0] + "_";
}
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.document.mongodb.repository;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.Predicate;
/**
* Interface for query methods taking a QueryDsl {@link Predicate}.
*
* @author Oliver Gierke
*/
public interface QueryDslPredicateExecutor<T> {
/**
* Returns a single entity matching the given {@link Predicate}.
*
* @param spec
* @return
*/
T findOne(Predicate predicate);
/**
* Returns all entities matching the given {@link Predicate}.
*
* @param spec
* @return
*/
List<T> findAll(Predicate predicate);
/**
* Returns all entities matching the given {@link Predicate} applying the given {@link OrderSpecifier}s.
*
* @param predicate
* @param orders
* @return
*/
List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders);
/**
* Returns a {@link Page} of entities matching the given {@link Predicate}.
*
* @param predicate
* @param pageable
* @return
*/
Page<T> findAll(Predicate predicate, Pageable pageable);
/**
* Returns the number of instances that the given {@link Predicate} will return.
*
* @param predicate the {@link Predicate} to count instances for
* @return the number of instances
*/
Long count(Predicate predicate);
}

View File

@@ -15,7 +15,7 @@
*/
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 java.io.Serializable;
import java.util.ArrayList;
@@ -23,6 +23,7 @@ import java.util.Collections;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
@@ -35,7 +36,7 @@ import org.springframework.util.Assert;
/**
* Repository base implementation for Mongo.
*
*
* @author Oliver Gierke
*/
public class SimpleMongoRepository<T, ID extends Serializable> implements PagingAndSortingRepository<T, ID> {
@@ -45,7 +46,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
/**
* Creates a ew {@link SimpleMongoRepository} for the given {@link MongoInformation} and {@link MongoTemplate}.
*
*
* @param metadata
* @param template
*/
@@ -58,10 +59,11 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.Repository#save(java.lang.Object)
*/
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.Repository#save(java.lang.Object)
*/
public T save(T entity) {
template.save(entityInformation.getCollectionName(), entity);
@@ -69,10 +71,11 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.Repository#save(java.lang.Iterable)
*/
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.Repository#save(java.lang.Iterable)
*/
public List<T> save(Iterable<? extends T> entities) {
List<T> result = new ArrayList<T>();
@@ -86,10 +89,12 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.Repository#findById(java.io.Serializable )
*/
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.Repository#findById(java.io.Serializable
* )
*/
public T findOne(ID id) {
return template.findOne(entityInformation.getCollectionName(), getIdQuery(id), entityInformation.getJavaType());
@@ -101,46 +106,50 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
}
private Criteria getIdCriteria(Object id) {
ObjectId objectId = template.getConverter().convertObjectId(id);
return where(entityInformation.getIdAttribute()).is(objectId);
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.Repository#exists(java.io.Serializable )
*/
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.Repository#exists(java.io.Serializable
* )
*/
public boolean exists(ID id) {
return findOne(id) != null;
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.Repository#count()
*/
* (non-Javadoc)
*
* @see org.springframework.data.repository.Repository#count()
*/
public Long count() {
return template.getCollection(entityInformation.getCollectionName()).count();
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.Repository#delete(java.lang.Object)
*/
* (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#delete(java.lang.Iterable)
*/
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.Repository#delete(java.lang.Iterable)
*/
public void delete(Iterable<? extends T> entities) {
for (T entity : entities) {
@@ -149,28 +158,32 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.Repository#deleteAll()
*/
* (non-Javadoc)
*
* @see org.springframework.data.repository.Repository#deleteAll()
*/
public void deleteAll() {
template.dropCollection(entityInformation.getCollectionName());
}
/* (non-Javadoc)
* @see org.springframework.data.repository.Repository#findAll()
*/
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.Repository#findAll()
*/
public List<T> findAll() {
return findAll(new Query());
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll
* (org.springframework.data.domain.Pageable)
*/
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.PagingAndSortingRepository#findAll
* (org.springframework.data.domain.Pageable)
*/
public Page<T> findAll(final Pageable pageable) {
Long count = count();
@@ -180,21 +193,24 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll
* (org.springframework.data.domain.Sort)
*/
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.PagingAndSortingRepository#findAll
* (org.springframework.data.domain.Sort)
*/
public List<T> findAll(final Sort sort) {
return findAll(QueryUtils.applySorting(new Query(), sort));
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.Repository#findAll(java.lang.Iterable)
*/
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.Repository#findAll(java.lang.Iterable
* )
*/
public List<T> findAll(Iterable<ID> ids) {
Query query = null;
@@ -218,4 +234,22 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
return template.find(entityInformation.getCollectionName(), query, entityInformation.getJavaType());
}
/**
* Returns the underlying {@link MongoOperations} instance.
*
* @return
*/
protected MongoOperations getMongoOperations() {
return this.template;
}
/**
* @return the entityInformation
*/
protected MongoEntityInformation<T, ID> getEntityInformation() {
return entityInformation;
}
}

View File

@@ -16,14 +16,11 @@
package org.springframework.data.document.mongodb;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;
@@ -31,12 +28,15 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.dao.DataAccessException;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.data.document.mongodb.convert.MongoConverter;
/**
* Abstract base class for unit tests to specify behaviour we expect from {@link MongoOperations}. Subclasses return
* instances of their implementation and thus can see if it correctly implements the {@link MongoOperations} interface.
*
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
@@ -63,7 +63,7 @@ public abstract class MongoOperationsUnitTests {
dbo.put("firstName", person.getFirstName());
}
@SuppressWarnings({"unchecked"})
@SuppressWarnings("unchecked")
public <S extends Object> S read(Class<S> clazz, DBObject dbo) {
return (S) person;
}
@@ -78,22 +78,21 @@ public abstract class MongoOperationsUnitTests {
};
}
@Test(expected = IllegalArgumentException.class)
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings({ "unchecked", "rawtypes" })
public void rejectsNullForCollectionCallback() {
getOperations().execute((CollectionCallback) null);
}
@Test(expected = IllegalArgumentException.class)
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings({ "unchecked", "rawtypes" })
public void rejectsNullForCollectionCallback2() {
getOperations().execute("collection", (CollectionCallback) null);
}
@Test(expected = IllegalArgumentException.class)
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings({ "unchecked", "rawtypes" })
public void rejectsNullForDbCallback() {
getOperations().execute((DbCallback) null);
}
@@ -288,7 +287,6 @@ public abstract class MongoOperationsUnitTests {
}.assertDataAccessException();
}
@Test
public void convertsExceptionForInsertList() throws Exception {
new Execution() {
@@ -339,16 +337,16 @@ public abstract class MongoOperationsUnitTests {
}
/**
* Expects an {@link MongoOperations} instance that will be used to check that invoking methods on it will only
* cause {@link DataAccessException}s.
*
* Expects an {@link MongoOperations} instance that will be used to check that invoking methods on it will only cause
* {@link DataAccessException}s.
*
* @return
*/
protected abstract MongoOperations getOperationsForExceptionHandling();
/**
* Returns a plain {@link MongoOperations}.
*
*
* @return
*/
protected abstract MongoOperations getOperations();

View File

@@ -1,14 +1,13 @@
package org.springframework.data.document.mongodb.repository;
import static java.util.Arrays.asList;
import static java.util.Arrays.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -20,7 +19,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Base class for tests for {@link PersonRepository}.
*
*
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@@ -29,6 +28,7 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@Autowired
protected PersonRepository repository;
Person dave, carter, boyd, stefan, leroi;
QPerson person;
@Before
public void setUp() {
@@ -41,17 +41,9 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
stefan = new Person("Stefan", "Lessard", 34);
leroi = new Person("Leroi", "Moore", 41);
repository.save(Arrays.asList(dave, carter, boyd, stefan, leroi));
}
person = new QPerson("person");
@Test
public void existsWorksCorrectly() {
assertThat(repository.exists(dave.getId()), is(true));
assertThat(repository.exists(carter.getId()), is(true));
assertThat(repository.exists(boyd.getId()), is(true));
assertThat(repository.exists(stefan.getId()), is(true));
assertThat(repository.exists(leroi.getId()), is(true));
assertThat(repository.exists(new ObjectId().toString()), is(false));
repository.save(Arrays.asList(dave, carter, boyd, stefan, leroi));
}
@Test
@@ -163,36 +155,34 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
assertThat(result, hasItem(dave));
}
@Test
public void findsPeopleByFirstnameInVarargs() {
public void findsPeopleByQueryDslLastnameSpec() throws Exception {
List<Person> result = repository.findByFirstnameIn("Dave", "Carter");
assertThat(result.size(), is(2));
assertThat(result, hasItems(dave, carter));
List<Person> result = repository.findAll(person.lastname.eq("Matthews"));
assertThat(result.size(), is(1));
assertThat(result, hasItem(dave));
}
@Test
public void findsPeopleByFirstnameNotInCollection() {
public void findsPeopleByzipCodePredicate() throws Exception {
List<Person> result = repository.findByFirstnameNotIn(Arrays.asList("Boyd", "Carter"));
assertThat(result.size(), is(3));
assertThat(result, hasItems(dave, leroi, stefan));
Address address = new Address("Foo Street 1", "C0123", "Bar");
dave.setAddress(address);
repository.save(dave);
List<Person> result = repository.findAll(person.address.zipCode.eq("C0123"));
assertThat(result.size(), is(1));
assertThat(result, hasItem(dave));
}
@Test
public void findsPeopleByLastnameLikeAndAgeIn() throws Exception {
public void findsPagedPeopleByPredicate() throws Exception {
List<Person> result = repository.findByLastnameLikeAndAgeBetween("*e*", 44, 50);
assertThat(result.size(), is(2));
assertThat(result, hasItems(carter, boyd));
}
@Test
public void findsPeopleWithAndAndOr() throws Exception {
List<Person> result = repository.findByAgeOrLastnameLikeAndFirstnameLike(45, "*ss*", "*a*");
assertThat(result.size(), is(2));
assertThat(result, hasItems(boyd, stefan));
Page<Person> page = repository.findAll(person.lastname.contains("a"), new PageRequest(0, 2, Direction.ASC,
"lastname"));
assertThat(page.isFirstPage(), is(true));
assertThat(page.isLastPage(), is(false));
assertThat(page.getNumberOfElements(), is(2));
assertThat(page, hasItems(carter, stefan));
}
}

View File

@@ -15,9 +15,12 @@
*/
package org.springframework.data.document.mongodb.repository;
import com.mysema.query.annotations.QueryEmbeddable;
/**
* @author Oliver Gierke
*/
@QueryEmbeddable
public class Address {
private String street;

View File

@@ -19,12 +19,15 @@ import java.util.Set;
import org.bson.types.ObjectId;
import com.mysema.query.annotations.QueryEntity;
/**
* Sample domain class.
*
* @author Oliver Gierke
*/
@QueryEntity
public class Person {
private String id;

View File

@@ -27,7 +27,7 @@ import org.springframework.data.domain.Pageable;
*
* @author Oliver Gierke
*/
public interface PersonRepository extends MongoRepository<Person, String> {
public interface PersonRepository extends MongoRepository<Person, String>, QueryDslPredicateExecutor<Person> {
/**
* Returns all {@link Person}s with the given lastname.

View File

@@ -26,11 +26,12 @@ Import-Template:
org.springframework.expression.spel.support.*;version="[3.0.0, 4.0.0)",
org.springframework.validation.*;version="[3.0.0, 4.0.0)",
com.mongodb.*;version="0",
org.bson.*;version="0",
com.mysema.query.*;version="[2.1.1, 3.0.0)",
javax.annotation.processing.*;version="0",
javax.tools.*;version="0",
org.aopalliance.*;version="[1.0.0, 2.0.0)";resolution:=optional,
org.apache.commons.collections15.*;version="[4.0.0,5.0.0)",
org.apache.commons.logging.*;version="[1.1.1, 2.0.0)",
org.bson.*;version="0",
org.slf4j.*;version="[1.5.0,1.6.0)",
org.w3c.dom.*;version="0",
javax.persistence.*;version="0"
org.w3c.dom.*;version="0"

View File

@@ -8,6 +8,7 @@ Repository
* Adapted new metamodel API (DATADOC-47, DATACMNS-17)
* Added support for 'In' and 'NotIn' keyword (DATADOC-46)
* Fixed 'And' and 'Or' keywords
* Added support for executing QueryDsl predicates (DATADOC-41)
Changes in version 1.0.0.M1 MongoDB (2011-02-14)
------------------------------------------------