spring-batchitemwriter

Spring-Batch: Item writer for Parent-Child relationship


I have written a item processor which returns the list of Objects. This object needs to be split into 2 data base table (One parent and a child). One header row and for this corresponding header ID we have child rows associated in child table. I have used ListUnpackingItemWriter example to solve list problem. I have used CompositeItemWriter to split the result into 2 writer, Now I need to split each one for header and child table. Now each writer has same number of rows. IS there a better way to do it? Solve both Table Primary key problem. I need example write custom item writer which does validation before insert. Thanking you in advance.

Below is the code

public JdbcBatchItemWriter<T> myWriter() {
         JdbcBatchItemWriter<T> myWriter = new JdbcBatchItemWriter<T>(); 
         myWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<T>());   
         myWriter.setSql("INSERT INTO Parent table( colums) values ( values )");
            myWriter.setDataSource(dataSource);
            myWriter.afterPropertiesSet();

            return myWriter;
        }

        public JdbcBatchItemWriter<T> myOtherWriter() {
             JdbcBatchItemWriter<T> myWriter = new JdbcBatchItemWriter<T>(); 1
             myWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<T>());   
             myWriter.setSql("INSERT INTO child table( colums) values ( values )");
             myWriter.setDataSource(dataSource);
             myWriter.afterPropertiesSet();
            return myWriter;
        }

        public CompositeItemWriter<T> compositeItemWriter() {
            CompositeItemWriter<T> writer = new CompositeItemWriter<T>();
            writer.setDelegates(Arrays.asList(myWriter(),myOtherWriter())); 
            return writer;
        }
    ```

Solution

  • If the composite writer does not work for you, then can you use a custom writer, something like:

    import java.util.List;
    
    import javax.sql.DataSource;
    
    import org.springframework.batch.item.ItemWriter;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    public class MyParentChildWriter implements ItemWriter<Object> {
    
        private JdbcTemplate jdbcTemplate;
    
        public MyParentChildWriter(DataSource dataSource) {
            this.jdbcTemplate = new JdbcTemplate(dataSource);
        }
    
        @Override
        public void write(List<?> items) {
            for (Object item : items) {
                // get parent/child values from item and use them in the query as needed
                jdbcTemplate.update("INSERT INTO Parent table( colums) values ( values )");
                jdbcTemplate.update("INSERT INTO child table( colums) values ( values )");
            }
        }
    }
    

    Note that all update statements will be executed in a single transaction as explained in the Chunk-oriented Processing section.