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

Ped*_*sso 10 java spring jpa spring-mvc spring-data

我知道这个问题,但使用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!).我的班级是:

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);
}
Run Code Online (Sandbox Code Playgroud)

编辑

呼叫:

 Pageable page = new PageRequest(0, 1, Sort.Direction.DESC, "date");
        BalanceHistory bh = balanceHistoryRepository.findCurrentBalanceByAccountNumber(1,page);
Run Code Online (Sandbox Code Playgroud)

方法:

@Query("select bh from BalanceHistory bh where bh.account.id = :idAccount")
public BalanceHistory findCurrentBalanceByAccountNumber(@Param("idAccount") long idAccount, Pageable pageCriteira);
Run Code Online (Sandbox Code Playgroud)

Oli*_*ohm 19

确保使用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);
}
Run Code Online (Sandbox Code Playgroud)

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

  • 修复您的退货类型.你输入一个`Pageable`所以结果必须是一个结果页而不是一个结果. (3认同)