Reject Limit parameter using String-based queries.
Document limitations, add test cases for derived queries. Closes #1654
This commit is contained in:
@@ -117,6 +117,11 @@ public class StringBasedJdbcQuery extends AbstractJdbcQuery {
|
||||
throw new UnsupportedOperationException(
|
||||
"Page queries are not supported using string-based queries; Offending method: " + queryMethod);
|
||||
}
|
||||
|
||||
if (queryMethod.getParameters().hasLimitParameter()) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Queries with Limit are not supported using string-based queries; Offending method: " + queryMethod);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -52,6 +52,7 @@ import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.ExampleMatcher;
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -491,6 +492,18 @@ public class JdbcRepositoryIntegrationTests {
|
||||
assertThat(repository.findPageByNameContains("a", PageRequest.of(1, 2)).getContent()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test // GH-1654
|
||||
public void selectWithLimitShouldReturnCorrectResult() {
|
||||
|
||||
repository.saveAll(Arrays.asList(new DummyEntity("a1"), new DummyEntity("a2"), new DummyEntity("a3")));
|
||||
|
||||
List<DummyEntity> page = repository.findByNameContains("a", Limit.of(3));
|
||||
assertThat(page).hasSize(3);
|
||||
|
||||
assertThat(repository.findByNameContains("a", Limit.of(2))).hasSize(2);
|
||||
assertThat(repository.findByNameContains("a", Limit.unlimited())).hasSize(3);
|
||||
}
|
||||
|
||||
@Test // GH-774
|
||||
public void sliceByNameShouldReturnCorrectResult() {
|
||||
|
||||
@@ -1385,6 +1398,8 @@ public class JdbcRepositoryIntegrationTests {
|
||||
|
||||
Page<DummyEntity> findPageByNameContains(String name, Pageable pageable);
|
||||
|
||||
List<DummyEntity> findByNameContains(String name, Limit limit);
|
||||
|
||||
Page<DummyProjection> findPageProjectionByName(String name, Pageable pageable);
|
||||
|
||||
Slice<DummyEntity> findSliceByNameContains(String name, Pageable pageable);
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.convert.ReadingConverter;
|
||||
import org.springframework.data.convert.WritingConverter;
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Slice;
|
||||
@@ -51,6 +52,7 @@ import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
|
||||
import org.springframework.data.repository.query.ExtensionAwareQueryMethodEvaluationContextProvider;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
|
||||
import org.springframework.data.spel.spi.EvaluationContextExtension;
|
||||
import org.springframework.jdbc.core.ResultSetExtractor;
|
||||
@@ -191,6 +193,16 @@ class StringBasedJdbcQueryUnitTests {
|
||||
.hasMessageContaining("Page queries are not supported using string-based queries");
|
||||
}
|
||||
|
||||
@Test // GH-1654
|
||||
void limitNotSupported() {
|
||||
|
||||
JdbcQueryMethod queryMethod = createMethod("unsupportedLimitQuery", String.class, Limit.class);
|
||||
|
||||
assertThatThrownBy(
|
||||
() -> new StringBasedJdbcQuery(queryMethod, operations, defaultRowMapper, converter, evaluationContextProvider))
|
||||
.isInstanceOf(UnsupportedOperationException.class);
|
||||
}
|
||||
|
||||
@Test // GH-1212
|
||||
void convertsEnumCollectionParameterIntoStringCollectionParameter() {
|
||||
|
||||
@@ -230,7 +242,6 @@ class StringBasedJdbcQueryUnitTests {
|
||||
.extractParameterSource();
|
||||
|
||||
assertThat(sqlParameterSource.getValue("value")).isEqualTo("one");
|
||||
|
||||
}
|
||||
|
||||
QueryFixture forMethod(String name, Class... paramTypes) {
|
||||
@@ -337,6 +348,9 @@ class StringBasedJdbcQueryUnitTests {
|
||||
|
||||
@Query("SELECT * FROM table WHERE c = :#{myext.testValue} AND c2 = :#{myext.doSomething()}")
|
||||
Object findBySpelExpression(Object object);
|
||||
|
||||
@Query("SELECT * FROM person WHERE lastname = $1")
|
||||
Object unsupportedLimitQuery(@Param("lastname") String lastname, Limit limit);
|
||||
}
|
||||
|
||||
@Test // GH-619
|
||||
|
||||
Reference in New Issue
Block a user