spring数据JpaRepository方法问题中的Pageable和@Param [2] [英] Pageable and @Param in a spring data JpaRepository method issue [2]

查看:930
本文介绍了spring数据JpaRepository方法问题中的Pageable和@Param [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!).我的课是:

I'm aware of this question, but using org.springframework.data:spring-data-jpa:1.7.0.RELEASE I'm still having the same issue (Either use @Param on all parameters except Pageable and Sort typed once, or none at all!). My class is:

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,以便将第一个参数识别为不绑定到实际查询的参数.另外,您需要将返回类型更改为PageList,因为大多数情况下您将返回多个结果.

Make sure you use Pageable instead of PageRequest so that the first parameter is recognized as one not to be bound to the actual query. Also, you need to change the return type to either Page or List as you'll return multiple results mostly.

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

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

This should do the trick. Note, that we generally recommend not to extend the store specific interfaces as they expose store-specific API that should only be exposed if really necessary.

这篇关于spring数据JpaRepository方法问题中的Pageable和@Param [2]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆