spring-projects / spring-projects/spring-batch

Improve JPA repository readers to be more typesafe [BATCH-2697]

Open
#910 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: enhancement
Dominant language
Java
Stars
3k
Forks
2.5k
Avg merge
6d 53m
Merged PRs (30d)
3

Description

Niels Bech Nielsen opened BATCH-2697 and commented

The JPA implementation org.springframework.batch.item.data.RepositoryItemReader
use a loosely typed configuration to request a page of objects.

Currently one needs to:

  • set the repository
  • Specify the method name
  • Store the arguments in a List<Object>

As well as needing to know that the method

  • MUST return Page<T> and
  • MUST take a Pageable as last parameter.

All of this is very fragile to changes, and there is no refactoring tools or type tools to help in this.

Given that none of this is very typesafe, I suggest introduce a function interface similar to:

public interface JPARepositoryPageReader<T> {
  Page<T> readNextPage(Pageable page);
}

such that the repository read can be specified more typesafe. One derivative of this interface could use the method invocation of the RepositoryItemReader and have methodName and args injected.

In the below example I use the name 'JPARepositoryItemReader' as the 'new' implementation, and use a Java8 lambda to simply the page reader:

@Bean
public ItemReader<Customer> createReader() {
   JPARepositoryItemReader reader = new JPARepositoryItemReader();
   reader.setPageSize(1000);
   reader.setSort(someSort);  // BTW, would be nice to be able to use Sort object directly as well
   reader.setPageReader ( (pageable) -> customerRepository.findByLastName("Jones", pageable) );
   return reader;
}

No further details from BATCH-2697

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing RepositoryItemReader configuration and its methodName, repository, and argument handling. Compare that entry point with the proposed JPARepositoryPageReader interface and lambda-based pageReader API. Done means the JPA repository reader has a clearly defined type-safe page-reading design and its existing behavior remains covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.