spring数据JpaRepository方法问题中的Pageable和@Param [2]


问题内容

我已经知道了这个问题,但是使用时org.springframework.data:spring-data-jpa:1.7.0.RELEASE我仍然遇到相同的问题(Either use @Param on all parameters exceptPageable and Sort typed once, or none at all!)。我的课是:

public interface BalanceHistoryRepository extends JpaRepository<BalanceHistory, Long> {
    @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<BalanceHistory, Long> {

  @Query("select bh from BalanceHistory bh where bh.account.id = :idAccount")
  Page<BalanceHistory> findCurrentBalanceByAccountNumber(@Param("idAccount") long idAccount, Pageable pageable);
}

这应该可以解决问题。请注意,我们通常 _不_建议扩展商店特定的接口,因为它们公开了商店特定的API,只有在确实必要时才应公开它们。