Class JpaItemWriter<T>
- java.lang.Object
- org.springframework.batch.item.database.JpaItemWriter<T>
- All Implemented Interfaces:
ItemWriter<T>
,org.springframework.beans.factory.InitializingBean
public class JpaItemWriter<T> extends java.lang.Object implements ItemWriter<T>, org.springframework.beans.factory.InitializingBean
ItemWriter
that is using a JPA EntityManagerFactory to merge any Entities that aren't part of the persistence context. It is required thatwrite(List)
is called inside a transaction.
The reader must be configured with anEntityManagerFactory
that is capable of participating in Spring managed transactions. The writer is thread-safe after its properties are set (normal singleton behaviour), so it can be used to write in multiple concurrent transactions.- Author:
- Thomas Risberg
Field Summary
Fields Modifier and Type Field Description protected static org.apache.commons.logging.Log
logger
Constructor Summary
Constructors Constructor Description JpaItemWriter()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterPropertiesSet()
Check mandatory properties - there must be an entityManagerFactory.protected void
doWrite(javax.persistence.EntityManager entityManager, java.util.List<? extends T> items)
Do perform the actual write operation.void
setEntityManagerFactory(javax.persistence.EntityManagerFactory entityManagerFactory)
Set the EntityManager to be used internally.void
write(java.util.List<? extends T> items)
Merge all provided items that aren't already in the persistence context and then flush the entity manager.
Method Detail
setEntityManagerFactory
public void setEntityManagerFactory(javax.persistence.EntityManagerFactory entityManagerFactory)
Set the EntityManager to be used internally.- Parameters:
entityManagerFactory
- the entityManagerFactory to set
afterPropertiesSet
public void afterPropertiesSet() throws java.lang.Exception
Check mandatory properties - there must be an entityManagerFactory.- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Throws:
java.lang.Exception
write
public void write(java.util.List<? extends T> items)
Merge all provided items that aren't already in the persistence context and then flush the entity manager.- Specified by:
write
in interfaceItemWriter<T>
- Parameters:
items
- items to be written- See Also:
ItemWriter.write(java.util.List)
doWrite
protected void doWrite(javax.persistence.EntityManager entityManager, java.util.List<? extends T> items)
Do perform the actual write operation. This can be overridden in a subclass if necessary.- Parameters:
entityManager
- the EntityManager to use for the operationitems
- the list of items to use for the write