DATAJPA-513 - Improve error message on missing @Param on query method parameter.
StringQuery now hints to the usage of @Param on query method parameters if named parameters are used and parameter names were not declared. Original pull request: #77.
This commit is contained in:
committed by
Oliver Gierke
parent
ee257548ff
commit
6cc838a3f8
@@ -36,9 +36,12 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Oliver Wehrens
|
||||
*/
|
||||
class StringQuery {
|
||||
|
||||
private static final String PARAMETER_NAME_MISSING = "Name for parameter binding must not be null or empty! For named parameters you need to use @Param for query method parameters on Java versions < 8.";
|
||||
|
||||
private final String query;
|
||||
private final List<ParameterBinding> bindings;
|
||||
private final String alias;
|
||||
@@ -102,7 +105,7 @@ class StringQuery {
|
||||
*/
|
||||
public ParameterBinding getBindingFor(String name) {
|
||||
|
||||
Assert.hasText(name, "Name must not be null or empty!");
|
||||
Assert.hasText(name, PARAMETER_NAME_MISSING);
|
||||
|
||||
for (ParameterBinding binding : bindings) {
|
||||
if (binding.hasName(name)) {
|
||||
|
||||
@@ -20,10 +20,13 @@ import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.data.jpa.repository.query.StringQuery.InParameterBinding;
|
||||
import org.springframework.data.jpa.repository.query.StringQuery.LikeParameterBinding;
|
||||
import org.springframework.data.jpa.repository.query.StringQuery.ParameterBinding;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.repository.query.parser.Part.Type;
|
||||
|
||||
/**
|
||||
@@ -34,6 +37,8 @@ import org.springframework.data.repository.query.parser.Part.Type;
|
||||
*/
|
||||
public class StringQueryUnitTests {
|
||||
|
||||
public @Rule ExpectedException exception = ExpectedException.none();
|
||||
|
||||
/**
|
||||
* @see DATAJPA-341
|
||||
*/
|
||||
@@ -227,6 +232,20 @@ public class StringQueryUnitTests {
|
||||
assertNamedBinding(InParameterBinding.class, "statuses", bindings.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-513
|
||||
*/
|
||||
@Test
|
||||
public void rejectsNullParameterNameHintingTowardsAtParamForNullParameterName() {
|
||||
|
||||
StringQuery query = new StringQuery("select x from X");
|
||||
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
exception.expectMessage(Param.class.getSimpleName());
|
||||
|
||||
query.getBindingFor(null);
|
||||
}
|
||||
|
||||
private void assertPositionalBinding(Class<? extends ParameterBinding> bindingType, Integer position,
|
||||
ParameterBinding expectedBinding) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user