DATAMONGO-1685 - Adapt to QueryByExampleExecutor API changes.
Use Optional as return type for findOne(Example example). Related ticket: DATACMNS-1058. Original pull request: #460.
This commit is contained in:
committed by
Mark Paluch
parent
96fbe49cdb
commit
af85b46e7d
@@ -312,12 +312,13 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
|
||||
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findOne(org.springframework.data.domain.Example)
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> S findOne(Example<S> example) {
|
||||
public <S extends T> Optional<S> findOne(Example<S> example) {
|
||||
|
||||
Assert.notNull(example, "Sample must not be null!");
|
||||
|
||||
Query q = new Query(new Criteria().alike(example));
|
||||
return mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName());
|
||||
return Optional
|
||||
.ofNullable(mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName()));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("config/MongoNamespaceIntegrationTests-context.xml")
|
||||
@@ -57,6 +58,6 @@ public class ContactRepositoryIntegrationTests {
|
||||
|
||||
Person person = repository.save(new Person("Oliver", "Gierke"));
|
||||
|
||||
assertThat(repository.findOne(Example.of(person)), instanceOf(Person.class));
|
||||
assertThat(repository.findOne(Example.of(person)).get(), instanceOf(Person.class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -376,9 +377,9 @@ public class SimpleMongoRepositoryTests {
|
||||
sample.setLastname("Matthews");
|
||||
trimDomainType(sample, "id", "createdAt", "email");
|
||||
|
||||
Person result = repository.findOne(Example.of(sample));
|
||||
Optional<Person> result = repository.findOne(Example.of(sample));
|
||||
|
||||
assertThat(result, is(equalTo(dave)));
|
||||
Assertions.assertThat(result).isPresent().contains(dave);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1245
|
||||
|
||||
Reference in New Issue
Block a user