Reputation: 173
I'm using JpaPagingItemReaderBuilder to query a DB and the result is being insert in another DB.
Query is returning results with no issue but I'm getting an error with the return of the reader and in the processor you can check my coding and error below.
can someone please give me insight on this? and why I'm not able to process the result?
Here is my code:
@Bean
public Step sampleStep(){
return stepBuilderFactory.get("sampleStep")
.<FCR_HDR,FCR_HDR>chunk(5)
.reader(itemReader())
.processor(processor())
//.writer(i -> i.stream().forEach(j -> System.out.println(j)))
//.writer(i -> i.forEach(j -> System.out.println(j)))
.writer(jpaItemWriter())
.build();
}
@Bean
public Job sampleJob(){
return jobBuilderFactory.get("sampleJob")
.incrementer(new RunIdIncrementer())
.start(sampleStep())
.build();
}
@Bean
public FcrItemProcessor processor() {
return new FcrItemProcessor();
}
@Bean
@StepScope
public JpaPagingItemReader<FCR_HDR> itemReader(/*@Value("${query}") String query*/){
return new JpaPagingItemReaderBuilder<FCR_HDR>()
.name("db2Reader")
.entityManagerFactory(localContainerEntityManagerFactoryBean.getObject())
.queryString("select f.fcr_ref,f.num_subbills from FCR_HDR f where f.fcr_ref in ('R2G0130185','R2G0128330')")
//.queryString(qry)
.pageSize(3)
.build();
}
@Bean
@StepScope
public JpaItemWriter jpaItemWriter(){
JpaItemWriter writer = new JpaItemWriter();
writer.setEntityManagerFactory(emf);
return writer;
}
}
public class FcrItemProcessor implements ItemProcessor<FCR_HDR,FCR_HDR> {
private static final Logger log = LoggerFactory.getLogger(FcrItemProcessor.class);
@Nullable
@Override
public FCR_HDR process(FCR_HDR fcr_hdr) throws Exception {
final String fcrNo = fcr_hdr.getFcr_ref();
final String numsubbills = fcr_hdr.getNum_subbills();
final FCR_HDR transformFcr = new FCR_HDR();
transformFcr.setFcr_ref(fcrNo);
transformFcr.setNum_subbills(numsubbills);
log.info("Converting (" + fcr_hdr + ") into (" + transformFcr + ")");
return transformFcr;
}
}
Error:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.electronicfcr.efcr.model.FCR_HDR
Upvotes: 0
Views: 1556
Reputation: 90417
Since you configure the following query in the JpaPagingItemReader
:
.queryString("select f.fcr_ref,f.num_subbills from FCR_HDR f where f.fcr_ref in ('R2G0130185','R2G0128330')")
The query is in the format of JPQL
which will be processed by the JPA and JPA will return a Object[]
if you select certain mapped columns from the mapped entity.
Change it to :
.queryString("select f from FCR_HDR f where f.fcr_ref in ('R2G0130185','R2G0128330')")
such that it will return the mapped entity class (i.e FCR_HDR) and should solve your problem.
Upvotes: 3