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 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 that write(Chunk)
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, Mahmoud Ben Hassine, Jinwoo Bae
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Check mandatory properties - there must be an entityManagerFactory.protected void
Do perform the actual write operation.void
setClearPersistenceContext
(boolean clearPersistenceContext) Flag to indicate that the persistence context should be cleared and flushed at the end of the write (default true).void
setEntityManagerFactory
(jakarta.persistence.EntityManagerFactory entityManagerFactory) Set the EntityManager to be used internally.void
setUsePersist
(boolean usePersist) Set whether the EntityManager should perform a persist instead of a merge.void
Merge all provided items that aren't already in the persistence context and then flush the entity manager.
-
Field Details
-
logger
protected static final org.apache.commons.logging.Log logger
-
-
Constructor Details
-
JpaItemWriter
public JpaItemWriter()
-
-
Method Details
-
setEntityManagerFactory
public void setEntityManagerFactory(jakarta.persistence.EntityManagerFactory entityManagerFactory) Set the EntityManager to be used internally.- Parameters:
entityManagerFactory
- the entityManagerFactory to set
-
setUsePersist
public void setUsePersist(boolean usePersist) Set whether the EntityManager should perform a persist instead of a merge.- Parameters:
usePersist
- whether to use persist instead of merge.
-
setClearPersistenceContext
public void setClearPersistenceContext(boolean clearPersistenceContext) Flag to indicate that the persistence context should be cleared and flushed at the end of the write (default true).- Parameters:
clearPersistenceContext
- the flag value to set- Since:
- 5.1
-
afterPropertiesSet
Check mandatory properties - there must be an entityManagerFactory.- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Throws:
Exception
-
write
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
- of items to be written. Must not benull
.- See Also:
-
doWrite
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
-