Browse Source

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.
pull/151/merge
Oliver Gierke 11 years ago
parent
commit
7796165e2b
  1. 2
      src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java
  2. 13
      src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java
  3. 13
      src/test/java/org/springframework/data/jpa/repository/query/JpaQueryMethodUnitTests.java

2
src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java

@ -96,7 +96,7 @@ public class JpaQueryMethod extends QueryMethod { @@ -96,7 +96,7 @@ public class JpaQueryMethod extends QueryMethod {
String annotatedQuery = getAnnotatedQuery();
if (!StringUtils.hasText(annotatedQuery)) {
if (!QueryUtils.hasNamedParameter(annotatedQuery)) {
return;
}

13
src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java

@ -89,6 +89,9 @@ public abstract class QueryUtils { @@ -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 { @@ -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.
*

13
src/test/java/org/springframework/data/jpa/repository/query/JpaQueryMethodUnitTests.java

@ -358,6 +358,16 @@ public class JpaQueryMethodUnitTests { @@ -358,6 +358,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.
*
@ -418,6 +428,9 @@ public class JpaQueryMethodUnitTests { @@ -418,6 +428,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> {

Loading…
Cancel
Save