Package org.springframework.batch.item.database

# Class JpaPagingItemReader<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.AbstractPagingItemReader<T>
org.springframework.batch.item.database.JpaPagingItemReader<T>

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

public class JpaPagingItemReader<T> extends AbstractPagingItemReader<T>

`ItemReader` for reading database records built on top of JPA.

It executes the JPQL `setQueryString(String)` to retrieve requested data. The query is executed using paged requests of a size specified in `AbstractPagingItemReader.setPageSize(int)`. Additional pages are requested when needed as `AbstractItemCountingItemStreamItemReader.read()` method is called, returning an object corresponding to current position.

The performance of the paging depends on the JPA implementation and its use of database specific features to limit the number of returned rows.

Setting a fairly large page size and using a commit interval that matches the page size should provide better performance.

In order to reduce the memory usage for large results the persistence context is flushed and cleared after each page is read. This causes any entities read to be detached. If you make changes to the entities and want the changes persisted then you must explicitly merge the entities.

The reader must be configured with an `EntityManagerFactory`. All entity access is performed within a new transaction, independent of any existing Spring managed transactions.

The implementation is thread-safe in between calls to `AbstractItemCountingItemStreamItemReader.open(ExecutionContext)`, but remember to use `saveState=false` if used in a multi-threaded client (no restart available).

Since:
  2.0
Author:
  Thomas Risberg, Dave Syer, Will Schipp, Mahmoud Ben Hassine

- ## Field Summary

  ### Fields inherited from class org.springframework.batch.item.database.AbstractPagingItemReader

  `logger, results`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `JpaPagingItemReader()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `afterPropertiesSet()`
  Check mandatory properties.
  `protected void`
  `doClose()`
  Close the resources opened in `AbstractItemCountingItemStreamItemReader.doOpen()`.
  `protected void`
  `doJumpToPage(int itemIndex)`
  `protected void`
  `doOpen()`
  Open resources necessary to start reading input.
  `protected void`
  `doReadPage()`
  `void`
  `setEntityManagerFactory(jakarta.persistence.EntityManagerFactory entityManagerFactory)`
  `void`
  `setParameterValues(Map<String,Object> parameterValues)`
  The parameter values to be used for the query execution.
  `void`
  `setQueryProvider(JpaQueryProvider queryProvider)`
  `void`
  `setQueryString(String queryString)`
  `void`
  `setTransacted(boolean transacted)`
  By default (true) the EntityTransaction will be started and committed around the read.

  ### Methods inherited from class org.springframework.batch.item.database.AbstractPagingItemReader

  `doRead, getPage, getPageSize, jumpToItem, setPageSize`

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

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

  ### 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

  - ### JpaPagingItemReader

    public JpaPagingItemReader()

- ## Method Details

  - ### setEntityManagerFactory

    public void setEntityManagerFactory(jakarta.persistence.EntityManagerFactory entityManagerFactory)

  - ### setParameterValues

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

  - ### setTransacted

    public void setTransacted(boolean transacted)
    By default (true) the EntityTransaction will be started and committed around the read. Can be overridden (false) in cases where the JPA implementation doesn't support a particular transaction. (e.g. Hibernate with a JTA transaction). NOTE: may cause problems in guaranteeing the object consistency in the EntityManagerFactory.
    Parameters:
      `transacted` - indicator

  - ### afterPropertiesSet

    public void afterPropertiesSet() throws Exception
    Description copied from class: `AbstractPagingItemReader`
    Check mandatory properties.
    Specified by:
      `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`
    Overrides:
      `afterPropertiesSet` in class `AbstractPagingItemReader<T>`
    Throws:
      `Exception`
    See Also:
      - `InitializingBean.afterPropertiesSet()`

  - ### setQueryString

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

  - ### setQueryProvider

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

  - ### doOpen

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

  - ### doReadPage

    protected void doReadPage()
    Specified by:
      `doReadPage` in class `AbstractPagingItemReader<T>`

  - ### doJumpToPage

    protected void doJumpToPage(int itemIndex)
    Specified by:
      `doJumpToPage` in class `AbstractPagingItemReader<T>`

  - ### doClose

    protected void doClose() throws Exception
    Description copied from class: `AbstractItemCountingItemStreamItemReader`
    Close the resources opened in `AbstractItemCountingItemStreamItemReader.doOpen()`.
    Overrides:
      `doClose` in class `AbstractPagingItemReader<T>`
    Throws:
      `Exception` - Allows subclasses to throw checked exceptions for interpretation by the framework