Using spring-batch and JPA (provided by hibernate).
I have a step that does the following:
- reads all clients from DB (Client entity)
- enhances them with data from 3rd party. The ItemProcessor goes to the 3rd party data source, fetches some data that it stores in the Client entity itself (its fields) but also brings more data that is stored as different entities (ClientSale) and Client has a property of List which is mapped by ManyToOne.
- The modified entity (Client) and the new ones (ClientSale) need to be stored in DB.
The reader part is straight forward, and for the writer I used JPAItemWriter. At the processing stage I tried to update the fields, create the new ones and add them to the client’s list and return the client, hoping that the writer will write both the referenced objects and the client itself to the DB.
Instead, I got an error saying that ClientSale with id #123213213 doesn’t exist in the DB.
How to I overcome this? Should I return a list of objects (different types) from my processor (the client + all ClientSale)? Can JPAItemWriter handle a list of objects? Another problem with that is that I’ll have to manually update the client_id in the ClientSale entities instead of adding them to the list and having hibernate to understand the relation between them and who points where..
What’s the best practice here?
Thanks!
OK.. Here is what I did at the end based on everything:
I created a MultiEntityItemWriter that can receive as item a list (and in that case it opens it and writes all elements to the delegated ItemWriter.
Code:
}
Now, my ItemProcessor can output a list with all entities to be written and I don’t need to rely on JPA to understand that there are more entities to be committed to the DB.
hope it helps…