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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25975159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:04:29  来源:igfitidea点击:

Pageable and @Param in a spring data JpaRepository method issue [2]

javaspringspring-mvcjpaspring-data

提问by Pedro Dusso

I'm aware of thisquestion, but using org.springframework.data:spring-data-jpa:1.7.0.RELEASEI'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:

我知道这个问题,但使用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);
}

Edit

编辑

Call:

称呼:

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

Method:

方法:

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

回答by Oliver Drotbohm

Make sure you use Pageableinstead of PageRequestso 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 Pageor Listas you'll return multiple results mostly.

确保您使用Pageable而不是,PageRequest以便第一个参数被识别为不绑定到实际查询的参数。此外,您需要将返回类型更改为Pageor ,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);
}

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

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

回答by tk_

I encountered the same exception when I accidentally imported the wrong Pageableclass.

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

This can also happen if you use PageRequestin the repository as well.

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

it should be,

它应该是,

import org.springframework.data.domain.Pageable;