DATAJPA-564 - Allow SpEL expressions to be the only consumer of query method parameters.

We now allow the parameters of query methods to also be exclusively consumed by expressions within a given query string. Previously this wasn't possible due to to strict checks for parameter usage in JpaQueryMethod.
Relax check for query param usage in JpaQueryMethod.
This commit is contained in:
Thomas Darimont
2014-07-07 16:17:27 +02:00
committed by Oliver Gierke
parent 841bb22906
commit 55f1131b2f
6 changed files with 85 additions and 6 deletions

View File

@@ -58,8 +58,8 @@ import org.springframework.data.jpa.domain.sample.Address;
import org.springframework.data.jpa.domain.sample.Role;
import org.springframework.data.jpa.domain.sample.SpecialUser;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.repository.sample.UserRepository;
import org.springframework.data.jpa.repository.sample.SampleSecurity.SampleSecurityContextHolder;
import org.springframework.data.jpa.repository.sample.UserRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@@ -1706,6 +1706,32 @@ public class UserRepositoryTests {
assertThat(users.get(0), is(secondUser));
}
/**
* @see DATAJPA-564
*/
@Test
public void shouldfindUsersByFirstnameForSpELExpressionOnlyWithParameterNameVariableReference() {
flushTestUsers();
List<User> users = repository.findUsersByFirstnameForSpELExpressionWithParameterVariableOnly("Joachim");
assertThat(users, hasSize(1));
assertThat(users.get(0), is(secondUser));
}
/**
* @see DATAJPA-564
*/
@Test
public void shouldfindUsersByFirstnameForSpELExpressionOnlyWithParameterIndexReference() {
flushTestUsers();
List<User> users = repository.findUsersByFirstnameForSpELExpressionWithParameterIndexOnly("Joachim");
assertThat(users, hasSize(1));
assertThat(users.get(0), is(secondUser));
}
private Page<User> executeSpecWithSort(Sort sort) {
flushTestUsers();

View File

@@ -507,4 +507,16 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
*/
@Query("select u from User u where u.firstname = ?1 and u.firstname=?#{[0]} and u.emailAddress = ?#{principal.emailAddress}")
List<User> findByFirstnameAndCurrentUserWithCustomQuery(String firstname);
/**
* @see DATAJPA-564
*/
@Query("select u from User u where u.firstname = :#{#firstname}")
List<User> findUsersByFirstnameForSpELExpressionWithParameterVariableOnly(@Param("firstname") String firstname);
/**
* @see DATAJPA-564
*/
@Query("select u from User u where u.firstname = ?#{[0]}")
List<User> findUsersByFirstnameForSpELExpressionWithParameterIndexOnly(String firstname);
}