Class HibernateCursorItemReader<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.HibernateCursorItemReader<T>
- All Implemented Interfaces:
ItemReader<T>
,ItemStream
,ItemStreamReader<T>
,org.springframework.beans.factory.InitializingBean
public class HibernateCursorItemReader<T>
extends AbstractItemCountingItemStreamItemReader<T>
implements org.springframework.beans.factory.InitializingBean
ItemStreamReader
for reading database records built on top of Hibernate. It
executes the HQL query when initialized iterates over the result set as AbstractItemCountingItemStreamItemReader.read()
method is called, returning an object corresponding to current row. The query can be
set directly using setQueryString(String)
, a named query can be used by
setQueryName(String)
, or a query provider strategy can be supplied via
setQueryProvider(HibernateQueryProvider)
.
The reader can be configured to use either StatelessSession
sufficient for
simple mappings without the need to cascade to associated objects or standard hibernate
Session
for more advanced mappings or when caching is desired. When stateful
session is used it will be cleared in the update(ExecutionContext)
method
without being flushed (no data modifications are expected).
- Author:
- Robert Kasanicky, Dave Syer, Mahmoud Ben Hassine
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
protected void
doClose()
Close the cursor and hibernate session.protected void
doOpen()
Open hibernate session and create a forward-only cursor for the query.protected T
doRead()
Read next item from input.protected void
jumpToItem
(int itemIndex) Wind forward through the result set to the item requested.void
setFetchSize
(int fetchSize) Fetch size used internally by Hibernate to limit amount of data fetched from database per round trip.void
setParameterValues
(Map<String, Object> parameterValues) The parameter values to apply to a query (map of name:value).void
setQueryName
(String queryName) A query name for an externalized query.void
setQueryProvider
(HibernateQueryProvider<T> queryProvider) A query provider.void
setQueryString
(String queryString) A query string in HQL.void
setSessionFactory
(org.hibernate.SessionFactory sessionFactory) The Hibernate SessionFactory to use the create a session.void
setUseStatelessSession
(boolean useStatelessSession) Can be set only in uninitialized state.void
update
(ExecutionContext executionContext) Update the context and clear the session if stateful.Methods inherited from class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
close, getCurrentItemCount, isSaveState, open, read, setCurrentItemCount, setMaxItemCount, setSaveState
Methods inherited from class org.springframework.batch.item.ItemStreamSupport
getExecutionContextKey, setExecutionContextName, setName
-
Constructor Details
-
HibernateCursorItemReader
public HibernateCursorItemReader()
-
-
Method Details
-
afterPropertiesSet
- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Throws:
Exception
-
setParameterValues
The parameter values to apply to a query (map of name:value).- Parameters:
parameterValues
- the parameter values to set
-
setQueryName
A query name for an externalized query. Either this or the {query string
or the {query provider
should be set.- Parameters:
queryName
- name of a hibernate named query
-
setFetchSize
public void setFetchSize(int fetchSize) Fetch size used internally by Hibernate to limit amount of data fetched from database per round trip.- Parameters:
fetchSize
- the fetch size to pass down to Hibernate
-
setQueryProvider
A query provider. Either this or the {query string
or the {query name
should be set.- Parameters:
queryProvider
- Hibernate query provider
-
setQueryString
A query string in HQL. Either this or the {query provider
or the {query name
should be set.- Parameters:
queryString
- HQL query string
-
setSessionFactory
public void setSessionFactory(org.hibernate.SessionFactory sessionFactory) The Hibernate SessionFactory to use the create a session.- Parameters:
sessionFactory
- theSessionFactory
to set
-
setUseStatelessSession
public void setUseStatelessSession(boolean useStatelessSession) Can be set only in uninitialized state.- Parameters:
useStatelessSession
-true
to useStatelessSession
false
to use standard hibernateSession
-
doRead
Description copied from class:AbstractItemCountingItemStreamItemReader
Read next item from input.- Specified by:
doRead
in classAbstractItemCountingItemStreamItemReader<T>
- Returns:
- an item or
null
if the data source is exhausted - Throws:
Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework
-
doOpen
Open hibernate session and create a forward-only cursor for the query.- Specified by:
doOpen
in classAbstractItemCountingItemStreamItemReader<T>
- Throws:
Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework
-
update
Update the context and clear the session if stateful.- Specified by:
update
in interfaceItemStream
- Overrides:
update
in classAbstractItemCountingItemStreamItemReader<T>
- Parameters:
executionContext
- the currentExecutionContext
- Throws:
ItemStreamException
- if there is a problem- See Also:
-
jumpToItem
Wind forward through the result set to the item requested. Also clears the session every now and then (if stateful) to avoid memory problems. The frequency of session clearing is the larger of the fetch size (if set) and 100.- Overrides:
jumpToItem
in classAbstractItemCountingItemStreamItemReader<T>
- Parameters:
itemIndex
- the first item to read- Throws:
Exception
- if there is a problem- See Also:
-
doClose
Close the cursor and hibernate session.- Specified by:
doClose
in classAbstractItemCountingItemStreamItemReader<T>
- Throws:
Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework
-