BATCH-1904: Updated to support Hibernate 4
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2007 the original author or authors.
|
||||
* Copyright 2006-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,33 +23,32 @@ import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCreditDao;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.orm.hibernate3.HibernateOperations;
|
||||
import org.springframework.orm.hibernate3.HibernateTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Delegates writing to a custom DAO and flushes + clears hibernate session to
|
||||
* fulfill the {@link ItemWriter} contract.
|
||||
*
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
* @author Michael Minella
|
||||
*/
|
||||
public class HibernateAwareCustomerCreditItemWriter implements ItemWriter<CustomerCredit>, InitializingBean {
|
||||
|
||||
private CustomerCreditDao dao;
|
||||
|
||||
private HibernateOperations hibernateTemplate;
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
public void write(List<? extends CustomerCredit> items) throws Exception {
|
||||
for (CustomerCredit credit : items) {
|
||||
dao.writeCredit(credit);
|
||||
}
|
||||
try {
|
||||
hibernateTemplate.flush();
|
||||
sessionFactory.getCurrentSession().flush();
|
||||
}
|
||||
finally {
|
||||
// this should happen automatically on commit, but to be on the safe
|
||||
// side...
|
||||
hibernateTemplate.clear();
|
||||
sessionFactory.getCurrentSession().clear();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -59,11 +58,11 @@ public class HibernateAwareCustomerCreditItemWriter implements ItemWriter<Custom
|
||||
}
|
||||
|
||||
public void setSessionFactory(SessionFactory sessionFactory) {
|
||||
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
|
||||
this.sessionFactory = sessionFactory;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(hibernateTemplate, "Hibernate session factory must be set");
|
||||
Assert.state(sessionFactory != null, "Hibernate SessionFactory is required");
|
||||
Assert.notNull(dao, "Delegate DAO must be set");
|
||||
}
|
||||
|
||||
|
||||
@@ -18,27 +18,32 @@ package org.springframework.batch.sample.domain.trade.internal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.RepeatListener;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCreditDao;
|
||||
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
* @author Dave Syer
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class HibernateCreditDao extends HibernateDaoSupport implements
|
||||
public class HibernateCreditDao implements
|
||||
CustomerCreditDao, RepeatListener {
|
||||
|
||||
private int failOnFlush = -1;
|
||||
private List<Throwable> errors = new ArrayList<Throwable>();
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
public void setSessionFactory(SessionFactory sessionFactory) {
|
||||
this.sessionFactory = sessionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public accessor for the errors property.
|
||||
*
|
||||
*
|
||||
* @return the errors - a list of Throwable instances
|
||||
*/
|
||||
public List<Throwable> getErrors() {
|
||||
@@ -47,7 +52,7 @@ public class HibernateCreditDao extends HibernateDaoSupport implements
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
*
|
||||
* @see org.springframework.batch.sample.domain.trade.internal.CustomerCreditWriter#write(org.springframework.batch.sample.domain.CustomerCredit)
|
||||
*/
|
||||
public void writeCredit(CustomerCredit customerCredit) {
|
||||
@@ -57,15 +62,15 @@ public class HibernateCreditDao extends HibernateDaoSupport implements
|
||||
newCredit.setId(customerCredit.getId());
|
||||
newCredit.setName(customerCredit.getName());
|
||||
newCredit.setCredit(customerCredit.getCredit());
|
||||
getHibernateTemplate().save(newCredit);
|
||||
sessionFactory.getCurrentSession().save(newCredit);
|
||||
} else {
|
||||
getHibernateTemplate().update(customerCredit);
|
||||
sessionFactory.getCurrentSession().update(customerCredit);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
*
|
||||
* @see org.springframework.batch.io.OutputSource#write(java.lang.Object)
|
||||
*/
|
||||
public void write(Object output) {
|
||||
@@ -74,7 +79,7 @@ public class HibernateCreditDao extends HibernateDaoSupport implements
|
||||
|
||||
/**
|
||||
* Public setter for the failOnFlush property.
|
||||
*
|
||||
*
|
||||
* @param failOnFlush
|
||||
* the ID of the record you want to fail on flush (for testing)
|
||||
*/
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
|
||||
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="mappingLocations" value="classpath*:/org/springframework/batch/sample/domain/**/*.hbm.xml" />
|
||||
<property name="hibernateProperties">
|
||||
@@ -16,7 +16,7 @@
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
|
||||
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" lazy-init="true">
|
||||
<property name="sessionFactory" ref="sessionFactory" />
|
||||
</bean>
|
||||
|
||||
|
||||
@@ -158,6 +158,7 @@ public class HibernateFailureJobFunctionalTests {
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
final BigDecimal creditBeforeUpdate = creditsBeforeUpdate.get(i++);
|
||||
final BigDecimal expectedCredit = creditBeforeUpdate.add(CREDIT_INCREASE);
|
||||
System.out.println("expectedCredit = " + expectedCredit + " db credit = " + rs.getBigDecimal(CREDIT_COLUMN));
|
||||
if (expectedCredit.equals(rs.getBigDecimal(CREDIT_COLUMN))) {
|
||||
matches.add(rs.getBigDecimal(ID_COLUMN));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user