21

I am trying to implement pagination feature in Spring Data JPA. I am referring this Blog My Controller contains following code :

 @RequestMapping(value="/organizationData", method = RequestMethod.GET)
  public String list(Pageable pageable, Model model){
    Page<Organization> members = this.OrganizationRepository.findAll(pageable);
    model.addAttribute("members", members.getContent());
    float nrOfPages = members.getTotalPages();
    model.addAttribute("maxPages", nrOfPages);
    return "members/list"; 
  }

My DAO is following :

@Query(value="select m from Member m", countQuery="select count(m) from Member m")
  Page<Organization> findMembers(Pageable pageable);

I am able to show first 20 records, how do I show next 20??? Is there any other pagination example that I can refer??

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Tejas
  • 239
  • 1
  • 2
  • 6
  • The blog you refer to also has a section which allows you to specify the page number. I would imagine that would be what you would want to use. – Makoto Nov 21 '16 at 07:18
  • Yes, I saw that but, it is Spring MVC, I want to implement same in Spring Data JPA. How do i modify it to Spring data JPA?/ – Tejas Nov 21 '16 at 07:21
  • ...If you've been following the entire tutorial, which was geared towards Spring MVC using Spring Data, then why not at least give it a try? Or did you give it a try already? If you did, then could show the code you attempted, what you expected to happen, and what did happen? – Makoto Nov 21 '16 at 07:23
  • Just pas a request parameter with the name `page`. that will be bound to the `Pageable` object in your request method. – M. Deinum Nov 21 '16 at 07:24
  • Actually the project that I am working on is in Spring Data JPA, so i need to implement same in Spring Data JPA. – Tejas Nov 21 '16 at 07:26
  • @M.Deinum I am also thinking the same, but I dont know where exactly should I pass the parameter. – Tejas Nov 21 '16 at 07:29
  • The URL you are calling. As explained [here](http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.web.basic.paging-and-sorting) in the reference guide. However if you follow the tutorial that will also (in the end) do exactly the same... – M. Deinum Nov 21 '16 at 07:29
  • @M.Deinum so what should I pass in the URL?? – Tejas Nov 21 '16 at 07:39
  • As mentioned a request parameter named page with the page number you want to see. – M. Deinum Nov 21 '16 at 07:42
  • So are you saying like ....url?page=0&size=0 ? – Tejas Nov 21 '16 at 07:42
  • @Tejas Either you can specify PageRequest as specified in the answer it will work but it is redundant since by passing "url?page=1&size=20" Pageable will automatically wire the page & size property and will fetch 2nd page (Since page number starts with 0) with 20Items. – VelNaga Nov 21 '16 at 16:09

4 Answers4

46

The constructors of Pageable are deprecated, use of() instead:

Pageable pageable = PageRequest.of(0, 20);
snowfox
  • 1,978
  • 1
  • 21
  • 21
34

I've seen similar problem last week, but can't find it so I'll answer directly.

Your problem is that you specify the parameters too late. Pageable works the following way: you create Pageable object with certain properties. You can at least specify:

  1. Page size,
  2. Page number,
  3. Sorting.

So let's assume that we have:

PageRequest p = new PageRequest(2, 20);

the above passed to the query will filter the results so only results from 21th to 40th will be returned.

You don't apply Pageable on result. You pass it with the query.

Edit:

Constructors of PageRequest are deprecated. Use Pageable pageable = PageRequest.of(2, 20);

xenteros
  • 15,586
  • 12
  • 56
  • 91
1

You can use Page, List or Slice.

If you dont need the number of pages, and only need to know if the next page exists, use Slice, since it does not do the "count" query:

  for (int i = 0;  ; i++) { 
     Slice<Organization> pageOrganization = organizationRepository.find(PageRequest.of(0, 100));        
    
    
     List<Organization> organizationList = pageOrganization.getContent();
     for (Organization org : organizationList) {
           // code
     }
    
    if (!pageOrganization.hasNext()) {
        break;
    }   
}
Tiago PC
  • 109
  • 1
  • 7
0

Pageable object by default comes with size 20, page 0, and unsorted So if you want the next page in front end the url can be sent with query params page, size,sort and these u can test it on postman.