Do not override existing limit in R2dbcEntityTemplate.selectOne.
Closes #1233
This commit is contained in:
@@ -87,6 +87,7 @@ import org.springframework.util.Assert;
|
||||
* @author Bogdan Ilchyshyn
|
||||
* @author Jens Schauder
|
||||
* @author Jose Luis Leon
|
||||
* @author Robert Heim
|
||||
* @since 1.1
|
||||
*/
|
||||
public class R2dbcEntityTemplate implements R2dbcEntityOperations, BeanFactoryAware, ApplicationContextAware {
|
||||
@@ -420,7 +421,13 @@ public class R2dbcEntityTemplate implements R2dbcEntityOperations, BeanFactoryAw
|
||||
*/
|
||||
@Override
|
||||
public <T> Mono<T> selectOne(Query query, Class<T> entityClass) throws DataAccessException {
|
||||
return doSelect(query.limit(2), entityClass, getTableName(entityClass), entityClass, RowsFetchSpec::one);
|
||||
Query q = query;
|
||||
/* If the query has not a defined limit, a limit of 2 is employed
|
||||
to catch cases where the query would yield more than one result. */
|
||||
if (query.getLimit() == -1) {
|
||||
q = query.limit(2);
|
||||
} // else: use the already defined limit.
|
||||
return doSelect(q, entityClass, getTableName(entityClass), entityClass, RowsFetchSpec::one);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -68,6 +68,7 @@ import org.springframework.util.CollectionUtils;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Jose Luis Leon
|
||||
* @author Robert Heim
|
||||
*/
|
||||
public class R2dbcEntityTemplateUnitTests {
|
||||
|
||||
@@ -202,6 +203,22 @@ public class R2dbcEntityTemplateUnitTests {
|
||||
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
|
||||
}
|
||||
|
||||
@Test // gh-220, gh-758
|
||||
void shouldSelectOneDoNotOverrideExistingLimit() {
|
||||
|
||||
recorder.addStubbing(s -> s.startsWith("SELECT"), Collections.emptyList());
|
||||
|
||||
entityTemplate.selectOne(Query.query(Criteria.where("name").is("Walter")).sort(Sort.by("name")).limit(1), Person.class) //
|
||||
.as(StepVerifier::create) //
|
||||
.verifyComplete();
|
||||
|
||||
StatementRecorder.RecordedStatement statement = recorder.getCreatedStatement(s -> s.startsWith("SELECT"));
|
||||
|
||||
assertThat(statement.getSql())
|
||||
.isEqualTo("SELECT person.* FROM person WHERE person.THE_NAME = $1 ORDER BY person.THE_NAME ASC LIMIT 1");
|
||||
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
|
||||
}
|
||||
|
||||
@Test // gh-220
|
||||
void shouldUpdateByQuery() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user