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?
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With