spring-projects / spring-batch

Spring Batch is a framework for writing batch applications using Java and Spring
http://projects.spring.io/spring-batch/
Apache License 2.0
2.74k stars 2.36k forks source link

Unable to use projections in ItemReader #4665

Open jobas2007 opened 2 months ago

jobas2007 commented 2 months ago

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

hpoettker commented 4 days ago

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

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.