2

I have a table with over 10 000 rows. I am using Spring with JPA and i want to make a "batch" job where i can fetch 500 rows at time until there is no more to get from the table? I have a repository class which extends JpaRepository<SchoolAdmin, Long>. I am new to Spring so i would appreciate if anyone can help me?

Thank you :)

ETO
  • 6,970
  • 1
  • 20
  • 37

1 Answers1

1

You should try something like this:

public interface SchoolAdminRepository extends PagingAndSortingRepository< SchoolAdmin, Long> {
    List<SchoolAdmin> findAll(Pageable pageable);
}

Then call method providing a Pageable object:

List<SchoolAdmin> page = repository.findAll(PageRequest.of(pageNumber, 500));
ETO
  • 6,970
  • 1
  • 20
  • 37
  • But how do i know where to start for the next 500 rows? And how do i know how amny there are left? i Want to fetch 500 at time until all are fetched –  Apr 16 '20 at 07:15
  • The `pageNumber` will tell you where to start next (depending on page size). – ETO Apr 16 '20 at 14:34
  • But do i need to extend `PaginationAndSortingRepository` ? –  Apr 16 '20 at 14:37
  • It wont give me the method `.of` ? Does it depend on my spring version? –  Apr 16 '20 at 14:39
  • 1
    Perhaps you're using an old version. Check this: https://stackoverflow.com/questions/44848653/pagerequest-constructors-have-been-deprecated – ETO Apr 16 '20 at 14:43
  • 1
    Or you can replace the static `PageRequest::of` with constructor `new PageRequest(pageNumber, 500)` – ETO Apr 16 '20 at 14:44