DATADOC-226 - Added QuerydslRepositorySupport class to ease implementing repositories using Querydsl predicates.
This commit is contained in:
@@ -24,14 +24,14 @@ 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.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverter;
|
||||
import org.springframework.data.querydsl.EntityPathResolver;
|
||||
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
||||
import org.springframework.data.querydsl.SimpleEntityPathResolver;
|
||||
import org.springframework.data.repository.core.EntityMetadata;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mysema.query.mongodb.MongodbQuery;
|
||||
import com.mysema.query.mongodb.MongodbSerializer;
|
||||
@@ -42,16 +42,13 @@ 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
|
||||
* Special QueryDsl based repository implementation that allows execution {@link Predicate}s in various forms.
|
||||
*
|
||||
* @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;
|
||||
|
||||
@@ -79,11 +76,10 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
|
||||
EntityPathResolver resolver) {
|
||||
|
||||
super(entityInformation, template);
|
||||
this.transformer = new MongoConverterTransformer(template.getConverter());
|
||||
this.serializer = new MongodbSerializer();
|
||||
|
||||
Assert.notNull(resolver);
|
||||
EntityPath<T> path = resolver.createPath(entityInformation.getJavaType());
|
||||
this.builder = new PathBuilder<T>(path.getType(), path.getMetadata());
|
||||
this.serializer = new MongodbSerializer();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -158,8 +154,14 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
|
||||
* @return
|
||||
*/
|
||||
private MongodbQuery<T> createQueryFor(Predicate predicate) {
|
||||
|
||||
MongodbQuery<T> query = new MongoTemplateQuery(getMongoOperations());
|
||||
|
||||
DBCollection collection = getMongoOperations().getCollection(getEntityInformation().getCollectionName());
|
||||
MongodbQuery<T> query = new MongodbQuery<T>(collection, new Transformer<DBObject, T>() {
|
||||
public T transform(DBObject input) {
|
||||
Class<T> type = getEntityInformation().getJavaType();
|
||||
return getMongoOperations().getConverter().read(type, input);
|
||||
}
|
||||
}, serializer);
|
||||
return query.where(predicate);
|
||||
}
|
||||
|
||||
@@ -214,48 +216,4 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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.mongodb.repository;
|
||||
|
||||
import org.apache.commons.collections15.Transformer;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mysema.query.mongodb.MongodbQuery;
|
||||
import com.mysema.query.mongodb.MongodbSerializer;
|
||||
import com.mysema.query.types.EntityPath;
|
||||
|
||||
/**
|
||||
* Base class to create repository implementations based on Querydsl.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class QuerydslRepositorySupport {
|
||||
|
||||
private final MongoOperations template;
|
||||
private final MappingContext<? extends MongoPersistentEntity<?>, ?> context;
|
||||
private final MongodbSerializer serializer;
|
||||
|
||||
/**
|
||||
* Creates a new {@link QuerydslRepositorySupport} for the given {@link MongoOperations}.
|
||||
*
|
||||
* @param operations must not be {@literal null}
|
||||
*/
|
||||
public QuerydslRepositorySupport(MongoOperations operations) {
|
||||
Assert.notNull(operations);
|
||||
this.template = operations;
|
||||
this.context = operations.getConverter().getMappingContext();
|
||||
this.serializer = new MongodbSerializer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link MongodbQuery} for the given {@link EntityPath}. The collection being queried is derived from the
|
||||
* entity metadata.
|
||||
*
|
||||
* @param path
|
||||
* @return
|
||||
*/
|
||||
protected <T> MongodbQuery<T> from(final EntityPath<T> path) {
|
||||
Assert.notNull(path);
|
||||
MongoPersistentEntity<?> entity = context.getPersistentEntity(path.getType());
|
||||
return from(path, entity.getCollection());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link MongodbQuery} for the given {@link EntityPath} querying the given collection.
|
||||
*
|
||||
* @param path must not be {@literal null}
|
||||
* @param collection must not be blank or {@literal null}
|
||||
* @return
|
||||
*/
|
||||
protected <T> MongodbQuery<T> from(final EntityPath<T> path, String collection) {
|
||||
|
||||
Assert.notNull(path);
|
||||
Assert.hasText(collection);
|
||||
|
||||
DBCollection dbCollection = template.getCollection(collection);
|
||||
return new MongodbQuery<T>(dbCollection, new Transformer<DBObject, T>() {
|
||||
public T transform(DBObject input) {
|
||||
return template.getConverter().read(path.getType(), input);
|
||||
}
|
||||
}, serializer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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.mongodb.repository;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.mysema.query.mongodb.MongodbQuery;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link QuerydslRepositorySupport}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:infrastructure.xml")
|
||||
public class QuerydslRepositorySupportUnitTests {
|
||||
|
||||
@Autowired
|
||||
MongoOperations operations;
|
||||
Person person;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
operations.remove(new Query(), Person.class);
|
||||
person = new Person("Dave", "Matthews");
|
||||
operations.save(person);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void providesMongoQuery() {
|
||||
QPerson p = QPerson.person;
|
||||
QuerydslRepositorySupport support = new QuerydslRepositorySupport(operations) {};
|
||||
MongodbQuery<Person> query = support.from(p).where(p.lastname.eq("Matthews"));
|
||||
assertThat(query.uniqueResult(), is(person));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user