43

I want to get all the results in single page, I've tried with

Pageable p = new PageRequest(1, Integer.MAX_VALUE);
return customerRepository.findAll(p);

Above is not working, is there any methods to achieve this? Seems like it cannot be achieved from custom query as asked here.

Community
  • 1
  • 1
Ruwanka De Silva
  • 3,555
  • 6
  • 35
  • 51
  • 2
    And why would you even use pagination in that case? Makes 0 sense. – Branislav Lazic Feb 05 '17 at 15:31
  • 2
    Yeah you are correct..but I am trying to make an api..sometimes it just ask for the all data (with different search query)..but should be displayed in a table which is used also for paginated results in different time. So I want to keep the response format untouched. – Ruwanka De Silva Feb 05 '17 at 15:38

4 Answers4

129

The more correct way is to use Pageable.unpaged()

Pageable wholePage = Pageable.unpaged();
return customerRepository.findAll(wholePage);
chekmare
  • 1,818
  • 2
  • 8
  • 8
  • 1
    This should be the accepted answer! Thanks – Parth Manaktala Aug 16 '21 at 11:43
  • 1
    But I still want to sort it, how do i do it? – Legna Dec 07 '21 at 19:59
  • @Legna you can send a Sort Parameter in the repository method . https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-six-sorting/ . org.springframework.data.domain.Sort; myRepository.findAll(sortByIdAsc()); and pass new Sort(Sort.Direction.ASC, "id"); – Mohammed Idris Jan 13 '22 at 07:11
  • @MohammedIdris, What I meant is that Pageable.unpaged() doesn't allow sorting. My question was strictly related to the suggestion of using Pageable.unpaged() – Legna Feb 24 '22 at 15:42
  • Look [here](https://stackoverflow.com/a/58762676/7589291) – chekmare Mar 02 '22 at 20:04
51

Your page request is incorrect because you are looking for results on the wrong page. It should be:

PageRequest.of(0, Integer.MAX_VALUE);

The first page for results is 0. Since you're returning all records, they are all on this page.

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
  • 6
    This answer is not perfect. You will get a max page size limitation of 1000 which means you will not get all results when your items is greater than 1000. – Max Peng Apr 13 '17 at 03:48
  • @MaxPeng That's highly dependent on data store type. And the question is pretty much data store agnostic. – Branislav Lazic Apr 13 '17 at 10:52
  • 3
    @BranislavLazic Well, my point is spring data itself has a little trap which might override the the max page size you passed in, refer to https://github.com/spring-projects/spring-data-rest/blob/master/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/RepositoryRestConfiguration.java#L50 – Max Peng Apr 17 '17 at 04:14
  • 4
    This is deprecated as of now. You need to use `PageRequest.of(0, Integer.MAX_VALUE)` – radbrawler Dec 11 '18 at 09:47
  • what's the easiest way to make the first page of results be 1 ? – Don Code Jul 15 '19 at 21:50
  • @DonCode make a `Top1` or `First1` query for one result. – nurettin Jul 29 '19 at 12:12
9

If you pass null for Pageable, Spring will ignore it and brings all data.

Pageable p = null;
return customerRepository.findAll(p);
Karthik Bose
  • 33,556
  • 3
  • 33
  • 43
3

As of spirng-data-commons@2.1.0 correct syntax is PageRequest.of(0, Integer.MAX_VALUE). You can look here

radbrawler
  • 2,391
  • 2
  • 15
  • 22