![](/img/trans.png)
[英]Pageable and @Param in a spring data JpaRepository method issue
[英]Pageable and @Param in a spring data JpaRepository method issue [2]
我知道这个问题,但是使用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<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,只有在必要时才会公开。
当我意外地导入了错误的Pageable
类时遇到了同样的异常。
如果您也在存储库中使用PageRequest
,也会发生这种情况。
它应该是,
import org.springframework.data.domain.Pageable;
存储库方法应该使用 Pageable 以允许使用 PageRequest 以外的其他实现,例如
public interface BalanceHistoryRepository extends CrudRepository<BalanceHistory, Pageable> {
@Query("select bh from BalanceHistory bh where bh.account.id = :idAccount")
Page<BalanceHistory> findCurrentBalanceByAccountNumber(@Param("idAccount") long idAccount, Pageable pageable);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.