Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 分页Spring boot JPA异常:方法必须具有以下返回类型之一_Java_Spring_Hibernate_Spring Boot_Jpa - Fatal编程技术网

Java 分页Spring boot JPA异常:方法必须具有以下返回类型之一

Java 分页Spring boot JPA异常:方法必须具有以下返回类型之一,java,spring,hibernate,spring-boot,jpa,Java,Spring,Hibernate,Spring Boot,Jpa,我正在尝试使用JPQL分页显示每页选定的记录。我的主回购接口扩展了分页和排序存储库接口。 下面是我的课程 StudentRepositoryPaging.java package com.hibernate.JPQLandNativeSql.repos; import java.util.List; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; imp

我正在尝试使用JPQL分页显示每页选定的记录。我的主回购接口扩展了分页和排序存储库接口。 下面是我的课程

StudentRepositoryPaging.java

package com.hibernate.JPQLandNativeSql.repos;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.hibernate.JPQLandNativeSql.entities.Student;
//To enable paging support, we just need to pass "Pageable" as a parameter to the methods as an arg
public interface StudentRepositoryPaging extends PagingAndSortingRepository<Student, Long> 
{
    @Query("from Student") //for executing JPQL, we write our queries inside @query annotation,this query is : "select * from Student" but we can also write it as "from Student"
    Page<Student> findAllStudents(Pageable page); //this will return a list. 

    @Query("select st.fname , st.lname from Student st")
    Page<Object[]> findAllStudentsPartial(Pageable page);// this will return an object array containing the returned columns

    @Query("from Student where fname=:fname")
    List<Student> findAllStudentsByFirstName(@Param("fname")String fname,Pageable page); //named queries using which you can pass arguments and those arguments can be used in queries

    @Query("select lname,score from Student where fname=:fname")
    List<Object[]> findAllStudentsByFirstNamePartial(@Param("fname")String fname,Pageable page); //named queries using which you can pass arguments and those arguments can be used in queries

    @Query("from Student where score>:min and score<:max")
    List<Student> findAllStudentsByScore(@Param("min")int min,@Param("max")int max,Pageable page);

    @Modifying //if we are using statements such as insert , update , delete, we need to use@Modifying, because Hibernate assumes that we are using select statements
    @Query("delete from Student where id=:id")
    void deleteFromTable(Long id,Pageable page);

}
@Test
    public void testFindAllPaging() {
        int pageNumber=0;
        int pageSize = 2;//number of database records to be displayed per page
        Pageable page = PageRequest.of(pageNumber, pageSize,Sort.by(Direction.DESC,"fname"));
        //Page<Student> findAllStudents = reposPaging.findAllStudents(page);
        Page<Object[]> studentsPartial = reposPaging.findAllStudentsPartial(page);
        for(Object[] s : studentsPartial ) {
            System.out.println(s[0]+"-----"+s[1]);
        }
    }

package com.hibernate.JPQLandNativeSql.repos;
导入java.util.List;
导入org.springframework.data.domain.Page;
导入org.springframework.data.domain.Pageable;
导入org.springframework.data.jpa.repository.Modifying;
导入org.springframework.data.jpa.repository.Query;
导入org.springframework.data.repository.crudepository;
导入org.springframework.data.repository.paging和sortingrepository;
导入org.springframework.data.repository.query.Param;
导入com.hibernate.JPQLandNativeSql.entities.Student;
//要启用分页支持,我们只需要将“Pageable”作为参数作为arg传递给方法
公共接口StudentRepositoryPaging扩展了分页和排序存储库
{
@Query(“from Student”)//为了执行JPQL,我们将查询写在@Query注释中,这个查询是:“select*from Student”,但我们也可以将它写为“from Student”
Page findAllStudents(可分页页面);//这将返回一个列表。
@查询(“从Student st中选择st.fname、st.lname”)
Page findallstudentsparial(可分页页面);//这将返回一个包含返回列的对象数组
@查询(“来自学生,其中fname=:fname”)
List findAllStudentsByFirstName(@Param(“fname”)String fname,Pageable page);//命名查询,您可以使用这些查询传递参数,并且这些参数可以在查询中使用
@查询(“选择lname,来自学生的分数,其中fname=:fname”)
列出findAllStudentsByFirstNamePartial(@Param(“fname”)字符串fname,可分页页面);//命名查询,您可以使用这些查询传递参数,并且这些参数可以在查询中使用
@Query(“from Student where score>:min和score返回类型
Page
将仅适用于本机查询

@Query(value=“选择st.fname,st.lname from Student st”,nativeQuery=true)
页面FindAllStudentSpatial(可分页页面);
Caused by: java.lang.IllegalStateException: Method has to have one of the following return types! [interface org.springframework.data.domain.Page, interface org.springframework.data.domain.Slice, interface java.util.List]
    at org.springframework.data.repository.query.QueryMethod.assertReturnTypeAssignable(QueryMethod.java:307) ~[spring-data-commons-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.data.repository.query.QueryMethod.<init>(QueryMethod.java:87) ~[spring-data-commons-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.data.jpa.repository.query.JpaQueryMethod.<init>(JpaQueryMethod.java:103) ~[spring-data-jpa-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:79) ~[spring-data-jpa-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lookupQuery(RepositoryFactorySupport.java:574) ~[spring-data-commons-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(RepositoryFactorySupport.java:567) ~[spring-data-commons-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_231]
    at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_231]