Spring数据JpaRepository方法问题中的Pagea

Spring数据JpaRepository方法问题中的Pagea

本文介绍了在Spring数据JpaRepository方法问题中的Pageable和@Param的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以下存储库:

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?

spring-data-jpa版本:

The spring-data-jpa version:

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


推荐答案

这确实是一个错误。我提交并修复了,这将进入Spring Data Commons 1.7.3,1.8.1和1.9 M1。

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.

这篇关于在Spring数据JpaRepository方法问题中的Pageable和@Param的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:43