DATAJPA-758 - JpaQueryMethod now only checks parameter names if query uses named parameters.
We now only validate parameter names if the declared query actually uses named parameters. This is mainly needed in context of an (accidentally) mixed use of named parameters with indexed placeholders which can occur if compiling with parameters is activated on Java 8 (i.e. using -parameters) but indexed parameters are used in the query definition.
This commit is contained in:
@@ -96,7 +96,7 @@ public class JpaQueryMethod extends QueryMethod {
|
||||
|
||||
String annotatedQuery = getAnnotatedQuery();
|
||||
|
||||
if (!StringUtils.hasText(annotatedQuery)) {
|
||||
if (!QueryUtils.hasNamedParameter(annotatedQuery)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,9 @@ public abstract class QueryUtils {
|
||||
private static final String EQUALS_CONDITION_STRING = "%s.%s = :%s";
|
||||
private static final Pattern ORDER_BY = Pattern.compile(".*order\\s+by\\s+.*", CASE_INSENSITIVE);
|
||||
|
||||
private static final Pattern NAMED_PARAMETER = Pattern.compile(":" + IDENTIFIER + "|\\#" + IDENTIFIER,
|
||||
CASE_INSENSITIVE);
|
||||
|
||||
private static final Map<PersistentAttributeType, Class<? extends Annotation>> ASSOCIATION_TYPES;
|
||||
|
||||
private static final int QUERY_JOIN_ALIAS_GROUP_INDEX = 2;
|
||||
@@ -390,6 +393,16 @@ public abstract class QueryUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given query contains named parameters.
|
||||
*
|
||||
* @param query can be {@literal null} or empty.
|
||||
* @return
|
||||
*/
|
||||
public static boolean hasNamedParameter(String query) {
|
||||
return StringUtils.hasText(query) && NAMED_PARAMETER.matcher(query).find();
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns the given {@link Sort} into {@link javax.persistence.criteria.Order}s.
|
||||
*
|
||||
|
||||
@@ -383,6 +383,16 @@ public class JpaQueryMethodUnitTests {
|
||||
assertThat(method.getEntityGraph().getType(), is(EntityGraphType.FETCH));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-758
|
||||
*/
|
||||
@Test
|
||||
public void allowsPositionalBindingEvenIfParametersAreNamed() throws Exception {
|
||||
|
||||
new JpaQueryMethod(ValidRepository.class.getMethod("queryWithPositionalBinding", String.class), metadata,
|
||||
extractor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface to define invalid repository methods for testing.
|
||||
*
|
||||
@@ -443,6 +453,9 @@ public class JpaQueryMethodUnitTests {
|
||||
*/
|
||||
@EntityGraph(value = "User.propertyLoadPath", type = EntityGraphType.LOAD)
|
||||
User queryMethodWithCustomEntityFetchGraph(Integer id);
|
||||
|
||||
@Query("select u from User u where u.firstname = ?1")
|
||||
User queryWithPositionalBinding(@Param("firstname") String firstname);
|
||||
}
|
||||
|
||||
static interface JpaRepositoryOverride extends JpaRepository<User, Long> {
|
||||
|
||||
Reference in New Issue
Block a user