DATAJPA-629 - Allow SpEL template expressions in combination with parameter expressions in @Query.

Previously SpEL template expressions like #{#entityName} could not be used in conjunction with parameter expressions in @Query because the SpEL template parser tried to evaluate the dynamic parameter expressions as well. We now mask the parameters prior to evaluating the SpEL template expression.

Original pull request: #122.
This commit is contained in:
Thomas Darimont
2014-11-14 12:44:05 +01:00
committed by Oliver Gierke
parent fa5a7ace50
commit 36a6f958f2
3 changed files with 49 additions and 4 deletions

View File

@@ -1753,6 +1753,20 @@ public class UserRepositoryTests {
assertThat(users.getContent().get(1), is(fourthUser));
}
/**
* @see DATAJPA-629
*/
@Test
public void shouldfindUsersBySpELExpressionParametersWithSpelTemplateExpression() {
flushTestUsers();
List<User> users = repository.findUsersByFirstnameForSpELExpressionWithParameterIndexOnlyWithEntityExpression(
"Joachim", "Arrasz");
assertThat(users, hasSize(1));
assertThat(users.get(0), is(secondUser));
}
private Page<User> executeSpecWithSort(Sort sort) {
flushTestUsers();

View File

@@ -459,7 +459,7 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
*/
@Query("select u from User u where u.emailAddress = ?1")
Optional<User> findOptionalByEmailAddress(String emailAddress);
/**
* @see DATAJPA-564
*/
@@ -527,4 +527,11 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
value = "select * from (select rownum() as RN, u.* from User u) where RN between ?#{ #pageable.offset -1} and ?#{#pageable.offset + #pageable.pageSize}",
countQuery = "select count(u.id) from User u", nativeQuery = true)
Page<User> findUsersInNativeQueryWithPagination(Pageable pageable);
/**
* @see DATAJPA-629
*/
@Query("select u from #{#entityName} u where u.firstname = ?#{[0]} and u.lastname = ?#{[1]}")
List<User> findUsersByFirstnameForSpELExpressionWithParameterIndexOnlyWithEntityExpression(String firstname,
String lastname);
}