春季可分页以在列表上分页<Object>不起作用



我有一个包含 10000 条记录的对象列表,我正在尝试将该记录拆分为 10 条记录,

但不知何故它不起作用..有人可以看看

@Query("select a.applicantid,coalesce(to_char(a.createdon,'yyyy-MM-dd'),to_char(filing_date,'yyyy-MM-dd')) as dt1, rn" + 
"coalesce(c.companyname,i.InstituteName,(u.firstname||' '||u.lastname),rn" + 
"u.firstname) ,a.c_denomination,cc.crop_common_name,cs.crop_botanical_name,rn" + 
"a.id,aps.status,a.cropid, rn" + 
"(select mv.varietytype from VarietyType mv where mv.id= a.varirtytypeid),rn" + 
"(select sv.subvarietytype from SubVarietyType sv,VarietyType mvr rn" + 
" where a.subvarietytypeid = sv.id and mvr.id= sv.varietyid),a.formtype,mcg.crop_group rn" + 
" from Applications a left join ApplicantRegistration ap on rn" + 
" a.applicantid = ap.id left join CompanyRegistration c on ap.companyid = c.id rn" + 
" left join InstitutionRegistration i on ap.institutionid = i.id rn" + 
" left join Crops cc on a.cropid = cc.id left join CropSpecies cs rn" + 
" on a.cropspeciesid =cs.id left join InternalUser u on ap.id = u.applicantid rn" + 
" left join ApplicationStatus aps on a.application_current_status = aps.id "
+ "left join CropGroup mcg on cc.cropgroupid = mcg.id order by a.id desc")
List<Object[]> getapplication_adminview();

List<Object[]> admin_viewapplication=applicationrepository.getapplication_adminview();
int pageNumber = 0;
int size = 10;
Pageable pageable = PageRequest.of(pageNumber, size); // object of pageable
Page<Object> pages = new PageImpl(admin_viewapplication, pageable, admin_viewapplication.size());
List<Object> lpage = pages.getContent(); // here i am getting the lpage size as 10000 but as i enter pageable as of size 10  i am expecting 10 results only

我哪里出错了? 如果我尝试将可分页对象添加到查询并运行代码,我将收到以下错误:

无法使用请求的结果类型 [java.lang.Long] 为具有多个返回项的查询创建 TypedQuery

;嵌套异常是 java.lang.IllegalArgumentException:无法使用请求的结果类型 [java.lang.Long] 为具有多个返回项的查询创建 TypedQuery。

Page只表示一页数据。因此page.getContent()创建此页面实例时,仅返回通过构造函数指定的一个页面中的所有数据。它与拆分页面中的数据无关。

如果要拆分列表,请使用ListsfromGuava最简单的方法:

List<List<Object>> splittedList = Lists.partition(list, 10);

如果你想做分页,将存储在数据库中的所有数据拆分成不同的小页面,在数据库级别拆分它,而不是让整个列表到内存中拆分,当整个列表很大时,这将非常低效。有关如何通过在查询方法中声明Pageable在数据库级别拆分它的信息,请参阅此处。

我们可以使用PagedListHolder,它可以更改页面中的列表,并且可以通过设置页面大小和页面来获取页面。

PagedListHolder<Object> page = new PagedListHolder(admin_viewapplicationpage);
page.setPageSize(50); // number of items per page
page.setPage(0);      // set to first page
int totalPages = page.getPageCount(); //  gives the totalpages according to the main list

List<Object> admin_viewapplication = page.getPageList();  // a List which represents the current page which is the sublist

以下教程帮助了我 -> https://www.baeldung.com/spring-data-jpa-query

此时 4.3.2.0.4 之前的 Spring 数据 JPA 版本

添加 n-- #pageable n非常重要

没有这个我错了

此外,分页设置必须没有排序

PageRequest paginaConf = new PageRequest ((param1 - 1)
, param2);

最后转换页面<对象>

Page <Object []> list = myQueryofRepo ();
List <XXXModel> lstReturn = myConversor (list.getContent ());
Page <XXXModel> ret = new PageImpl <XXXModel> (lstReturn, pageConf, param2);

最新更新