gpt4 book ai didi

java - Spring Boot JPA分页异常

转载 作者:行者123 更新时间:2023-12-02 01:55:30 27 4
gpt4 key购买 nike

我正在为我的服务实现分页,其中限制和偏移量作为请求正文的一部分。当我在服务级别的 PageRequest 中设置这些值时,我收到以下错误和实现

Caused by: java.lang.IllegalArgumentException: Either use @Param on all parameters except Pageable and Sort typed once, or none at all!
at org.springframework.util.Assert.isTrue(Assert.java:92) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.data.repository.query.Parameters.assertEitherAllParamAnnotatedOrNone(Parameters.java:297) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.query.Parameters.<init>(Parameters.java:91) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaParameters.<init>(JpaParameters.java:43) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryMethod.createParameters(JpaQueryMethod.java:325) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryMethod.createParameters(JpaQueryMethod.java:53) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.data.repository.query.QueryMethod.<init>(QueryMethod.java:77) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryMethod.<init>(JpaQueryMethod.java:89) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
... 55 common frames omitted

学生请求:

{
"name": "string",
"paging": {
"limit": 0,
"startOffset": 0
}
}

学生服务:

@Service
public class StudentService {

@Autowired
private StudentRepository studentRepository;

public List<Student> getAllStudents(StudentRequest studentRequest) {

List<Student> studentList = studentRepository.getAll(studentRequest,new PageRequest(studentRequest.getPaging().getStartOffset(), studentRequest.getPaging().getLimit()));
return studentList;
}
}

学生存储库:

@Repository
public interface StudentRepository extends CrudRepository<Student, Long> {

@Query(value = "select o from Student o where (o.name = :#{#studentRequest.name} OR :#{#studentRequest.name} IS NULL ) ")
public List<Student> getAll(@Param("studentRequest") StudentRequest studentRequest, PageRequest pageRequest);
}

最佳答案

检查错误:

对除一次键入的 Pageable 和 Sort 之外的所有参数使用 @Param,或者根本不使用 @Param!

这意味着您不能Pageable(或PageRequest)和中使用@Param参数进行排序,它们由spring自动处理。

只需删除 @Param("pageRequest") 即可正常工作。

提示:使用 Pageable 接口(interface)而不是 PageRequest 类。

关于java - Spring Boot JPA分页异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52371703/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com