Skip to content

Commit

Permalink
DATAJPA-758 - JpaQueryMethod now only checks parameter names if query…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
odrotbohm committed Jul 10, 2015
1 parent 3a56e00 commit e8e8e1b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void assertParameterNamesInAnnotatedQuery() {

String annotatedQuery = getAnnotatedQuery();

if (!StringUtils.hasText(annotatedQuery)) {
if (!QueryUtils.hasNamedParameter(annotatedQuery)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -390,6 +393,16 @@ public static boolean hasNamedParameter(Query query) {
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,16 @@ public void shouldFindEntityGraphAnnotationOnQueryMethodGetOneByWithDerivedName(
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.
*
Expand Down Expand Up @@ -443,6 +453,9 @@ static interface ValidRepository {
*/
@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> {
Expand Down

0 comments on commit e8e8e1b

Please sign in to comment.