DATAMONGO-1690 - Adapt to QuerydslPredicateExecutor API changes.

We now return Optional<T> for QuerydslPredicateExecutor.findOne(Predicate).

Original pull request #461.
Related ticket: DATACMNS-1059.
This commit is contained in:
Christoph Strobl
2017-05-11 12:04:10 +02:00
committed by Oliver Gierke
parent 3575d5461e
commit 898489fecf
3 changed files with 29 additions and 3 deletions

View File

@@ -17,7 +17,9 @@ package org.springframework.data.mongodb.repository.support;
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;
@@ -34,6 +36,7 @@ import org.springframework.data.repository.core.EntityMetadata;
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;
@@ -47,6 +50,7 @@ import com.querydsl.mongodb.AbstractMongodbQuery;
* @author Oliver Gierke
* @author Thomas Darimont
* @author Mark Paluch
* @author Christoph Strobl
*/
public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleMongoRepository<T, ID>
implements QuerydslPredicateExecutor<T> {
@@ -93,11 +97,15 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
* @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);
}
}
/*

View File

@@ -917,7 +917,7 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
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

View File

@@ -21,10 +21,12 @@ import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.junit.Before;
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;
@@ -39,6 +41,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*
* @author Thomas Darimont
* @author Mark Paluch
* @author Christoph Strobl
*/
@ContextConfiguration(
locations = "/org/springframework/data/mongodb/repository/PersonRepositoryIntegrationTests-context.xml")
@@ -86,4 +89,19 @@ public class QueryDslMongoRepositoryIntegrationTests {
assertThat(users.get(2).getFirstname(), is(oliver.getFirstname()));
assertThat(users, hasItems(carter, dave, oliver));
}
@Test // DATAMONGO-1690
public void findOneWithPredicateReturnsResultCorrectly() {
Assertions.assertThat(repository.findOne(person.firstname.eq(dave.getFirstname()))).contains(dave);
}
@Test // DATAMONGO-1690
public void findOneWithPredicateReturnsOptionalEmptyWhenNoDataFound() {
Assertions.assertThat(repository.findOne(person.firstname.eq("batman"))).isNotPresent();
}
@Test(expected = IncorrectResultSizeDataAccessException.class) // DATAMONGO-1690
public void findOneWithPredicateThrowsExceptionForNonUniqueResults() {
repository.findOne(person.firstname.contains("e"));
}
}