Skip to content

DATAMONGO-1690 - Adapt to QuerydslPredicateExecutor API changes. #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1690-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand All @@ -28,7 +28,7 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>2.0.0.BUILD-SNAPSHOT</springdata.commons>
<springdata.commons>2.0.0.DATACMNS-1059-SNAPSHOT</springdata.commons>
<mongo>3.4.2</mongo>
<mongo.reactivestreams>1.3.0</mongo.reactivestreams>
</properties>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1690-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1690-SNAPSHOT</version>
</dependency>

<!-- reactive -->
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1690-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1690-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1690-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return SimpleReactiveMongoRepository.class;
}

return isQueryDslRepository ? QueryDslMongoRepository.class : SimpleMongoRepository.class;
return isQueryDslRepository ? QuerydslMongoRepository.class : SimpleMongoRepository.class;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand All @@ -34,6 +36,7 @@
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.util.Assert;

import com.querydsl.core.NonUniqueResultException;
import com.querydsl.core.types.EntityPath;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.OrderSpecifier;
Expand All @@ -42,39 +45,40 @@
import com.querydsl.mongodb.AbstractMongodbQuery;

/**
* Special QueryDsl based repository implementation that allows execution {@link Predicate}s in various forms.
*
* Special Querydsl based repository implementation that allows execution {@link Predicate}s in various forms.
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Mark Paluch
* @author Christoph Strobl
*/
public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleMongoRepository<T, ID>
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;
private final MongoOperations mongoOperations;

/**
* Creates a new {@link QueryDslMongoRepository} for the given {@link EntityMetadata} and {@link MongoTemplate}. Uses
* 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 must not be {@literal null}.
* @param mongoOperations must not be {@literal null}.
*/
public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations) {
public QuerydslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations) {
this(entityInformation, mongoOperations, SimpleEntityPathResolver.INSTANCE);
}

/**
* Creates a new {@link QueryDslMongoRepository} for the given {@link MongoEntityInformation}, {@link MongoTemplate}
* Creates a new {@link QuerydslMongoRepository} for the given {@link MongoEntityInformation}, {@link MongoTemplate}
* and {@link EntityPathResolver}.
*
*
* @param entityInformation must not be {@literal null}.
* @param mongoOperations must not be {@literal null}.
* @param resolver must not be {@literal null}.
*/
public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations,
public QuerydslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations,
EntityPathResolver resolver) {

super(entityInformation, mongoOperations);
Expand All @@ -93,11 +97,15 @@ public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation,
* @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findById(com.querydsl.core.types.Predicate)
*/
@Override
public T findOne(Predicate predicate) {
public Optional<T> findOne(Predicate predicate) {

Assert.notNull(predicate, "Predicate must not be null!");

return createQueryFor(predicate).fetchOne();
try {
return Optional.ofNullable(createQueryFor(predicate).fetchOne());
} catch (NonUniqueResultException ex) {
throw new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
}
}

/*
Expand Down Expand Up @@ -219,7 +227,7 @@ public boolean exists(Predicate predicate) {

/**
* Creates a {@link MongodbQuery} for the given {@link Predicate}.
*
*
* @param predicate
* @return
*/
Expand All @@ -238,7 +246,7 @@ private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> createQuery() {

/**
* Applies the given {@link Pageable} to the given {@link MongodbQuery}.
*
*
* @param query
* @param pageable
* @return
Expand All @@ -252,7 +260,7 @@ private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> applyPagination(

/**
* Applies the given {@link Sort} to the given {@link MongodbQuery}.
*
*
* @param query
* @param sort
* @return
Expand All @@ -276,7 +284,7 @@ private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> applySorting(

/**
* Transforms a plain {@link Order} into a QueryDsl specific {@link OrderSpecifier}.
*
*
* @param order
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ public void shouldExecuteFindOnDbRefCorrectly() {
dave.setCreator(user);
operations.save(dave);

assertThat(repository.findOne(QPerson.person.creator.eq(user)), is(dave));
assertThat(repository.findOne(QPerson.person.creator.eq(user)).get(), is(dave));
}

@Test // DATAMONGO-969
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/
package org.springframework.data.mongodb.repository.support;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;

import java.util.Arrays;
import java.util.List;
Expand All @@ -25,6 +24,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoOperations;
Expand All @@ -35,18 +35,19 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* Integration test for {@link QueryDslMongoRepository}.
* Integration test for {@link QuerydslMongoRepository}.
*
* @author Thomas Darimont
* @author Mark Paluch
* @author Christoph Strobl
*/
@ContextConfiguration(
locations = "/org/springframework/data/mongodb/repository/PersonRepositoryIntegrationTests-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class QueryDslMongoRepositoryIntegrationTests {

@Autowired MongoOperations operations;
QueryDslMongoRepository<Person, String> repository;
QuerydslMongoRepository<Person, String> repository;

Person dave, oliver, carter;
QPerson person;
Expand All @@ -56,7 +57,7 @@ public void setup() {

MongoRepositoryFactory factory = new MongoRepositoryFactory(operations);
MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
repository = new QueryDslMongoRepository<Person, String>(entityInformation, operations);
repository = new QuerydslMongoRepository<>(entityInformation, operations);

operations.dropCollection(Person.class);

Expand All @@ -72,18 +73,30 @@ public void setup() {
@Test // DATAMONGO-1146
public void shouldSupportExistsWithPredicate() throws Exception {

assertThat(repository.exists(person.firstname.eq("Dave")), is(true));
assertThat(repository.exists(person.firstname.eq("Unknown")), is(false));
assertThat(repository.exists(person.firstname.eq("Dave"))).isTrue();
assertThat(repository.exists(person.firstname.eq("Unknown"))).isFalse();
}

@Test // DATAMONGO-1167
public void shouldSupportFindAllWithPredicateAndSort() {

List<Person> users = repository.findAll(person.lastname.isNotNull(), Sort.by(Direction.ASC, "firstname"));

assertThat(users, hasSize(3));
assertThat(users.get(0).getFirstname(), is(carter.getFirstname()));
assertThat(users.get(2).getFirstname(), is(oliver.getFirstname()));
assertThat(users, hasItems(carter, dave, oliver));
assertThat(users).containsExactly(carter, dave, oliver);
}

@Test // DATAMONGO-1690
public void findOneWithPredicateReturnsResultCorrectly() {
assertThat(repository.findOne(person.firstname.eq(dave.getFirstname()))).contains(dave);
}

@Test // DATAMONGO-1690
public void findOneWithPredicateReturnsOptionalEmptyWhenNoDataFound() {
assertThat(repository.findOne(person.firstname.eq("batman"))).isNotPresent();
}

@Test(expected = IncorrectResultSizeDataAccessException.class) // DATAMONGO-1690
public void findOneWithPredicateThrowsExceptionForNonUniqueResults() {
repository.findOne(person.firstname.contains("e"));
}
}