Hello jglynn
thanks for your reply.
The relevant Controller:
Code:
package de.uni.erlangen.swtmfisc.exampleapp.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/* [...]*/
import de.uni.erlangen.swtmfisc.exampleapp.domain.Customer;
import de.uni.erlangen.swtmfisc.exampleapp.service.CustomerService;
public class EditCustomerController extends SimpleFormController {
private CustomerService customerService;
private Log logger = LogFactory.getLog(getClass());
public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
public EditCustomerController() {
setCommandClass(Customer.class);
setCommandName("customer");
setSessionForm(true);
}
@Override
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
String custID = request.getParameter("custID");
Customer customer = customerService.findCustomerById(Long.parseLong(custID))<D-c>;
if (customer != null) {
logger.info("version: " + customer.getVersion());
}
return customer;
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
Customer customer = (Customer) command;
logger.info("before update with version: " + customer.getVersion() + customer);
customerService.updateCustomer(customer);
logger.info("after update with version: " + customer.getVersion() + customer);
return new ModelAndView(getSuccessView());
}
}
This controller calls a CustomerService to retreive and store a Customer Object
Customer customer = customerService.findCustomerById(Long.parseLong(cu stID));
and:
customerService.updateCustomer(customer);
The CustomerService-Interface is implemented by CustomerServiceImpl:
Code:
package de.uni.erlangen.swtmfisc.exampleapp.service.impl;
import java.util.List;
import de.uni.erlangen.swtmfisc.exampleapp.dao.CustomerDao;
import de.uni.erlangen.swtmfisc.exampleapp.domain.Customer;
import de.uni.erlangen.swtmfisc.exampleapp.service.CustomerService;
public class CustomerServiceImpl implements CustomerService {
private CustomerDao customerDao;
public CustomerDao getCustomerDao() {
return customerDao;
}
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public List<Customer> getCustomers() {
return customerDao.findAll();
}
public Customer findCustomerById(Long custID) {
return customerDao.findById(custID);
}
public void createNewCustomer(Customer cust) {
customerDao.insert(cust);
}
public void updateCustomer(Customer cust) {
customerDao.update(cust);
}
public void deleteCustomer(Customer cust) {
customerDao.delete(cust);
}
}
This service uses a simple CustomerDAO to retrieve and persist Objects:
Code:
package de.uni.erlangen.swtmfisc.exampleapp.dao.hibernate;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import de.uni.erlangen.swtmfisc.dao.hibernate.AbstractHibernateDao;
import de.uni.erlangen.swtmfisc.exampleapp.dao.CustomerDao;
import de.uni.erlangen.swtmfisc.exampleapp.domain.Customer;
public class CustomerHibernateDaoImpl extends AbstractHibernateDao<Customer, Long>
implements CustomerDao {
public CustomerHibernateDaoImpl() {
super(Customer.class, Long.class);
}
@SuppressWarnings("unchecked")
public List<Customer> findByFirstName(String firstname) {
DetachedCriteria criteria = DetachedCriteria.forClass(getObjectClass());
criteria.add(Restrictions.ilike("firstname", firstname != null ? firstname + "%" : ""));
criteria.addOrder(Order.asc("firstname"));
return getHibernateTemplate().findByCriteria(criteria);
}
@SuppressWarnings("unchecked")
public List<Customer> findByFullName(String firstname, String lastname) {
DetachedCriteria criteria = DetachedCriteria.forClass(getObjectClass());
criteria.add(Restrictions.ilike("firstname", firstname != null ? lastname + "%" : ""));
criteria.add(Restrictions.ilike("lastname", lastname != null ? lastname + "%" : ""));
criteria.addOrder(Order.asc("lastname"));
criteria.addOrder(Order.asc("firstname"));
return getHibernateTemplate().findByCriteria(criteria);
}
@SuppressWarnings("unchecked")
public List<Customer> findByLastName(String lastname) {
DetachedCriteria criteria = DetachedCriteria.forClass(getObjectClass());
criteria.add(Restrictions.ilike("lastname", lastname != null ? lastname + "%" : ""));
criteria.addOrder(Order.asc("lastname"));
return getHibernateTemplate().findByCriteria(criteria);
}
}
The relevant Methods (customerDao.findById(custID); customerDao.update(cust);) are implemented
in the super-calss of the CustomerHibernateDAO:
Code:
package de.uni.erlangen.swtmfisc.dao.hibernate;
import java.io.Serializable;
import java.util.List;
import org.hibernate.StaleObjectStateException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import de.uni.erlangen.swtmfisc.dao.Dao;
import de.uni.erlangen.swtmfisc.domain.DomainObject;
@Transactional /* All public methods within the class are executed in a separate transaction */
public abstract class AbstractHibernateDao<T extends DomainObject<I>, I extends Serializable>
extends HibernateDaoSupport implements Dao<T, I> {
private final Class<T> objectClass;
private final Class<I> keyClass;
protected AbstractHibernateDao(final Class<T> objectClass, final Class<I> keyClass) {
this.keyClass = keyClass;
this.objectClass = objectClass;
}
protected Class<T> getObjectClass() {
return objectClass;
}
protected Class<I> getKeyClass() {
return keyClass;
}
public void insert(final T entity) {
if (entity == null) {
return;
}
getHibernateTemplate().saveOrUpdate(entity);
}
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public final List<T> findAll() {
return getHibernateTemplate().loadAll(getObjectClass());
}
@SuppressWarnings("unchecked")
public final T findById(I id) {
return (T) getHibernateTemplate().get(objectClass, id);
}
public final void delete(T entity) {
getHibernateTemplate().delete(entity);
}
public final void update(T entity) {
if (entity == null) {
return;
}
getHibernateTemplate().update(entity);
}
}
These are the my config-files for the application-context:
Hibernate-Configuration:
Code:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>/hibernate/customer.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${database.dialect}
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">100</prop>
<prop key="hibernate.hbm2dll.auto">update</prop>
</props>
</property>
</bean>
</beans>
Persitence Configuration:
Code:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${database.driver}"/>
<property name="url" value="${database.server}"/>
<property name="username" value="${database.user}"/>
<property name="password" value="${database.password}"/>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/database/localHSQLdatabase.properties</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>