DATAMONGO-1238 - Upgraded to Querydsl 4.

This commit is contained in:
Oliver Gierke
2015-11-16 17:50:33 +01:00
parent e66e1e0502
commit 9dce117555
12 changed files with 85 additions and 104 deletions

View File

@@ -59,14 +59,14 @@
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-mongodb</artifactId>
<version>${querydsl}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl}</version>
<scope>provided</scope>
@@ -183,7 +183,7 @@
<version>${apt}</version>
<dependencies>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl}</version>
</dependency>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2015 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.
@@ -25,14 +25,14 @@ import javax.tools.Diagnostic;
import org.springframework.data.mongodb.core.mapping.Document;
import com.mysema.query.annotations.QueryEmbeddable;
import com.mysema.query.annotations.QueryEmbedded;
import com.mysema.query.annotations.QueryEntities;
import com.mysema.query.annotations.QuerySupertype;
import com.mysema.query.annotations.QueryTransient;
import com.mysema.query.apt.AbstractQuerydslProcessor;
import com.mysema.query.apt.Configuration;
import com.mysema.query.apt.DefaultConfiguration;
import com.querydsl.apt.AbstractQuerydslProcessor;
import com.querydsl.apt.Configuration;
import com.querydsl.apt.DefaultConfiguration;
import com.querydsl.core.annotations.QueryEmbeddable;
import com.querydsl.core.annotations.QueryEmbedded;
import com.querydsl.core.annotations.QueryEntities;
import com.querydsl.core.annotations.QuerySupertype;
import com.querydsl.core.annotations.QueryTransient;
/**
* Annotation processor to create Querydsl query types for QueryDsl annotated classes.

View File

@@ -34,12 +34,12 @@ import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.core.EntityMetadata;
import org.springframework.util.Assert;
import com.mysema.query.mongodb.MongodbQuery;
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;
import com.querydsl.core.types.EntityPath;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.mongodb.AbstractMongodbQuery;
/**
* Special QueryDsl based repository implementation that allows execution {@link Predicate}s in various forms.
@@ -47,8 +47,8 @@ import com.mysema.query.types.path.PathBuilder;
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleMongoRepository<T, ID> implements
QueryDslPredicateExecutor<T> {
public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleMongoRepository<T, ID>
implements QueryDslPredicateExecutor<T> {
private final PathBuilder<T> builder;
private final EntityInformation<T, ID> entityInformation;
@@ -92,7 +92,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
*/
@Override
public T findOne(Predicate predicate) {
return createQueryFor(predicate).uniqueResult();
return createQueryFor(predicate).fetchOne();
}
/*
@@ -101,7 +101,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
*/
@Override
public List<T> findAll(Predicate predicate) {
return createQueryFor(predicate).list();
return createQueryFor(predicate).fetchResults().getResults();
}
/*
@@ -110,7 +110,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
*/
@Override
public List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
return createQueryFor(predicate).orderBy(orders).list();
return createQueryFor(predicate).orderBy(orders).fetchResults().getResults();
}
/*
@@ -119,7 +119,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
*/
@Override
public List<T> findAll(Predicate predicate, Sort sort) {
return applySorting(createQueryFor(predicate), sort).list();
return applySorting(createQueryFor(predicate), sort).fetchResults().getResults();
}
/*
@@ -128,7 +128,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
*/
@Override
public Iterable<T> findAll(OrderSpecifier<?>... orders) {
return createQuery().orderBy(orders).list();
return createQuery().orderBy(orders).fetchResults().getResults();
}
/*
@@ -138,10 +138,11 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
@Override
public Page<T> findAll(Predicate predicate, Pageable pageable) {
MongodbQuery<T> countQuery = createQueryFor(predicate);
MongodbQuery<T> query = createQueryFor(predicate);
AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> countQuery = createQueryFor(predicate);
AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> query = createQueryFor(predicate);
return new PageImpl<T>(applyPagination(query, pageable).list(), pageable, countQuery.count());
return new PageImpl<T>(applyPagination(query, pageable).fetchResults().getResults(), pageable,
countQuery.fetchCount());
}
/*
@@ -151,10 +152,11 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
@Override
public Page<T> findAll(Pageable pageable) {
MongodbQuery<T> countQuery = createQuery();
MongodbQuery<T> query = createQuery();
AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> countQuery = createQuery();
AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> query = createQuery();
return new PageImpl<T>(applyPagination(query, pageable).list(), pageable, countQuery.count());
return new PageImpl<T>(applyPagination(query, pageable).fetchResults().getResults(), pageable,
countQuery.fetchCount());
}
/*
@@ -163,7 +165,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
*/
@Override
public List<T> findAll(Sort sort) {
return applySorting(createQuery(), sort).list();
return applySorting(createQuery(), sort).fetchResults().getResults();
}
/*
@@ -172,7 +174,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
*/
@Override
public long count(Predicate predicate) {
return createQueryFor(predicate).count();
return createQueryFor(predicate).fetchCount();
}
/*
@@ -181,7 +183,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
*/
@Override
public boolean exists(Predicate predicate) {
return createQueryFor(predicate).exists();
return createQueryFor(predicate).fetchCount() > 0;
}
/**
@@ -190,7 +192,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
* @param predicate
* @return
*/
private MongodbQuery<T> createQueryFor(Predicate predicate) {
private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> createQueryFor(Predicate predicate) {
return createQuery().where(predicate);
}
@@ -199,7 +201,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
*
* @return
*/
private MongodbQuery<T> createQuery() {
private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> createQuery() {
return new SpringDataMongodbQuery<T>(mongoOperations, entityInformation.getJavaType());
}
@@ -210,7 +212,8 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
* @param pageable
* @return
*/
private MongodbQuery<T> applyPagination(MongodbQuery<T> query, Pageable pageable) {
private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> applyPagination(
AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> query, Pageable pageable) {
if (pageable == null) {
return query;
@@ -227,7 +230,8 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
* @param sort
* @return
*/
private MongodbQuery<T> applySorting(MongodbQuery<T> query, Sort sort) {
private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> applySorting(
AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> query, Sort sort) {
if (sort == null) {
return query;
@@ -260,7 +264,7 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
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);
return new OrderSpecifier(
order.isAscending() ? com.querydsl.core.types.Order.ASC : com.querydsl.core.types.Order.DESC, property);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2012 the original author or authors.
* Copyright 2011-2015 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.
@@ -20,8 +20,8 @@ import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.util.Assert;
import com.mysema.query.mongodb.MongodbQuery;
import com.mysema.query.types.EntityPath;
import com.querydsl.core.types.EntityPath;
import com.querydsl.mongodb.AbstractMongodbQuery;
/**
* Base class to create repository implementations based on Querydsl.
@@ -36,7 +36,7 @@ public abstract class QuerydslRepositorySupport {
/**
* Creates a new {@link QuerydslRepositorySupport} for the given {@link MongoOperations}.
*
* @param operations must not be {@literal null}
* @param operations must not be {@literal null}.
*/
public QuerydslRepositorySupport(MongoOperations operations) {
@@ -53,7 +53,7 @@ public abstract class QuerydslRepositorySupport {
* @param path
* @return
*/
protected <T> MongodbQuery<T> from(final EntityPath<T> path) {
protected <T> AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> from(final EntityPath<T> path) {
Assert.notNull(path);
MongoPersistentEntity<?> entity = context.getPersistentEntity(path.getType());
return from(path, entity.getCollection());
@@ -66,7 +66,7 @@ public abstract class QuerydslRepositorySupport {
* @param collection must not be blank or {@literal null}
* @return
*/
protected <T> MongodbQuery<T> from(final EntityPath<T> path, String collection) {
protected <T> AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> from(final EntityPath<T> path, String collection) {
Assert.notNull(path);
Assert.hasText(collection);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2015 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.
@@ -20,14 +20,14 @@ import org.springframework.data.mongodb.core.MongoOperations;
import com.google.common.base.Function;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mysema.query.mongodb.MongodbQuery;
import com.querydsl.mongodb.AbstractMongodbQuery;
/**
* Spring Data specfic {@link MongodbQuery} implementation.
* Spring Data specific {@link MongodbQuery} implementation.
*
* @author Oliver Gierke
*/
class SpringDataMongodbQuery<T> extends MongodbQuery<T> {
class SpringDataMongodbQuery<T> extends AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> {
private final MongoOperations operations;
@@ -48,7 +48,8 @@ class SpringDataMongodbQuery<T> extends MongodbQuery<T> {
* @param type must not be {@literal null}.
* @param collectionName must not be {@literal null} or empty.
*/
public SpringDataMongodbQuery(final MongoOperations operations, final Class<? extends T> type, String collectionName) {
public SpringDataMongodbQuery(final MongoOperations operations, final Class<? extends T> type,
String collectionName) {
super(operations.getCollection(collectionName), new Function<DBObject, T>() {
public T apply(DBObject input) {
@@ -61,7 +62,7 @@ class SpringDataMongodbQuery<T> extends MongodbQuery<T> {
/*
* (non-Javadoc)
* @see com.mysema.query.mongodb.MongodbQuery#getCollection(java.lang.Class)
* @see com.querydsl.mongodb.AbstractMongodbQuery#getCollection(java.lang.Class)
*/
@Override
protected DBCollection getCollection(Class<?> type) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2014 the original author or authors.
* Copyright 2011-2015 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.
@@ -29,12 +29,10 @@ import org.springframework.util.Assert;
import com.mongodb.DBObject;
import com.mongodb.DBRef;
import com.mysema.query.mongodb.MongodbSerializer;
import com.mysema.query.types.Constant;
import com.mysema.query.types.Operation;
import com.mysema.query.types.Path;
import com.mysema.query.types.PathMetadata;
import com.mysema.query.types.PathType;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.PathMetadata;
import com.querydsl.core.types.PathType;
import com.querydsl.mongodb.MongodbSerializer;
/**
* Custom {@link MongodbSerializer} to take mapping information into account when building keys for constraints.
@@ -76,10 +74,10 @@ class SpringDataMongodbSerializer extends MongodbSerializer {
/*
* (non-Javadoc)
* @see com.mysema.query.mongodb.MongodbSerializer#getKeyForPath(com.mysema.query.types.Path, com.mysema.query.types.PathMetadata)
* @see com.querydsl.mongodb.MongodbSerializer#getKeyForPath(com.querydsl.core.types.Path, com.querydsl.core.types.PathMetadata)
*/
@Override
protected String getKeyForPath(Path<?> expr, PathMetadata<?> metadata) {
protected String getKeyForPath(Path<?> expr, PathMetadata metadata) {
if (!metadata.getPathType().equals(PathType.PROPERTY)) {
return super.getKeyForPath(expr, metadata);
@@ -94,7 +92,7 @@ class SpringDataMongodbSerializer extends MongodbSerializer {
/*
* (non-Javadoc)
* @see com.mysema.query.mongodb.MongodbSerializer#asDBObject(java.lang.String, java.lang.Object)
* @see com.querydsl.mongodb.MongodbSerializer#asDBObject(java.lang.String, java.lang.Object)
*/
@Override
protected DBObject asDBObject(String key, Object value) {
@@ -108,7 +106,7 @@ class SpringDataMongodbSerializer extends MongodbSerializer {
/*
* (non-Javadoc)
* @see com.mysema.query.mongodb.MongodbSerializer#isReference(com.mysema.query.types.Path)
* @see com.querydsl.mongodb.MongodbSerializer#isReference(com.querydsl.core.types.Path)
*/
@Override
protected boolean isReference(Path<?> path) {
@@ -119,34 +117,13 @@ class SpringDataMongodbSerializer extends MongodbSerializer {
/*
* (non-Javadoc)
* @see com.mysema.query.mongodb.MongodbSerializer#asReference(java.lang.Object)
* @see com.querydsl.mongodb.MongodbSerializer#asReference(java.lang.Object)
*/
@Override
protected DBRef asReference(Object constant) {
return converter.toDBRef(constant, null);
}
/*
* (non-Javadoc)
* @see com.mysema.query.mongodb.MongodbSerializer#asReference(com.mysema.query.types.Operation, int)
*/
@Override
protected DBRef asReference(Operation<?> expr, int constIndex) {
for (Object arg : expr.getArgs()) {
if (arg instanceof Path) {
MongoPersistentProperty property = getPropertyFor((Path<?>) arg);
Object constant = ((Constant<?>) expr.getArg(constIndex)).getConstant();
return converter.toDBRef(constant, property);
}
}
return super.asReference(expr, constIndex);
}
private MongoPersistentProperty getPropertyFor(Path<?> path) {
Path<?> parent = path.getMetadata().getParent();

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011 by the original author(s).
* Copyright 2011-2015 by the original author(s).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,14 +16,15 @@
package org.springframework.data.mongodb.core.mapping;
import com.mysema.query.annotations.QuerySupertype;
import com.querydsl.core.annotations.QuerySupertype;
/**
* {@link QuerySupertype} is necessary for Querydsl 2.2.0-beta4 to compile the query classes directly. Can be removed as
* soon as {@link https://bugs.launchpad.net/querydsl/+bug/776219} is fixed.
*
* @see https://bugs.launchpad.net/querydsl/+bug/776219
* @author Jon Brisbin <jbrisbin@vmware.com>
* @author Jon Brisbin
* @author Oliver Gierke
*/
@QuerySupertype
public abstract class BasePerson {
@@ -32,8 +33,7 @@ public abstract class BasePerson {
protected String firstName;
protected String lastName;
public BasePerson() {
}
public BasePerson() {}
public BasePerson(Integer ssn, String firstName, String lastName) {
this.ssn = ssn;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2011-2015 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.
@@ -15,7 +15,7 @@
*/
package org.springframework.data.mongodb.repository;
import com.mysema.query.annotations.QueryEmbeddable;
import com.querydsl.core.annotations.QueryEmbeddable;
/**
* @author Oliver Gierke

View File

@@ -34,7 +34,7 @@ import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mysema.query.types.Predicate;
import com.querydsl.core.types.Predicate;
/**
* Integration test for {@link QueryDslMongoRepository}.

View File

@@ -31,8 +31,6 @@ import org.springframework.data.mongodb.repository.QPerson;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mysema.query.mongodb.MongodbQuery;
/**
* Unit tests for {@link QuerydslRepositorySupport}.
*
@@ -59,8 +57,8 @@ public class QuerydslRepositorySupportTests {
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));
SpringDataMongodbQuery<Person> query = support.from(p).where(p.lastname.eq("Matthews"));
assertThat(query.fetchOne(), is(person));
}
/**
@@ -76,8 +74,8 @@ public class QuerydslRepositorySupportTests {
QPerson p = QPerson.person;
QuerydslRepositorySupport support = new QuerydslRepositorySupport(operations) {};
MongodbQuery<Person> query = support.from(p).where(p.skills.any().in("guitarist"));
SpringDataMongodbQuery<Person> query = support.from(p).where(p.skills.any().in("guitarist"));
assertThat(query.uniqueResult(), is(person));
assertThat(query.fetchOne(), is(person));
}
}

View File

@@ -37,10 +37,10 @@ import org.springframework.data.mongodb.repository.QPerson;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mysema.query.types.expr.BooleanOperation;
import com.mysema.query.types.path.PathBuilder;
import com.mysema.query.types.path.SimplePath;
import com.mysema.query.types.path.StringPath;
import com.querydsl.core.types.dsl.BooleanOperation;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.core.types.dsl.SimplePath;
import com.querydsl.core.types.dsl.StringPath;
/**
* Unit tests for {@link SpringDataMongodbSerializer}.
@@ -73,6 +73,7 @@ public class SpringDataMongodbSerializerUnitTests {
@Test
public void buildsNestedKeyCorrectly() {
StringPath path = QPerson.person.address.street;
assertThat(serializer.getKeyForPath(path, path.getMetadata()), is("street"));
}

View File

@@ -10,7 +10,7 @@ Import-Template:
com.fasterxml.jackson.*;version="${jackson:[=.=.=,+1.0.0)}";resolution:=optional,
com.google.common.base.*;version="[11.0.0,14.0.0)";resolution:=optional,
com.mongodb.*;version="${mongo.osgi:[=.=.=,+1.0.0)}",
com.mysema.query.*;version="[2.1.1, 3.0.0)";resolution:=optional,
com.querydsl.*;version="${querydsl:[=.=.=,+1.0.0)}";resolution:=optional,
javax.annotation.processing.*;version="0",
javax.enterprise.*;version="${cdi:[=.=.=,+1.0.0)}";resolution:=optional,
javax.tools.*;version="0",