Pinaki Mukherjee
Pinaki Mukherjee

Reputation: 1656

Spring batch writing to multiple tables based on order object

I am consuming order xml and converting them to order object using spring batch. The structure of order object is something like below:

public class Order{

prop1
prop2
---
propn
List<Item> items;
List<Shipping> shipping;
List<Billing> billing;
----
}

Now prop1 - popn will go to order table but items, shipping and billing information will go to different tables and for simplicity lets assume they one to many relationship. Currently, I am parsing the xml and populating the order bean and pushing some date using JdbcBatchItemWriter to order table only. In the documentation I see compositeWriter but don't see any good example.I am sure some one already implemented this kind of solution before so want to get some idea on this. It would be great if i get some information on how to achieve this with a simple example.

Upvotes: 0

Views: 2561

Answers (1)

Michael Minella
Michael Minella

Reputation: 21503

The ComposedItemWriter passes each item in the list passed to the ItemWriter and passes it to each of the list of delegate ItemWriter implementations. So you'd configure it as follows (each of the writer*() methods refers to a writer to a table you need to write to):

@Bean
public CompositeItemWriter<Order> orderCompositeItemWriter() {
    List<ItemWriter<Order>> delegates = new ArrayList<Order>();
    delegates.add(writer1());
    delegates.add(writer2());
    delegates.add(writer3());

    return new CompositeItemWriterBuilder<Order>()
            .delegates(delegates)
            .build();
}

Upvotes: 1

Related Questions