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).
logger, results
Constructor and Description |
---|
JpaPagingItemReader() |
Modifier and Type | Method and 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(javax.persistence.EntityManagerFactory entityManagerFactory) |
void |
setParameterValues(java.util.Map<java.lang.String,java.lang.Object> parameterValues)
The parameter values to be used for the query execution.
|
void |
setQueryProvider(JpaQueryProvider queryProvider) |
void |
setQueryString(java.lang.String queryString) |
void |
setTransacted(boolean transacted)
By default (true) the EntityTransaction will be started and committed around the read.
|
doRead, getPage, getPageSize, jumpToItem, setPageSize
close, getCurrentItemCount, isSaveState, open, read, setCurrentItemCount, setMaxItemCount, setSaveState, update
getExecutionContextKey, setExecutionContextName, setName
public void setEntityManagerFactory(javax.persistence.EntityManagerFactory entityManagerFactory)
public void setParameterValues(java.util.Map<java.lang.String,java.lang.Object> parameterValues)
parameterValues
- the values keyed by the parameter named used in
the query string.public void setTransacted(boolean transacted)
transacted
- indicatorpublic void afterPropertiesSet() throws java.lang.Exception
AbstractPagingItemReader
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
afterPropertiesSet
in class AbstractPagingItemReader<T>
java.lang.Exception
InitializingBean.afterPropertiesSet()
public void setQueryString(java.lang.String queryString)
queryString
- JPQL query stringpublic void setQueryProvider(JpaQueryProvider queryProvider)
queryProvider
- JPA query providerprotected void doOpen() throws java.lang.Exception
AbstractItemCountingItemStreamItemReader
doOpen
in class AbstractPagingItemReader<T>
java.lang.Exception
- Allows subclasses to throw checked exceptions for interpretation by the frameworkprotected void doReadPage()
doReadPage
in class AbstractPagingItemReader<T>
protected void doJumpToPage(int itemIndex)
doJumpToPage
in class AbstractPagingItemReader<T>
protected void doClose() throws java.lang.Exception
AbstractItemCountingItemStreamItemReader
AbstractItemCountingItemStreamItemReader.doOpen()
.doClose
in class AbstractPagingItemReader<T>
java.lang.Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework