可分页和@Param在弹簧数据JpaRepository方法问题[2]

2022-09-04 07:12:40

我知道这个问题,但使用我仍然遇到同样的问题()。我的班级是:org.springframework.data:spring-data-jpa:1.7.0.RELEASEEither 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);
}

编辑

叫:

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

答案 1

请确保使用 而不是将第一个参数识别为不绑定到实际查询的参数。此外,您需要将返回类型更改为其中之一,或者因为您将主要返回多个结果。PageablePageRequestPageList

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,这些 API 应仅在确实必要时才公开。


答案 2

当我不小心导入错误的类时,我遇到了相同的异常。Pageable

如果您在存储库中使用,也可能发生这种情况。PageRequest

它应该是,

import org.springframework.data.domain.Pageable;

推荐