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

java.lang.Object
  extended by org.springframework.batch.item.database.HibernateItemWriter<T>
All Implemented Interfaces:
ItemWriter<T>, InitializingBean

public class HibernateItemWriter<T>
extends Object
implements ItemWriter<T>, InitializingBean

ItemWriter that uses a Hibernate session to save or update entities that are not part of the current Hibernate session. It will also flush and clear the session at chunk boundaries.

The writer is thread safe after its properties are set (normal singleton behavior), so it can be used to write in multiple concurrent transactions.

Author:
Dave Syer, Thomas Risberg

Field Summary
protected static Log logger
           
 
Constructor Summary
HibernateItemWriter()
           
 
Method Summary
 void afterPropertiesSet()
          Check mandatory properties - there must be a hibernateTemplate.
protected  void doWrite(HibernateOperations hibernateTemplate, List<? extends T> items)
          Do perform the actual write operation.
 void setHibernateTemplate(HibernateOperations hibernateTemplate)
          Public setter for the HibernateOperations property.
 void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
          Set the Hibernate SessionFactory to be used internally.
 void write(List<? extends T> items)
          Save or update any entities not in the current hibernate session and then flush and clear the hibernate session.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static final Log logger
Constructor Detail

HibernateItemWriter

public HibernateItemWriter()
Method Detail

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

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

write

public final void write(List<? extends T> items)
                 throws Exception
Save or update any entities not in the current hibernate session and then flush and clear the hibernate session.

Specified by:
write in interface ItemWriter<T>
Throws:
Exception - if there are errors. The framework will catch the exception and convert or rethrow it as appropriate.
See Also:
ItemWriter.write(java.util.List)

doWrite

protected void doWrite(HibernateOperations hibernateTemplate,
                       List<? extends T> items)
Do perform the actual write operation. This can be overridden in a subclass if necessary.

Parameters:
hibernateTemplate - the HibernateTemplate to use for the operation
items - the list of items to use for the write


Copyright © 2009 SpringSource. All Rights Reserved.