org.springframework.batch.item.database
Class HibernateCursorItemReader

java.lang.Object
  extended by org.springframework.batch.item.ExecutionContextUserSupport
      extended by org.springframework.batch.item.database.HibernateCursorItemReader
All Implemented Interfaces:
ItemReader, ItemStream, InitializingBean

public class HibernateCursorItemReader
extends ExecutionContextUserSupport
implements ItemReader, ItemStream, InitializingBean

ItemReader for reading database records built on top of Hibernate. It executes the HQL setQueryString(String) when initialized and iterates over the result set as read() method is called, returning an object corresponding to current row. Input source 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 after successful commit without being flushed (no inserts or updates are expected).

Author:
Robert Kasanicky, Dave Syer

Constructor Summary
HibernateCursorItemReader()
           
 
Method Summary
 void afterPropertiesSet()
           
 void close(ExecutionContext executionContext)
          Closes the result set cursor and hibernate session.
 void mark()
          Mark is supported as long as this ItemStream is used in a single-threaded environment.
 void open(ExecutionContext executionContext)
          Creates cursor for the query.
 Object read()
          Reads a piece of input data and advance to the next one.
 void reset()
          Reset the stream to the last mark.
 void setQueryString(String queryString)
           
 void setSaveState(boolean saveState)
           
 void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
           
 void setUseStatelessSession(boolean useStatelessSession)
          Can be set only in uninitialized state.
 void update(ExecutionContext executionContext)
          Indicates that the execution context provided during open is about to be saved.
 
Methods inherited from class org.springframework.batch.item.ExecutionContextUserSupport
getKey, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateCursorItemReader

public HibernateCursorItemReader()
Method Detail

read

public Object read()
Description copied from interface: ItemReader
Reads a piece of input data and advance to the next one. Implementations must return null at the end of the input data set. In a transactional setting, caller might get the same item twice from successive calls (or otherwise), if the first call was in a transaction that rolled back.

Specified by:
read in interface ItemReader

close

public void close(ExecutionContext executionContext)
Closes the result set cursor and hibernate session.

Specified by:
close in interface ItemStream
Parameters:
executionContext - TODO

open

public void open(ExecutionContext executionContext)
Creates cursor for the query.

Specified by:
open in interface ItemStream

setSessionFactory

public void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
Parameters:
sessionFactory - hibernate session factory

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

setQueryString

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

setUseStatelessSession

public void setUseStatelessSession(boolean useStatelessSession)
Can be set only in uninitialized state.

Parameters:
useStatelessSession - true to use StatelessSession false to use standard hibernate Session

update

public void update(ExecutionContext executionContext)
Description copied from interface: ItemStream
Indicates that the execution context provided during open is about to be saved. If any state is remaining, but has not been put in the context, it should be added here.

Specified by:
update in interface ItemStream
Parameters:
executionContext - to be updated

mark

public void mark()
Mark is supported as long as this ItemStream is used in a single-threaded environment. The state backing the mark is a single counter, keeping track of the current position, so multiple threads cannot be accommodated.

Specified by:
mark in interface ItemReader
See Also:
ItemReader.mark()

reset

public void reset()
Description copied from interface: ItemReader
Reset the stream to the last mark. After a reset the stream state will be such that changes (items read or written) since the last call to mark will not be visible after a call to close.
In a multi-threaded setting implementations have to ensure that only the state from the current thread is reset.

Specified by:
reset in interface ItemReader

setSaveState

public void setSaveState(boolean saveState)


Copyright © 2008 SpringSource. All Rights Reserved.