How Can I use Streaming or Pagination in Spring Batch

27 views Asked by At

I'm so new on spring-batch. I just try to fetch all data from table via stream and save to csv file.

You know that to read table effectively, we can use jpa stream.

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {

    @Query("SELECT DISTINCT e from Employee e "
            + "LEFT JOIN FETCH e.salaries "
            + "LEFT JOIN FETCH e.titles "
            + "WHERE e.hireDate BETWEEN ?1 AND ?2 "
            + "ORDER BY e.employeeId")
    @QueryHints(value = {
            @QueryHint(name = HINT_FETCH_SIZE, value = "" + Integer.MIN_VALUE),
            @QueryHint(name = HINT_CACHEABLE, value = "false"),
            @QueryHint(name = HINT_READONLY, value = "true"),
            @QueryHint(name = HINT_PASS_DISTINCT_THROUGH, value = "false")
    })    
    Stream<Employee> findByHireDateBetween(LocalDate from, LocalDate to);
}

I wonder that Can I use it on spring-batch? Or can I use pagination on spring-batch? For example List<Employee> findAll(Pageable pageable);

1

There are 1 answers

2
Mahmoud Ben Hassine On BEST ANSWER

Spring Batch provides the RepositoryItemReader which is a paging item reader. It requires a PagingAndSortingRepository.

Otherwise you can create a custom reader that extends AbstractPaginatedDataItemReader.