Spring中的Pageable和@Param数据JpaRepository方法问题
我知道这个问题,但是使用org.springframework.data:spring-data-jpa:1.7.0.RELEASE
我仍然遇到同样的问题( Either use @Param on all parameters except Pageable and Sort typed once, or none at all!
)。 我的class级是:
public interface BalanceHistoryRepository extends JpaRepository { @Query("select bh from BalanceHistory bh where bh.account.id = :idAccount") public BalanceHistory findCurrentBalanceByAccountNumber(PageRequest pageCriteira, @Param("idAccount") long idAccount); }
编辑
呼叫:
Pageable page = new PageRequest(0, 1, Sort.Direction.DESC, "date"); BalanceHistory bh = balanceHistoryRepository.findCurrentBalanceByAccountNumber(1,page);
方法:
@Query("select bh from BalanceHistory bh where bh.account.id = :idAccount") public BalanceHistory findCurrentBalanceByAccountNumber(@Param("idAccount") long idAccount, Pageable pageCriteira);
确保使用Pageable
而不是PageRequest
以便将第一个参数识别为不与实际查询绑定的参数。 此外,您需要将返回类型更改为Page
或List
因为您将主要返回多个结果。
public interface BalanceHistoryRepository extends CrudRepository { @Query("select bh from BalanceHistory bh where bh.account.id = :idAccount") Page findCurrentBalanceByAccountNumber(@Param("idAccount") long idAccount, Pageable pageable); }
这应该可以解决问题。 请注意,我们通常建议不要扩展特定于商店的接口,因为它们会公开特定于商店的API,只有在必要时才会公开。