org.springframework.batch.item.database
Class JpaItemWriter<T>

java.lang.Object
  extended by org.springframework.batch.item.database.JpaItemWriter<T>
All Implemented Interfaces:
ItemWriter<T>, InitializingBean

public class JpaItemWriter<T>
extends Object
implements ItemWriter<T>, InitializingBean

ItemWriter that is using a JPA EntityManagerFactory to merge any Entities that aren't part of the persistence context. It is required that write(List) is called inside a transaction.
The reader must be configured with an EntityManagerFactory 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
protected static Log logger
           
 
Constructor Summary
JpaItemWriter()
           
 
Method Summary
 void afterPropertiesSet()
          Check mandatory properties - there must be an entityManagerFactory.
protected  void doWrite(EntityManager entityManager, List<? extends T> items)
          Do perform the actual write operation.
 void setEntityManagerFactory(EntityManagerFactory entityManagerFactory)
          Set the EntityManager to be used internally.
 void write(List<? extends T> items)
          Merge all provided items that aren't already in the persistence context and then flush the entity manager.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static final Log logger
Constructor Detail

JpaItemWriter

public JpaItemWriter()
Method Detail

setEntityManagerFactory

public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory)
Set the EntityManager to be used internally.

Parameters:
entityManagerFactory - the entityManagerFactory to set

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Check mandatory properties - there must be an entityManagerFactory.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

write

public final void write(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 interface ItemWriter<T>
See Also:
ItemWriter.write(java.util.List)

doWrite

protected void doWrite(EntityManager entityManager,
                       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 operation
items - the list of items to use for the write


Copyright © 2013 SpringSource. All Rights Reserved.