Skip to content

Commit 898489f

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1690 - Adapt to QuerydslPredicateExecutor API changes.
We now return Optional<T> for QuerydslPredicateExecutor.findOne(Predicate). Original pull request #461. Related ticket: DATACMNS-1059.
1 parent 3575d54 commit 898489f

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QueryDslMongoRepository.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
import java.io.Serializable;
1919
import java.util.List;
20+
import java.util.Optional;
2021

22+
import org.springframework.dao.IncorrectResultSizeDataAccessException;
2123
import org.springframework.data.domain.Page;
2224
import org.springframework.data.domain.Pageable;
2325
import org.springframework.data.domain.Sort;
@@ -34,6 +36,7 @@
3436
import org.springframework.data.repository.support.PageableExecutionUtils;
3537
import org.springframework.util.Assert;
3638

39+
import com.querydsl.core.NonUniqueResultException;
3740
import com.querydsl.core.types.EntityPath;
3841
import com.querydsl.core.types.Expression;
3942
import com.querydsl.core.types.OrderSpecifier;
@@ -47,6 +50,7 @@
4750
* @author Oliver Gierke
4851
* @author Thomas Darimont
4952
* @author Mark Paluch
53+
* @author Christoph Strobl
5054
*/
5155
public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleMongoRepository<T, ID>
5256
implements QuerydslPredicateExecutor<T> {
@@ -93,11 +97,15 @@ public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation,
9397
* @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findById(com.querydsl.core.types.Predicate)
9498
*/
9599
@Override
96-
public T findOne(Predicate predicate) {
100+
public Optional<T> findOne(Predicate predicate) {
97101

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

100-
return createQueryFor(predicate).fetchOne();
104+
try {
105+
return Optional.ofNullable(createQueryFor(predicate).fetchOne());
106+
} catch (NonUniqueResultException ex) {
107+
throw new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
108+
}
101109
}
102110

103111
/*

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ public void shouldExecuteFindOnDbRefCorrectly() {
917917
dave.setCreator(user);
918918
operations.save(dave);
919919

920-
assertThat(repository.findOne(QPerson.person.creator.eq(user)), is(dave));
920+
assertThat(repository.findOne(QPerson.person.creator.eq(user)).get(), is(dave));
921921
}
922922

923923
@Test // DATAMONGO-969

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/QueryDslMongoRepositoryIntegrationTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import java.util.Arrays;
2222
import java.util.List;
2323

24+
import org.assertj.core.api.Assertions;
2425
import org.junit.Before;
2526
import org.junit.Test;
2627
import org.junit.runner.RunWith;
2728
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.dao.IncorrectResultSizeDataAccessException;
2830
import org.springframework.data.domain.Sort;
2931
import org.springframework.data.domain.Sort.Direction;
3032
import org.springframework.data.mongodb.core.MongoOperations;
@@ -39,6 +41,7 @@
3941
*
4042
* @author Thomas Darimont
4143
* @author Mark Paluch
44+
* @author Christoph Strobl
4245
*/
4346
@ContextConfiguration(
4447
locations = "/org/springframework/data/mongodb/repository/PersonRepositoryIntegrationTests-context.xml")
@@ -86,4 +89,19 @@ public void shouldSupportFindAllWithPredicateAndSort() {
8689
assertThat(users.get(2).getFirstname(), is(oliver.getFirstname()));
8790
assertThat(users, hasItems(carter, dave, oliver));
8891
}
92+
93+
@Test // DATAMONGO-1690
94+
public void findOneWithPredicateReturnsResultCorrectly() {
95+
Assertions.assertThat(repository.findOne(person.firstname.eq(dave.getFirstname()))).contains(dave);
96+
}
97+
98+
@Test // DATAMONGO-1690
99+
public void findOneWithPredicateReturnsOptionalEmptyWhenNoDataFound() {
100+
Assertions.assertThat(repository.findOne(person.firstname.eq("batman"))).isNotPresent();
101+
}
102+
103+
@Test(expected = IncorrectResultSizeDataAccessException.class) // DATAMONGO-1690
104+
public void findOneWithPredicateThrowsExceptionForNonUniqueResults() {
105+
repository.findOne(person.firstname.contains("e"));
106+
}
89107
}

0 commit comments

Comments
 (0)