【问题标题】:Pageable and @Param in a spring data JpaRepository method issue春季数据JpaRepository方法问题中的可分页和@Param
【发布时间】:2014-08-01 23:39:25
【问题描述】:

我有以下存储库:

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);
}

如上定义的工作正常,但如果我切换参数顺序:

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);
}

我在上下文初始化时遇到以下异常(发布最有趣的根本原因):

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

这对我来说似乎是一个错误。我的感觉对吗?有人知道这方面的报告问题吗?或者这可能记录在某处并且是合理的行为?

spring-data-jpa 版本:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.5.2.RELEASE</version>
</dependency>

【问题讨论】:

  • 这是一个错误。我已经为你提交了DATACMNS-520 并且即将解决这个问题:)。解决后会回复。

标签: java jpa spring-data spring-data-jpa


【解决方案1】:

这确实是一个错误。我提交并修复了DATACMNS-520,它将使其成为 Spring Data Commons 1.7.3、1.8.1 和 1.9 M1。

【讨论】:

  • 这是一项即时服务:) 不想强求,但我仍然想知道现在检查是否不会因以下原因而失败:User validWithPageableSecond(String username, Pageable pageable); 没有机会测试它,但是只看代码我认为它会失败。我不确定它实际上不应该吗? Pageable/Sortable 可以与非命名参数混合使用吗?
  • 实际上它不应该(并且下游测试仍然有效)。请注意,断言方法迭代 bindable 参数(规则 PageableSort out)。之前将 0 作为 parameter 索引的硬检查是这里的问题。
  • 谢谢,应该也检查一下测试
  • 这还是spring data 1.7.0的问题?请看stackoverflow.com/questions/25975159/…
猜你喜欢
  • 2014-11-16
  • 2023-03-31
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
相关资源
最近更新 更多