RepositoryItemReader: java.util.ArrayList cannot be cast to org.springframework.data.domain.Page

DIMAKATSO BOPAPE picture DIMAKATSO BOPAPE · Aug 14, 2017 · Viewed 7.8k times · Source

I'm setting up an ItemRepositoryReader for the reader in a spring batch step. I wanna use findByScenarioBetween method which takes two parameters, min, and max.

My repo

public interface FuneralPricingRepository extends JpaRepository<FuneralPricing, Long> {

    @Query("select p from FuneralPricing p where p.scenario between ?1 and ?2")
    List<FuneralPricing> findByScenarioBetween(Long min, Long max, Pageable pageable);


}

My Reader

  @Bean
  @StepScope
  public ItemReader<FuneralPricing> funeralPricingRepositoryReader(
          @Value("#{jobParameters[start]}") Long start,
          @Value("#{jobParameters[end]}") Long end) {
    RepositoryItemReader<FuneralPricing> repositoryItemReader = new RepositoryItemReader<>();
    repositoryItemReader.setPageSize(10);
    repositoryItemReader.setSort(funeralPricingSort());
    repositoryItemReader.setRepository(funeralPricingRepository);
    repositoryItemReader.setMaxItemCount(100);
    repositoryItemReader.setMethodName("findByScenarioBetween");
    List parameters = new ArrayList();
    parameters.add(start);
    parameters.add(end);
    repositoryItemReader.setArguments(parameters);
    return repositoryItemReader;
  }

When I use findAll it's working fine. But then I don't wanna read all row every time. I don't know if I am missing something in my reader!, Thanks.

java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.springframework.data.domain.Page at org.springframework.batch.item.data.RepositoryItemReader.doPageRead(RepositoryItemReader.java:208) at org.springframework.batch.item.data.RepositoryItemReader.doRead(RepositoryItemReader.java:153) at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133) at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) at com.sun.proxy.$Proxy120.read(Unknown Source) at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91) at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:157) at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:116) at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374) at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144) at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:110) at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69) at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) at org.sp

Answer

Barath picture Barath · Aug 14, 2017

Update List<FuneralPricing>to Page<FuneralPricing>

public interface FuneralPricingRepository extends JpaRepository<FuneralPricing, Long> {


    Page<FuneralPricing> findByScenarioBetween(Long min, Long max, Pageable pageable);


}