spring-batch icon indicating copy to clipboard operation
spring-batch copied to clipboard

Unable to use projections in ItemReader

Open jobas2007 opened this issue 1 year ago • 1 comments

While tyring to use projections in item reader, always getting "NoSuchMethodException" (for the projection method in jpa repository). Tried multiple ways, although same works when invoking via say a controller. Error: Caused by: java.lang.NoSuchMethodException: jdk.proxy2.$Proxy115.findDistinctByCreatedAfter(java.time.LocalDate,org.springframework.data.domain.PageRequest) at java.base/java.lang.Class.getMethod(Class.java:2405) ~[na:na]

Env: Spring Boot 3.X, JDK 17

Created a github demo project with all set up to demonstrate issue runtime https://github.com/jobas2007/batch-h2-demo

Expected behavior is to be able to use projection fitting into batch processing

All details are in the stackoverflow https://stackoverflow.com/questions/79002944/purge-group-of-old-records-iteratively-to-insert-fresh-data-daily

Appreciate any help/assistance/suggestions, as been struggling for a while now. Thanks

jobas2007 avatar Sep 20 '24 15:09 jobas2007

You can use the RepositoryItemReader also with projections. But there are conditions that need to be fulfilled:

  • The last argument of the method that you want to use with the reader must be a Pageable. See e.g. the Javadoc of the methodName on the builder.
  • The method must return a subtype of Slice, in particular not List.
  • You must include the column that you want to use for the pagination in the projection.

So if I add the following to your CountryDistinctView

Integer getId();

and the following to your CountyRepository

Slice<CountryDistinctView> findDistinctByCreatedAfter(LocalDate created, Pageable pageable);

then your sample app seems okay to me.

hpoettker avatar Nov 24 '24 03:11 hpoettker