Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
* @author Bogdan Ilchyshyn
* @author Jens Schauder
* @author Jose Luis Leon
* @author Robert Heim
* @since 1.1
*/
public class R2dbcEntityTemplate implements R2dbcEntityOperations, BeanFactoryAware, ApplicationContextAware {
Expand Down Expand Up @@ -420,7 +421,13 @@ private <T> RowsFetchSpec<T> doSelect(Query query, Class<?> entityClass, SqlIden
*/
@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);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
*
* @author Mark Paluch
* @author Jose Luis Leon
* @author Robert Heim
*/
public class R2dbcEntityTemplateUnitTests {

Expand Down Expand Up @@ -202,6 +203,22 @@ void shouldSelectOne() {
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() {

Expand Down