RESOLVED - BATCH-893: Remove the HibernateAwareItemWriter?
-removed HibernateAwareItemWriter and fixed sample -moved business logic from CustomerCreditIncreaseItemWriter into processor
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package org.springframework.batch.sample.domain.trade.internal;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
|
||||
/**
|
||||
* Increases customer's credit by a fixed amount.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class CustomerCreditIncreaseProcessor implements ItemProcessor<CustomerCredit, CustomerCredit> {
|
||||
|
||||
public static final BigDecimal FIXED_AMOUNT = new BigDecimal("1000");
|
||||
|
||||
public CustomerCredit process(CustomerCredit item) throws Exception {
|
||||
return item.increaseCreditBy(FIXED_AMOUNT);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.springframework.batch.sample.domain.trade.internal;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
@@ -8,13 +7,11 @@ import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCreditDao;
|
||||
|
||||
/**
|
||||
* Increases customer's credit by fixed amount.
|
||||
* Delegates actual writing to a custom DAO.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class CustomerCreditIncreaseWriter implements ItemWriter<CustomerCredit> {
|
||||
|
||||
public static final BigDecimal FIXED_AMOUNT = new BigDecimal("1000");
|
||||
public class CustomerCreditItemWriter implements ItemWriter<CustomerCredit> {
|
||||
|
||||
private CustomerCreditDao customerCreditDao;
|
||||
|
||||
@@ -26,17 +23,9 @@ public class CustomerCreditIncreaseWriter implements ItemWriter<CustomerCredit>
|
||||
this.customerCreditDao = customerCreditDao;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.batch.item.processor.DelegatingItemWriter#doProcess
|
||||
* (java.lang.Object)
|
||||
*/
|
||||
public void write(List<? extends CustomerCredit> customerCredits) throws Exception {
|
||||
for (CustomerCredit customerCredit : customerCredits) {
|
||||
CustomerCredit result = customerCredit.increaseCreditBy(FIXED_AMOUNT);
|
||||
customerCreditDao.writeCredit(result);
|
||||
customerCreditDao.writeCredit(customerCredit);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.springframework.batch.sample.domain.trade.internal;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
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
|
||||
*/
|
||||
public class HibernateAwareCustomerCreditItemWriter implements ItemWriter<CustomerCredit>, InitializingBean {
|
||||
|
||||
private CustomerCreditDao dao;
|
||||
|
||||
private HibernateOperations hibernateTemplate;
|
||||
|
||||
public void write(List<? extends CustomerCredit> items) throws Exception {
|
||||
for (CustomerCredit credit : items) {
|
||||
dao.writeCredit(credit);
|
||||
}
|
||||
try {
|
||||
hibernateTemplate.flush();
|
||||
}
|
||||
finally {
|
||||
// this should happen automatically on commit, but to be on the safe
|
||||
// side...
|
||||
hibernateTemplate.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setDao(CustomerCreditDao dao) {
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
public void setSessionFactory(SessionFactory sessionFactory) {
|
||||
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(hibernateTemplate, "Hibernate session factory must be set");
|
||||
Assert.notNull(dao, "Delegate DAO must be set");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user