BATCH-1904: Updated to support Hibernate 4

This commit is contained in:
Michael Minella
2012-12-26 15:05:06 -06:00
parent 1824fff441
commit 6955ab1d6b
17 changed files with 237 additions and 99 deletions

View File

@@ -19,11 +19,12 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.context.spi.CurrentSessionContext;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.orm.hibernate3.HibernateOperations;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.util.Assert;
/**
@@ -34,11 +35,21 @@ import org.springframework.util.Assert;
* default (see {@link #setClearSession(boolean) clearSession} property).<br/>
* <br/>
*
<<<<<<< HEAD
* 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
* @author Thomas Risberg
=======
* The writer is thread safe once properties are set (normal singleton behavior)
* if a {@link CurrentSessionContext} that uses only one session per thread is
* used.
*
* @author Dave Syer
* @author Thomas Risberg
* @author Michael Minella
>>>>>>> BATCH-1904: Updated to support Hibernate 4
*
*/
public class HibernateItemWriter<T> implements ItemWriter<T>, InitializingBean {
@@ -47,6 +58,7 @@ public class HibernateItemWriter<T> implements ItemWriter<T>, InitializingBean {
.getLog(HibernateItemWriter.class);
private HibernateOperations hibernateTemplate;
private SessionFactory sessionFactory;
private boolean clearSession = true;
@@ -66,19 +78,19 @@ public class HibernateItemWriter<T> implements ItemWriter<T>, InitializingBean {
*
* @param hibernateTemplate
* the hibernateTemplate to set
* @deprecated As of 2.2 in favor of using Hibernate's session management APIs directly
*/
public void setHibernateTemplate(HibernateOperations hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
/**
* Set the Hibernate SessionFactory to be used internally. Will
* automatically create a HibernateTemplate for the given SessionFactory.
* Set the Hibernate SessionFactory to be used internally.
*
* @see #setHibernateTemplate
* @param sessionFactory session factory to be used by the writer
*/
public final void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
this.sessionFactory = sessionFactory;
}
/**
@@ -86,8 +98,8 @@ public class HibernateItemWriter<T> implements ItemWriter<T>, InitializingBean {
*/
@Override
public void afterPropertiesSet() {
Assert.notNull(hibernateTemplate,
"HibernateItemWriter requires a HibernateOperations");
Assert.state(!(hibernateTemplate == null && sessionFactory == null),
"Either HibernateOperations or SessionFactory must be provided");
}
/**
@@ -98,21 +110,70 @@ public class HibernateItemWriter<T> implements ItemWriter<T>, InitializingBean {
*/
@Override
public final void write(List<? extends T> items) {
doWrite(hibernateTemplate, items);
hibernateTemplate.flush();
if (clearSession) {
hibernateTemplate.clear();
if(sessionFactory == null) {
doWrite(hibernateTemplate, items);
hibernateTemplate.flush();
if (clearSession) {
hibernateTemplate.clear();
}
}
else {
doWrite(sessionFactory, items);
sessionFactory.getCurrentSession().flush();
if(clearSession) {
sessionFactory.getCurrentSession().clear();
}
}
}
/**
* Do perform the actual write operation. This can be overridden in a
* subclass if necessary.
* Do perform the actual write operation using Hibernate's API.
* This can be overridden in a subclass if necessary.
*
* @param hibernateTemplate
* the HibernateTemplate to use for the operation
* @param items
* the list of items to use for the write
* @deprecated As of 2.2 in favor of using Hibernate's session management APIs directly
*/
protected void doWrite(SessionFactory sessionFactory, List<? extends T> items) {
if (logger.isDebugEnabled()) {
logger.debug("Writing to Hibernate with " + items.size()
+ " items.");
}
Session currentSession = sessionFactory.getCurrentSession();
if (!items.isEmpty()) {
long saveOrUpdateCount = 0;
for (T item : items) {
if (!currentSession.contains(item)) {
currentSession.saveOrUpdate(item);
saveOrUpdateCount++;
}
}
if (logger.isDebugEnabled()) {
logger.debug(saveOrUpdateCount + " entities saved/updated.");
logger.debug((items.size() - saveOrUpdateCount)
+ " entities found in session.");
}
}
}
/**
<<<<<<< HEAD
* Do perform the actual write operation. This can be overridden in a
* subclass if necessary.
=======
* Do perform the actual write operation using {@link HibernateOperations}.
* This can be overridden in a subclass if necessary.
>>>>>>> BATCH-1904: Updated to support Hibernate 4
*
* @param hibernateTemplate
* the HibernateTemplate to use for the operation
* @param items
* the list of items to use for the write
* @deprecated As of 2.2 in favor of using Hibernate's session management APIs directly
*/
protected void doWrite(HibernateOperations hibernateTemplate,
List<? extends T> items) {