Browse Source

DATAMONGO-1690 - Adapt to QuerydslPredicateExecutor API changes.

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

Original pull request #461.
Related ticket: DATACMNS-1059.
pull/444/head
Christoph Strobl 9 years ago committed by Oliver Gierke
parent
commit
898489fecf
  1. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QueryDslMongoRepository.java
  2. 2
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java
  3. 18
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/QueryDslMongoRepositoryIntegrationTests.java

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

@ -17,7 +17,9 @@ package org.springframework.data.mongodb.repository.support; @@ -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; @@ -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; @@ -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 @@ -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);
}
}
/*

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

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

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

@ -21,10 +21,12 @@ import static org.junit.Assert.*; @@ -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; @@ -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 { @@ -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"));
}
}

Loading…
Cancel
Save