public class RepositoryItemWriter<T> extends java.lang.Object implements ItemWriter<T>, org.springframework.beans.factory.InitializingBean
A ItemReader
wrapper for a
CrudRepository
from Spring Data.
It depends on CrudRepository.saveAll(Iterable)
method to store the items for the chunk. Performance will be determined by that
implementation more than this writer.
As long as the repository provided is thread-safe, this writer is also thread-safe once properties are set (normal singleton behavior), so it can be used in multiple concurrent transactions.
NOTE: The RepositoryItemWriter
only stores Java Objects i.e. non primitives.
Modifier and Type | Field and Description |
---|---|
protected static org.apache.commons.logging.Log |
logger |
Constructor and Description |
---|
RepositoryItemWriter() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Check mandatory properties - there must be a repository.
|
protected void |
doWrite(java.util.List<? extends T> items)
Performs the actual write to the repository.
|
void |
setMethodName(java.lang.String methodName)
Specifies what method on the repository to call.
|
void |
setRepository(org.springframework.data.repository.CrudRepository<T,?> repository)
Set the
CrudRepository implementation
for persistence |
void |
write(java.util.List<? extends T> items)
Write all items to the data store via a Spring Data repository.
|
public void setMethodName(java.lang.String methodName)
methodName
- String
containing the method name.public void setRepository(org.springframework.data.repository.CrudRepository<T,?> repository)
CrudRepository
implementation
for persistencerepository
- the Spring Data repository to be setpublic void write(java.util.List<? extends T> items) throws java.lang.Exception
write
in interface ItemWriter<T>
items
- items to be writtenjava.lang.Exception
- if there are errors. The framework will catch the
exception and convert or rethrow it as appropriate.ItemWriter.write(java.util.List)
protected void doWrite(java.util.List<? extends T> items) throws java.lang.Exception
items
- the list of items to be persisted.java.lang.Exception
- thrown if error occurs during writing.public void afterPropertiesSet() throws java.lang.Exception
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
java.lang.Exception