Skip to content

Commit be980d9

Browse files
RobertHeimmp911de
authored andcommitted
Do not override existing limit in R2dbcEntityTemplate.selectOne.
Closes #758
1 parent f4e3e2a commit be980d9

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
@@ -67,6 +67,7 @@
6767
*
6868
* @author Mark Paluch
6969
* @author Jose Luis Leon
70+
* @author Robert Heim
7071
*/
7172
public class R2dbcEntityTemplateUnitTests {
7273

@@ -200,6 +201,23 @@ void shouldSelectOne() {
200201
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
201202
}
202203

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

0 commit comments

Comments
 (0)