0

I am having a requirement to write into 2 tables header and Line table. After reading and processing I am saving the result into a list of Objects. In the writer I need to get each object from list and write into Header table(One row), get the ID of the primary Key for the row inserted and then use this Key as a Foreign key of Line table during insert.

I have 2 problems here: 1. Reading the list in item writer from list. I have used below 2 links to write this: Spring Batch - Using an ItemWriter with List of Lists Writing List of Items using JdbcBatchItemWriter

But getting the error:

java.lang.NullPointerException: null at org.springframework.batch.item.database.JdbcBatchItemWriter.write(JdbcBatchItemWriter.java:186) ~[spring-batch-infrastructure-4.2.1.RELEASE.jar:4.2.1.RELEASE]

  1. How to implement one to many mapping. One solution suggested is to write a custom service method: Spring Batch - write to 2 tables at once

Is it right to call another service method inside from writer?

Thanks, BR

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Welcome to SO. Please ask one question at a time, see https://stackoverflow.com/help/asking. For question 1, please post the full stack trace of the error and the code of your job configuration (From `JdbcBatchItemWriter.java:186` : https://github.com/spring-projects/spring-batch/blob/e673281e9c9a1abf1a1424d3a0c7424fb46c29c4/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcBatchItemWriter.java#L186, it looks like your `namedParameterJdbcTemplate` is not correctly configured) – Mahmoud Ben Hassine Apr 02 '20 at 09:30
  • Hi Mahnoud, Thanks for the reply. Yes You are right. There was a error in configuration. I resolved the issue. Coming to the second part. I have written CompositeItemWriter to write into Header and Line table. But having trouble in writing the data into Header table as I need to satisfy Parent child relationship also. For this i thought to call select next sequence for parent table and then store this in both the tables but having issue in writing custom JdbcBatchItemWriter. Is there any solution to this kind of problem? I will write a seperate question for this. Thanks, BR – Bhuvanesh Ramachandra Apr 02 '20 at 10:43
  • I believe this is the other question you created: https://stackoverflow.com/questions/60990171/spring-batch-item-writer-for-parent-child-relationship. I added an answer there. You can remove this one if you want. And btw, I added an answer to https://stackoverflow.com/questions/49756267/spring-batch-write-to-2-tables-at-once/60976938#60976938 as well. – Mahmoud Ben Hassine Apr 02 '20 at 17:41
  • Thanks Mahmoud . I'll try both the methods you mentioned above. Regards, – Bhuvanesh Ramachandra Apr 02 '20 at 18:02

0 Answers0