Reputation: 3694
I have a list of object which contain 10000 records i am trying to split that records in each of 10,
But somehow it is not working.. can someone have a look
@Query("select a.applicantid,coalesce(to_char(a.createdon,'yyyy-MM-dd'),to_char(filing_date,'yyyy-MM-dd')) as dt1, \r\n" +
"coalesce(c.companyname,i.InstituteName,(u.firstname||' '||u.lastname),\r\n" +
"u.firstname) ,a.c_denomination,cc.crop_common_name,cs.crop_botanical_name,\r\n" +
"a.id,aps.status,a.cropid, \r\n" +
"(select mv.varietytype from VarietyType mv where mv.id= a.varirtytypeid),\r\n" +
"(select sv.subvarietytype from SubVarietyType sv,VarietyType mvr \r\n" +
" where a.subvarietytypeid = sv.id and mvr.id= sv.varietyid),a.formtype,mcg.crop_group \r\n" +
" from Applications a left join ApplicantRegistration ap on \r\n" +
" a.applicantid = ap.id left join CompanyRegistration c on ap.companyid = c.id \r\n" +
" left join InstitutionRegistration i on ap.institutionid = i.id \r\n" +
" left join Crops cc on a.cropid = cc.id left join CropSpecies cs \r\n" +
" on a.cropspeciesid =cs.id left join InternalUser u on ap.id = u.applicantid \r\n" +
" 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
where i am going wrong in this ? if i am trying to add pagable object to query and run the code i will get the following error:
Cannot create TypedQuery for query with more than one return using requested result type [java.lang.Long]; nested exception is java.lang.IllegalArgumentException: Cannot create TypedQuery for query with more than one return using requested result type [java.lang.Long]
Upvotes: 0
Views: 8162
Reputation: 179
the following tutorial helped me -> https://www.baeldung.com/spring-data-jpa-query
At this point 4.3. Spring Data JPA Versions Prior to 2.0.4
VERY IMPORTANT to add \ n-- #pageable \ n
Without this I was wrong
Also the pagination setting must be without ordering
PageRequest paginaConf = new PageRequest ((param1 - 1)
, param2);
Finally to convert the Page <Object []>
Page <Object []> list = myQueryofRepo ();
List <XXXModel> lstReturn = myConversor (list.getContent ());
Page <XXXModel> ret = new PageImpl <XXXModel> (lstReturn, pageConf, param2);
Upvotes: 1
Reputation: 3694
We can use PagedListHolder which can change the list in pages and we can than fetch a page by setting it's page size and page.
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
Upvotes: 1
Reputation: 90447
Page
just represents one page of data . So page.getContent()
only return all data in one page which is specified through constructor when you create this page instance . It has nothing to do with splitting the data in a page.
If you want to split a list , use Lists
from Guava
is the simplest way to go :
List<List<Object>> splittedList = Lists.partition(list, 10);
If you want to do pagination which split all the data stored in the database into different smaller pages , split it at the database level rather than getting the whole list to the memory to split which will be very inefficient when the entire list is large. See this for how to split it at the database level by declaring Pageable
in the query method.
Upvotes: 1