org.springframework.batch.item.database
Class HibernateCursorItemReader<T>

java.lang.Object
  extended by org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
      extended by org.springframework.batch.item.database.HibernateCursorItemReader<T>
All Implemented Interfaces:
ItemReader<T>, ItemStream, InitializingBean

public class HibernateCursorItemReader<T>
extends AbstractItemCountingItemStreamItemReader<T>
implements ItemStream, InitializingBean

ItemReader 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) or a named query can be used by setQueryName(String).

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).

The implementation is not thread-safe.

Author:
Robert Kasanicky, Dave Syer

Constructor Summary
HibernateCursorItemReader()
           
 
Method Summary
 void afterPropertiesSet()
           
protected  void doClose()
          Close the cursor and hibernate session.
protected  void doOpen()
          Open hibernate session and create a forward-only cursor for the setQueryString(String).
protected  T doRead()
          Read next item from input.
 void setFetchSize(int fetchSize)
          Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for this ResultSet object.
 void setQueryName(String queryName)
           
 void setQueryString(String queryString)
           
 void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
           
 void setUseStatelessSession(boolean useStatelessSession)
          Can be set only in uninitialized state.
 void update(ExecutionContext executionContext)
          Clears the session if not stateful and delegates to super class.
 
Methods inherited from class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
close, getCurrentItemCount, jumpToItem, open, read, setCurrentItemCount, setMaxItemCount, setName, setSaveState
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.batch.item.ItemStream
close, open
 

Constructor Detail

HibernateCursorItemReader

public HibernateCursorItemReader()
Method Detail

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

setQueryName

public void setQueryName(String queryName)
Parameters:
queryName - name of a hibernate named query

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)
            throws ItemStreamException
Clears the session if not stateful and delegates to super class.

Specified by:
update in interface ItemStream
Overrides:
update in class AbstractItemCountingItemStreamItemReader<T>
Parameters:
executionContext - to be updated
Throws:
ItemStreamException

setFetchSize

public void setFetchSize(int fetchSize)
Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for this ResultSet object. If the fetch size specified is zero, the JDBC driver ignores the value.

Parameters:
fetchSize - the number of rows to fetch, 0 by default
See Also:
Query.setFetchSize(int)

doRead

protected T doRead()
            throws Exception
Description copied from class: AbstractItemCountingItemStreamItemReader
Read next item from input.

Specified by:
doRead in class AbstractItemCountingItemStreamItemReader<T>
Returns:
item
Throws:
Exception

doOpen

protected void doOpen()
               throws Exception
Open hibernate session and create a forward-only cursor for the setQueryString(String).

Specified by:
doOpen in class AbstractItemCountingItemStreamItemReader<T>
Throws:
Exception

doClose

protected void doClose()
                throws Exception
Close the cursor and hibernate session.

Specified by:
doClose in class AbstractItemCountingItemStreamItemReader<T>
Throws:
Exception


Copyright © 2009 SpringSource. All Rights Reserved.