Skip to content

Commit 6583fe0

Browse files
RobertHeimmp911de
authored andcommitted
Do not override existing limit in R2dbcEntityTemplate.selectOne.
Closes #758
1 parent 386e04b commit 6583fe0

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/main/java/org/springframework/data/r2dbc/core/R2dbcEntityTemplate.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
* @author Bogdan Ilchyshyn
9292
* @author Jens Schauder
9393
* @author Jose Luis Leon
94+
* @author Robert Heim
9495
* @since 1.1
9596
*/
9697
public class R2dbcEntityTemplate implements R2dbcEntityOperations, BeanFactoryAware, ApplicationContextAware {
@@ -448,7 +449,8 @@ private <T> RowsFetchSpec<T> doSelect(Query query, Class<?> entityClass, SqlIden
448449
*/
449450
@Override
450451
public <T> Mono<T> selectOne(Query query, Class<T> entityClass) throws DataAccessException {
451-
return doSelect(query.limit(2), entityClass, getTableName(entityClass), entityClass, RowsFetchSpec::one);
452+
return doSelect(query.getLimit() != -1 ? query : query.limit(2), entityClass, getTableName(entityClass),
453+
entityClass, RowsFetchSpec::one);
452454
}
453455

454456
/*
@@ -641,11 +643,9 @@ private <T> Mono<T> doInsert(T entity, SqlIdentifier tableName, OutboundRow outb
641643
}
642644

643645
return statement.returnGeneratedValues(dataAccessStrategy.renderForGeneratedValues(identifierColumns.get(0)));
644-
})
645-
.map(this.dataAccessStrategy.getConverter().populateIdIfNecessary(entity)) //
646+
}).map(this.dataAccessStrategy.getConverter().populateIdIfNecessary(entity)) //
646647
.all() //
647-
.last(entity)
648-
.flatMap(saved -> maybeCallAfterSave(saved, outboundRow, tableName));
648+
.last(entity).flatMap(saved -> maybeCallAfterSave(saved, outboundRow, tableName));
649649
}
650650

651651
@SuppressWarnings("unchecked")

src/test/java/org/springframework/data/r2dbc/core/R2dbcEntityTemplateUnitTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
*
6969
* @author Mark Paluch
7070
* @author Jose Luis Leon
71+
* @author Robert Heim
7172
*/
7273
public class R2dbcEntityTemplateUnitTests {
7374

@@ -202,6 +203,23 @@ void shouldSelectOne() {
202203
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
203204
}
204205

206+
@Test // gh-758
207+
void shouldSelectOneDoNotOverrideExistingLimit() {
208+
209+
recorder.addStubbing(s -> s.startsWith("SELECT"), Collections.emptyList());
210+
211+
entityTemplate
212+
.selectOne(Query.query(Criteria.where("name").is("Walter")).sort(Sort.by("name")).limit(1), Person.class) //
213+
.as(StepVerifier::create) //
214+
.verifyComplete();
215+
216+
StatementRecorder.RecordedStatement statement = recorder.getCreatedStatement(s -> s.startsWith("SELECT"));
217+
218+
assertThat(statement.getSql())
219+
.isEqualTo("SELECT person.* FROM person WHERE person.THE_NAME = $1 ORDER BY person.THE_NAME ASC LIMIT 1");
220+
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
221+
}
222+
205223
@Test // gh-220
206224
void shouldUpdateByQuery() {
207225

0 commit comments

Comments
 (0)