org.springframework.batch.item.database
Class HibernateAwareItemWriter

java.lang.Object
  extended by org.springframework.batch.item.database.AbstractTransactionalResourceItemWriter
      extended by org.springframework.batch.item.database.HibernateAwareItemWriter
All Implemented Interfaces:
ItemWriter, InitializingBean

public class HibernateAwareItemWriter
extends AbstractTransactionalResourceItemWriter
implements InitializingBean

ItemWriter that is aware of the Hibernate session and can take some responsibilities to do with chunk boundaries away from a less smart ItemWriter (the delegate). A delegate is required, and will be used to do the actual writing of the item.
It is expected that AbstractTransactionalResourceItemWriter.write(Object) is called inside a transaction, and that AbstractTransactionalResourceItemWriter.flush() is then subsequently called before the transaction commits, or AbstractTransactionalResourceItemWriter.clear() before it rolls back.
The writer is thread safe after its properties are set (normal singleton behaviour), so it can be used to write in multiple concurrent transactions. Note, however, that the set of failed items is stored in a collection internally, and this collection is never cleared, so it is not a great idea to go on using the writer indefinitely. Normally it would be used for the duration of a batch job and then discarded.

Author:
Dave Syer

Constructor Summary
HibernateAwareItemWriter()
           
 
Method Summary
 void afterPropertiesSet()
          Check mandatory properties - there must be a delegate and hibernateTemplate.
protected  void doClear()
          Call the delegate clear() method, and then clear the hibernate session.
protected  void doFlush()
          Delegate to subclass and flush the hibernate session.
protected  void doWrite(Object output)
          Callback method of AbstractTransactionalResourceItemWriter.write(Object).
protected  String getResourceKey()
           
 void setDelegate(ItemWriter delegate)
          Public setter for the ItemWriter property.
 void setHibernateTemplate(HibernateOperations hibernateTemplate)
          Public setter for the HibernateOperations property.
 void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
          Set the Hibernate SessionFactory to be used internally.
 
Methods inherited from class org.springframework.batch.item.database.AbstractTransactionalResourceItemWriter
clear, flush, getProcessed, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateAwareItemWriter

public HibernateAwareItemWriter()
Method Detail

setDelegate

public void setDelegate(ItemWriter delegate)
Public setter for the ItemWriter property.

Parameters:
delegate - the delegate to set

setHibernateTemplate

public void setHibernateTemplate(HibernateOperations hibernateTemplate)
Public setter for the HibernateOperations property.

Parameters:
hibernateTemplate - the hibernateTemplate to set

setSessionFactory

public final void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
Set the Hibernate SessionFactory to be used internally. Will automatically create a HibernateTemplate for the given SessionFactory.

See Also:
setHibernateTemplate(org.springframework.orm.hibernate3.HibernateOperations)

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Check mandatory properties - there must be a delegate and hibernateTemplate.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

doFlush

protected void doFlush()
Delegate to subclass and flush the hibernate session.

Specified by:
doFlush in class AbstractTransactionalResourceItemWriter

doClear

protected void doClear()
                throws ClearFailedException
Call the delegate clear() method, and then clear the hibernate session.

Specified by:
doClear in class AbstractTransactionalResourceItemWriter
Throws:
ClearFailedException

getResourceKey

protected String getResourceKey()
Specified by:
getResourceKey in class AbstractTransactionalResourceItemWriter
Returns:
Key for items processed in the current transaction RepeatContext.

doWrite

protected void doWrite(Object output)
                throws Exception
Description copied from class: AbstractTransactionalResourceItemWriter
Callback method of AbstractTransactionalResourceItemWriter.write(Object).

Specified by:
doWrite in class AbstractTransactionalResourceItemWriter
Throws:
Exception


Copyright © 2009 SpringSource. All Rights Reserved.