DATAMONGO-1619 - Polishing.
Align to changes in DATACMNS-995 and emit Exception if findOne yields more than one result. Original Pull Request: #444
This commit is contained in:
@@ -27,6 +27,7 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
|
||||
@@ -43,6 +44,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@@ -86,7 +88,16 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
|
||||
Assert.notNull(example, "Sample must not be null!");
|
||||
|
||||
Query q = new Query(new Criteria().alike(example));
|
||||
return mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName());
|
||||
q.limit(2);
|
||||
|
||||
return mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName()).buffer(2)
|
||||
.flatMap(vals -> {
|
||||
|
||||
if (vals.size() > 1) {
|
||||
return Mono.error(new IncorrectResultSizeDataAccessException(1));
|
||||
}
|
||||
return Mono.just(vals.iterator().next());
|
||||
}).single();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -49,9 +50,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Test for {@link ReactiveMongoRepository}.
|
||||
* Tests for {@link ReactiveMongoRepository}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:reactive-infrastructure.xml")
|
||||
@@ -403,6 +405,15 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
|
||||
StepVerifier.create(repository.exists(example)).expectNext(false).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1619
|
||||
public void findOneShouldEmitIncorrectResultSizeDataAccessExceptionWhenMoreThanOneElementFound() {
|
||||
|
||||
Example<ReactivePerson> example = Example.of(new ReactivePerson(null, "Matthews", -1),
|
||||
matching().withIgnorePaths("age"));
|
||||
|
||||
StepVerifier.create(repository.findOne(example)).expectError(IncorrectResultSizeDataAccessException.class);
|
||||
}
|
||||
|
||||
interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> {
|
||||
|
||||
Flux<ReactivePerson> findByLastname(String lastname);
|
||||
|
||||
Reference in New Issue
Block a user