7

I have a following repository:

public interface MilestoneRepository extends JpaRepository<Milestone,Date> {
    @Query("select m from Milestone m where m.date <= :date order by m.date desc")
    Page<Milestone> findLeftClosest(@Param("date") Date date, Pageable pageable);
}

Defined like above works ok, but if I switch the arguments order:

public interface MilestoneRepository extends JpaRepository<Milestone,Date> {
    @Query("select m from Milestone m where m.date <= :date order by m.date desc")
    Page<Milestone> findLeftClosest(Pageable pageable, @Param("date") Date date);
}

I get a following exception on context initialization (posting the most interesting bottom cause):

Caused by: java.lang.IllegalArgumentException: Either use @Param on all parameters except Pageable and Sort typed once, or none at all!
    at org.springframework.util.Assert.isTrue(Assert.java:65)
    at org.springframework.data.repository.query.Parameters.assertEitherAllParamAnnotatedOrNone(Parameters.java:265)
    at org.springframework.data.repository.query.Parameters.<init>(Parameters.java:85)
    at org.springframework.data.jpa.repository.query.JpaParameters.<init>(JpaParameters.java:43)
    at org.springframework.data.jpa.repository.query.JpaQueryMethod.createParameters(JpaQueryMethod.java:284)
    at org.springframework.data.jpa.repository.query.JpaQueryMethod.createParameters(JpaQueryMethod.java:51)
    at org.springframework.data.repository.query.QueryMethod.<init>(QueryMethod.java:70)
    at org.springframework.data.jpa.repository.query.JpaQueryMethod.<init>(JpaQueryMethod.java:79)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:304)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:161)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:84)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
    ... 59 more

This looks like a bug to me. Is my feeling right? Anyone aware of a reported issue on that? Or maybe this is documented somewhere and justified behavior?

The spring-data-jpa version:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.5.2.RELEASE</version>
</dependency>
makasprzak
  • 5,082
  • 3
  • 30
  • 49
  • 2
    That's a bug. I've filed [DATACMNS-520](https://jira.spring.io/browse/DATACMNS-520) for you and am about to fix that :). Will reply with an answer once that's fixed. – Oliver Drotbohm Jun 12 '14 at 13:41

1 Answers1

10

This is a bug indeed. I filed and fixed DATACMNS-520 which will make it into Spring Data Commons 1.7.3, 1.8.1 and 1.9 M1.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
  • 1
    That's an immediate service:) Wouldn't like to be importunate, but I still wonder if the check wouldn't now fail for following : `User validWithPageableSecond(String username, Pageable pageable);` Didn't have chance to test it, but just looking at the code I think it will fail. I'm not sure it shouldn't actually? Can Pageable/Sortable be mixed with nonnamed parameters? – makasprzak Jun 12 '14 at 14:46
  • Actually it shouldn't (and the downstream tests all still work). Note, that the asserting method iterates over the *bindable* parameters (which rules `Pageable` and `Sort` out). The previous hard check for 0 as *parameter* index was the issue here. – Oliver Drotbohm Jun 12 '14 at 15:05
  • 1
    Thanks, should have check the tests out as well – makasprzak Jun 12 '14 at 15:12
  • This is still an issue in spring data 1.7.0? Please see https://stackoverflow.com/questions/25975159/pageable-and-param-in-a-spring-data-jparepository-method-issue-2 – Pedro Dusso Sep 22 '14 at 13:25