弹簧规范和可分页
Posted
技术标签:
【中文标题】弹簧规范和可分页【英文标题】:Spring Specification and Pageable 【发布时间】:2017-09-12 20:20:12 【问题描述】:如何同时使用Specification
和Pageable
?
personelRepository.java
@Query("SELECT e FROM PersonelEntity e ")
List<PersonelEntity> findData(Specification<PersonelEntity> test, Pageable pageable);
personelService.java
public List<PersonelEntity> filteredData(Specification<PersonelEntity> filter,Pageable pageable)
List<PersonelEntity> filteredData = personelRepository.findData(filter,pageable);
return filteredData;
personelController.java
Pageable reqCount = new PageRequest(0, 10);
Specification<PersonelEntity> filter = new FilterSpecification<PersonelEntity>(new SearchCriteria("name", "=","lux"));
personels = personelService.findData(filter,reqCount);
当我运行它时,我会收到这样的错误。但是,如果我像 findAll(Pageable page) 和 findAll(Specification filter) 那样分别调用 findAll() 函数,它就可以工作。但是不能一起用。
java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:502) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:692) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:181) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:141) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.StringQueryParameterBinder.bind(StringQueryParameterBinder.java:61) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:101) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.SpelExpressionStringQueryParameterBinder.bind(SpelExpressionStringQueryParameterBinder.java:69) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:161) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:152) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.doCreateQuery(AbstractStringBasedJpaQuery.java:81) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createQuery(AbstractJpaQuery.java:190) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:121) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:85) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
【问题讨论】:
这个是怎么用的,能提供一些代码吗? 【参考方案1】:通过编辑personelService.java并删除personelRepository上的findData函数来解决。我使用 springframework.data.jpa.repository.JpaRepository findAll() 函数并且它有效。
public List<PersonelEntity> filteredData (Specification<PersonelEntity> spec, Pageable pageable)
Page<PersonelEntity> pageData = personelRepository.findAll(spec,pageable);
List<PersonelEntity> filteredData = pageData.getContent();
return filteredData;
【讨论】:
如果我从我的存储库中删除findAll
,则会出现编译错误Cannot resolve method findAll(Specification<>, Pageable)
。在您的情况下,该方法是从弹簧接口继承的吗?如果有,是哪一个?
派对可能有点晚了,但我在 JpaSpecificationExecutor 界面中找到了
我将extends JpaSpecificationExecutor<PersonelEntity>
添加到我现有的存储库中。它就像一个魅力。以上是关于弹簧规范和可分页的主要内容,如果未能解决你的问题,请参考以下文章