Class RepositoryItemWriter<T>
- All Implemented Interfaces:
ItemWriter<T>, org.springframework.beans.factory.InitializingBean
A ItemWriter wrapper for a
CrudRepository from Spring Data.
By default, this writer will use CrudRepository.saveAll(Iterable) to save
items, unless another method is selected with setMethodName(java.lang.String).
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.
If the underlying repository needs to be flushed after writing (for example
JpaRepository), this can be achieved in one of two ways: if the repository
exposes a single method that both saves and flushes items, such as
JpaRepository#saveAllAndFlush(Iterable), that method name can be set with
setMethodName(java.lang.String) and it will be invoked once with the whole
chunk. Otherwise, if saving and flushing are separate operations, flush() can
be overridden in a subclass to perform the flush after the chunk has been written.
- Since:
- 2.2
- Author:
- Michael Minella, Mahmoud Ben Hassine
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final org.apache.commons.logging.Logprotected org.springframework.data.repository.CrudRepository<T, ?> -
Constructor Summary
ConstructorsConstructorDescriptionRepositoryItemWriter(org.springframework.data.repository.CrudRepository<T, ?> repository) Create a newRepositoryItemWriterinstance with the provided repository. -
Method Summary
Modifier and TypeMethodDescriptionvoidCheck mandatory properties - there must be a repository.protected voidPerforms the actual write to the repository.protected voidflush()Flush the repository if necessary.voidsetMethodName(String methodName) Specifies what method on the repository to call.voidsetRepository(org.springframework.data.repository.CrudRepository<T, ?> repository) Deprecated, for removal: This API element is subject to removal in a future version.since 6.1 in favor of passing the repository to the constructor.voidWrite all items to the data store via a Spring Data repository.
-
Field Details
-
logger
protected static final org.apache.commons.logging.Log logger -
repository
-
-
Constructor Details
-
RepositoryItemWriter
Create a newRepositoryItemWriterinstance with the provided repository.- Parameters:
repository- the Spring Data repository to be used for persistence.- Since:
- 6.0
-
-
Method Details
-
setMethodName
Specifies what method on the repository to call. This method must either accept the type of object passed to this writer as its sole argument, in which case it is invoked once per item, or accept anIterableas its sole argument (for examplesaveAllorsaveAllAndFlush), in which case it is invoked once with the whole chunk.- Parameters:
methodName-Stringcontaining the method name.
-
setRepository
@Deprecated(since="6.1", forRemoval=true) public void setRepository(org.springframework.data.repository.CrudRepository<T, ?> repository) Deprecated, for removal: This API element is subject to removal in a future version.since 6.1 in favor of passing the repository to the constructor. Scheduled for removal in 7.0.Set theCrudRepositoryimplementation for persistence.- Parameters:
repository- the Spring Data repository to be set
-
write
Write all items to the data store via a Spring Data repository.- Specified by:
writein interfaceItemWriter<T>- Parameters:
chunk- of items to be written. Nevernullbut may be empty.- Throws:
Exception- if there are errors. The framework will catch the exception and convert or rethrow it as appropriate.- See Also:
-
flush
protected void flush()Flush the repository if necessary. This method is a no-op by default, but can be overridden by a subclass to flush the underlying repository when it exposes a separate flush operation, rather than a single method that both saves and flushes (such asJpaRepository#saveAllAndFlush, which can be used directly viasetMethodName(String)).- Since:
- 6.1
-
doWrite
-
afterPropertiesSet
-