Определите шаблон спящего режима в весеннем XML, а также не используйте новое ключевое слово.Ниже приведен пример конфигурации и классов.
<bean id="yourHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="yourSessionFactory"/>
</property>
</bean>
<bean id="yourRootDAO" class="woc.dao.base.RootDAOImpl" abstract="true">
<property name="hibernateTemplate" ref="yourHibernateTemplate"/>
</bean>
<bean id="comboValueDAO" parent="yourRootDAO" class="woc.dao.impl.ComboValueDAOImpl" />
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public abstract class RootDAOImpl<T> extends HibernateDaoSupport implements RootDAO<T> {
protected Logger logger = Logger.getLogger(getClass());
private Class<T> clazz;
public RootDAOImpl(Class<T> clazz) {
this.clazz = clazz;
}
public void delete(T entity) {
//Mark entity as deleted
try{
Method setDeletedMethod = clazz.getDeclaredMethod("setDeleted", Boolean.class);
setDeletedMethod.invoke(entity, true);
getHibernateTemplate().saveOrUpdate(entity);
}catch(Exception e){
e.printStackTrace();
}
//actually delete
// getHibernateTemplate().delete(entity);
}
@Override
public void deleteAll(Collection<T> entities) {
getHibernateTemplate().deleteAll(entities);
}
@Override
public void saveOrUpdateAll(Collection<T> entities) {
getHibernateTemplate().saveOrUpdateAll(entities);
}
@SuppressWarnings("unchecked")
@Override
public T get(Serializable id) {
return (T)getHibernateTemplate().get(clazz, id);
}
@SuppressWarnings("unchecked")
@Override
public T load(Serializable id) {
return (T)getHibernateTemplate().load(clazz, id);
}
@SuppressWarnings("unchecked")
@Override
public List<T> find(String hql){
return (List<T>)getHibernateTemplate().find(hql);
}
@Override
public Object execute(HibernateCallback action) {
return getHibernateTemplate().execute(action);
}
@SuppressWarnings("unchecked")
@Override
public List<T> findByNamedParam(String queryString, String paramName,Object value) {
return getHibernateTemplate().findByNamedParam(queryString, paramName, value);
}
@SuppressWarnings("unchecked")
@Override
public List<T> findByNamedParam(String queryString, String[] paramNames,Object[] values) {
return getHibernateTemplate().findByNamedParam(queryString, paramNames, values);
}
@SuppressWarnings("unchecked")
@Override
public List<T> getByExampleList(T entity) {
return getHibernateTemplate().findByExample(entity);
}
@Override
public T getByExampleUnique(T entity) {
return null;
}
@SuppressWarnings("unchecked")
@Override
public List<T> listAll() {
return getHibernateTemplate().loadAll(clazz);
}
@SuppressWarnings("unchecked")
@Override
public List<T> loadAll() {
return getHibernateTemplate().loadAll(clazz);
}
@SuppressWarnings("unchecked")
@Override
public T save(T entity) {
return (T)getHibernateTemplate().save(entity);
}
@Override
public void saveOrUpdate(T entity) {
getHibernateTemplate().saveOrUpdate(entity);
}
@Override
public void update(T entity) {
getHibernateTemplate().update(entity);
}
}
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import org.springframework.orm.hibernate3.HibernateCallback;
public interface RootDAO<T> {
public List<T> loadAll();
public T save(T entity);
public void delete(T entity);
public T get(Serializable id);
public T load(Serializable id);
public void saveOrUpdate(T entity);
public void deleteAll(Collection<T> entities);
public void saveOrUpdateAll(Collection<T> entities);
public List<T> find(String hql);
public void update(T entity);
public T getByExampleUnique(T entity);
public List<T> getByExampleList(T entity);
public List<T> listAll();
public Object execute(HibernateCallback action);
public List<T> findByNamedParam(String queryString, String paramName,Object value);
public List<T> findByNamedParam(String queryString, String[] paramNames,Object[] values);
}
public interface ComboValueDAO extends RootDAO<ComboValue>{
}
public class ComboValueDAOImpl extends RootDAOImpl<ComboValue> implements ComboValueDAO {
public ComboValueDAOImpl() {
super(ComboValue.class);
}
}
В вашем коде getHibernateTemplate () и делайте все, что вам нужно.