Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all the records for findAll () service using pagination and spring data jpa?

How can I get all the records for findAll () service using pagination and Spring Data JPA when we do not apply filters it should return all the records rather than displaying it pagewise.I have findAll (Pageable pageable) service and calling it from custom repository. IS it possible to get all the records in one page only using pagination?

like image 294
Aditya Gupta Avatar asked Jan 27 '17 10:01

Aditya Gupta


2 Answers

public interface UserRepository extends PagingAndSortingRepository<User, Long> {
    //Page<User> findAll(Pageable pageable); is already in this repository.
}

So just in case you want to find the first page of size 20 try:

Page<User> users = repository.findAll(new PageRequest(0, 20));

If what you want to do is to get all entities on a single page, it doesn't make much sense but can be done in two steps:

int count = repository.count();
Page<User> users = repository.findAll(new PageRequest(0, count));

count() comes from CrudRepository which is extended by PagingAndSortingRepository.

like image 135
xenteros Avatar answered Sep 24 '22 07:09

xenteros


If you are using PagingAndSortingRepository and still want a list of "things",

You can add a method List<Thing> findBy()

If you have a RepositoryRestResource, it will be exposed as REST: /things/search/findBy

like image 24
MNassar Avatar answered Sep 24 '22 07:09

MNassar