spring data jpa limit pagesize, how to set to maxSize
java, spring-data-jpa
Solution
In conjunction with Spring MVC you can use `PageableDefaults` annotation with `value = Integer.MAX_VALUE` like
public String showUsers(Model model,
@PageableDefaults(pageNumber = 0, value = Integer.MAX_VALUE) Pageable pageable) { … }
see PageableDefaults annotation Javadoc.
In any other client code you can set second constructor parameter to `Integer.MAX_VALUE`:
new PageRequest(
queryForm.getPageNumber()- 1,
queryForm.getPageSize() == null ? Integer.MAX_VALUE : queryForm.getPageSize(),
Sort.Direction.ASC,"id");
see PageRequest constructor. I assume that `queryForm.getPageSize()` is a wrapper type not a primitive. Otherwise you get a zero if pageSize wasn't set by the user (intentionally for a "search all" request).
UPDATE:
Since Spring Data Commons 1.6 you should use `PageableDefault` instead of `PageableDefaults`
public String showUsers(Model model,
@PageableDefault(page= 2 ,value = Integer.MAX_VALUE)
See PageableDefault annotation Javadoc.
Problem
I have one requirement is to search by pageable and non-pageable, and in my Java code, I use spring data jpa Pageable class, ``` Pageable pageable = new PageRequest( queryForm.getPageNumber()- 1, queryForm.getPageSize(),Sort.Direction.ASC,"id"); Page page = repository.fullTextSearch(queryForm.getText(), pageable); ``` And I don't want to change the return structure, So when non-pageable situation (search all), how to set the pageSize value to MAX ?