Package org.springframework.batch.item.database

# Class JpaCursorItemReader<T>

java.lang.Object
org.springframework.batch.item.ItemStreamSupport
org.springframework.batch.item.support.AbstractItemStreamItemReader<T>
org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
org.springframework.batch.item.database.JpaCursorItemReader<T>

Type Parameters:
  `T` - type of items to read

All Implemented Interfaces:
  `ItemReader<T>`, `ItemStream`, `ItemStreamReader<T>`, `org.springframework.beans.factory.InitializingBean`

public class JpaCursorItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements org.springframework.beans.factory.InitializingBean
`ItemStreamReader` implementation based on JPA `Query.getResultStream()`. It executes the JPQL query when initialized and iterates over the result set as `AbstractItemCountingItemStreamItemReader.read()` method is called, returning an object corresponding to the current row. The query can be set directly using `setQueryString(String)`, or using a query provider via `setQueryProvider(JpaQueryProvider)`. The implementation is not thread-safe.

Since:
  4.3
Author:
  Mahmoud Ben Hassine

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `JpaCursorItemReader()`
  Create a new `JpaCursorItemReader`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `afterPropertiesSet()`
  `protected void`
  `doClose()`
  Close the resources opened in `AbstractItemCountingItemStreamItemReader.doOpen()`.
  `protected void`
  `doOpen()`
  Open resources necessary to start reading input.
  `protected T`
  `doRead()`
  Read next item from input.
  `void`
  `setEntityManagerFactory(jakarta.persistence.EntityManagerFactory entityManagerFactory)`
  Set the JPA entity manager factory.
  `void`
  `setParameterValues(Map<String,Object> parameterValues)`
  Set the parameter values to be used for the query execution.
  `void`
  `setQueryProvider(JpaQueryProvider queryProvider)`
  Set the JPA query provider.
  `void`
  `setQueryString(String queryString)`
  Set the JPQL query string.
  `void`
  `update(ExecutionContext executionContext)`
  Return empty `ExecutionContext`.

  ### Methods inherited from class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader

  `close, getCurrentItemCount, isSaveState, jumpToItem, open, read, setCurrentItemCount, setMaxItemCount, setSaveState`

  ### Methods inherited from class org.springframework.batch.item.ItemStreamSupport

  `getExecutionContextKey, setExecutionContextName, setName`

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

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

- ## Constructor Details

  - ### JpaCursorItemReader

    public JpaCursorItemReader()
    Create a new `JpaCursorItemReader`.

- ## Method Details

  - ### setEntityManagerFactory

    public void setEntityManagerFactory(jakarta.persistence.EntityManagerFactory entityManagerFactory)
    Set the JPA entity manager factory.
    Parameters:
      `entityManagerFactory` - JPA entity manager factory

  - ### setQueryProvider

    public void setQueryProvider(JpaQueryProvider queryProvider)
    Set the JPA query provider.
    Parameters:
      `queryProvider` - JPA query provider

  - ### setQueryString

    public void setQueryString(String queryString)
    Set the JPQL query string.
    Parameters:
      `queryString` - JPQL query string

  - ### setParameterValues

    public void setParameterValues(Map<String,Object> parameterValues)
    Set the parameter values to be used for the query execution.
    Parameters:
      `parameterValues` - the values keyed by parameter names used in the query string.

  - ### afterPropertiesSet

    public void afterPropertiesSet() throws Exception
    Specified by:
      `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`
    Throws:
      `Exception`

  - ### doOpen

    protected void doOpen() throws Exception
    Description copied from class: `AbstractItemCountingItemStreamItemReader`
    Open resources necessary to start reading input.
    Specified by:
      `doOpen` in class `AbstractItemCountingItemStreamItemReader<T>`
    Throws:
      `Exception` - Allows subclasses to throw checked exceptions for interpretation by the framework

  - ### doRead

    protected T doRead()
    Description copied from class: `AbstractItemCountingItemStreamItemReader`
    Read next item from input.
    Specified by:
      `doRead` in class `AbstractItemCountingItemStreamItemReader<T>`
    Returns:
      an item or `null` if the data source is exhausted

  - ### update

    public void update(ExecutionContext executionContext) throws ItemStreamException
    Description copied from class: `ItemStreamSupport`
    Return empty `ExecutionContext`.
    Specified by:
      `update` in interface `ItemStream`
    Overrides:
      `update` in class `AbstractItemCountingItemStreamItemReader<T>`
    Parameters:
      `executionContext` - to be updated
    Throws:
      `ItemStreamException`
    See Also:
      - `ItemStream.update(ExecutionContext)`

  - ### doClose

    protected void doClose()
    Description copied from class: `AbstractItemCountingItemStreamItemReader`
    Close the resources opened in `AbstractItemCountingItemStreamItemReader.doOpen()`.
    Specified by:
      `doClose` in class `AbstractItemCountingItemStreamItemReader<T>`