分页时出现错误,但使用列表时正常工作。

huangapple go评论81阅读模式
英文:

Getting error with paging but working fine with List

问题

我正在处理一个项目,其中包含两个实体:学生和教师。
它遵循@ManyToOne关系。一个教师可以有多个学生。我在运行项目时遇到了一些困难。

Pageable page = PageRequest.of(pageNo, size)

@Query(value = "SELECT s FROM TeacherEntity s JOIN FETCH s.student d WHERE s.student IN (:students) and s.deleted = false" +
" ORDER BY s.createdAt DESC")
Page findByStudent(@Param("students") Collection students, Pageable pageable);

我遇到了这个错误:“FactoryBean在对象创建时抛出异常;嵌套异常是java.lang.IllegalArgumentException: 对于方法public abstract org.springframework.data.domain.Page ”,当我使用分页时。

当我不使用分页时,像这样:

@Query(value = "SELECT s FROM TeacherEntity s JOIN FETCH s.student d WHERE s.student IN (:students) and s.deleted = false" +
" ORDER BY s.createdAt DESC")
List findByStudentIn(@Param("students")
Collection student);

它正常工作。有人可以告诉我我做错了什么吗?

org.springframework.beans.factory.BeanCreationException: 在对象创建时,bean名称'teacherRepository'的工厂Bean抛出异常;嵌套异常是java.lang.IllegalArgumentException:验证方法public abstract org.springframework.data.domain.Page com.pro.aks.repository.TeacherRepository.findByStudent(java.util.Collection,org.springframework.data.domain.Pageable)失败!
   
在这里,我只提供了您要求的翻译内容,没有添加其他额外的内容。

英文:

I am working on a project which has two entities: Student and teacher.
It follows the @ManyToOne relationship. One Teacher can have multiple students. I am facing some difficulty while running the project.

Pageable page = PageRequest.of(pageNo, size)

@Query(value = "SELECT s FROM TeacherEntity s JOIN FETCH s.student d WHERE s.student IN (:students) and s.deleted = false" +
            " ORDER BY s.createdAt DESC")
Page<TeacherEntity> findByStudent(@Param("students") Collection<StudentEntity> students, Pageable pageable);

I am getting this error "FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Count query validation failed for method public abstract org.springframework.data.domain.Page " when I use paging.

When I do it without using paging like this,

@Query(value = "SELECT s FROM TeacherEntity s JOIN FETCH s.student d WHERE s.student IN (:students) and s.deleted = false" +
            " ORDER BY s.createdAt DESC")
List<TeacherEntity> findByStudentIn(@Param("students") 
Collection<StudentEntity> student); 

, it is working fine. Can someone please tell me what is the mistake I am doing?

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teacherRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Count query validation failed for method public abstract org.springframework.data.domain.Page com.pro.aks.repository.TeacherRepository.findByStudent(java.util.Collection,org.springframework.data.domain.Pageable)!
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:176) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:101) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1827) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getObjectForBeanInstance(AbstractAutowireCapableBeanFactory.java:1265) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:624) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:612) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	at org.springframework.data.repository.config.DeferredRepositoryInitializationListener.onApplicationEvent(DeferredRepositoryInitializationListener.java:51) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
	at org.springframework.data.repository.config.DeferredRepositoryInitializationListener.onApplicationEvent(DeferredRepositoryInitializationListener.java:36) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]


</details>


# 答案1
**得分**: -1

你需要打印出完整的堆栈跟踪以检查错误:**计数查询验证失败**

您需要在您的方法“findByStudent”中定义“countQuery”。有关详细信息,请查看此链接:https://stackoverflow.com/questions/38349930/spring-data-and-native-query-with-pagination

Spring框架无法创建对象,因为方法定义(注释)是错误的。

**背景技术:**
Spring框架将使用“countQuery”注释(HQL)来生成“原始SQL”(SQL),如果缺少此信息,则Spring框架不知道如何计算数据。

<details>
<summary>英文:</summary>

You need to print out the full stacktrace for checking the error: **Count query validation failed**

You need to define &quot;countQuery&quot; in your method &quot;findByStudent&quot;. Check this link for the details: https://stackoverflow.com/questions/38349930/spring-data-and-native-query-with-pagination

Spring framework can&#39;t create the object because the method definition (annotation) is wrong.

**Technology behind:**
Spring framework will use the &quot;countQuery&quot; annotation (HQL) to generate the &quot;Raw SQL&quot; (SQL), if this information is missing, then Spring framework don&#39;t know how to count the data.

</details>



huangapple
  • 本文由 发表于 2020年9月27日 02:13:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64081047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定