org.springframework.batch.item.data

## Class RepositoryItemWriter<T>

- java.lang.Object
- - org.springframework.batch.item.data.RepositoryItemWriter<T>

- All Implemented Interfaces:
    ItemWriter<T>, org.springframework.beans.factory.InitializingBean

      public class RepositoryItemWriter<T>
      extends java.lang.Object
      implements 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.

  Since:
    2.2
  Author:
    Michael Minella, Mahmoud Ben Hassine

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected static org.apache.commons.logging.Log` | `logger`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `RepositoryItemWriter()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### logger

          protected static final org.apache.commons.logging.Log logger

  - ### Constructor Detail

    - #### RepositoryItemWriter

          public RepositoryItemWriter()

  - ### Method Detail

    - #### setMethodName

          public void setMethodName(java.lang.String methodName)

      Specifies what method on the repository to call. This method must have the type of object passed to this writer as the sole argument.
      Parameters:
        `methodName` - `String` containing the method name.

    - #### setRepository

          public void setRepository(org.springframework.data.repository.CrudRepository<T,?> repository)

      Set the `CrudRepository` implementation for persistence
      Parameters:
        `repository` - the Spring Data repository to be set

    - #### write

          public void write(java.util.List<? extends T> items)
                     throws java.lang.Exception

      Write all items to the data store via a Spring Data repository.
      Specified by:
        `write` in interface `ItemWriter<T>`
      Parameters:
        `items` - items to be written
      Throws:
        `java.lang.Exception` - if there are errors. The framework will catch the exception and convert or rethrow it as appropriate.
      See Also:
        `ItemWriter.write(java.util.List)`

    - #### doWrite

          protected void doWrite(java.util.List<? extends T> items)
                          throws java.lang.Exception

      Performs the actual write to the repository. This can be overridden by a subclass if necessary.
      Parameters:
        `items` - the list of items to be persisted.
      Throws:
        `java.lang.Exception` - thrown if error occurs during writing.

    - #### afterPropertiesSet

          public void afterPropertiesSet()
                                  throws java.lang.Exception

      Check mandatory properties - there must be a repository.
      Specified by:
        `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`
      Throws:
        `java.lang.Exception`