最近在用Spring + Hibernate开发,在写DAO的时候用到了Spring的HibernateDaoSupport类,然后在实现的时候就可以很轻松的使用getHibernateTemplate()方法之后就可以调用save()、delete()、update()等 Hibernate的Session的操作,很简单。比如:
getHibernateTemplate().save(user);
这样一句话在我们没有Spring的时候就必须使用如下的代码才能完成:
这里还省去了异常处理,同时使用了HibernateUtil类来简化从SessionFactory获取Session,以及关闭Session等处理。
不过,HibernateDaoSupport只能注入一个SessionFactory,要是有多个SessionFactory的话,就可以模仿Spring的处理方式,做一个Hibernate的模板, 使用模板模式来简化我们的开发,其主要的目的就是为了简化开发,使代码达到最大话的重用。
getHibernateTemplate().save(user);
这样一句话在我们没有Spring的时候就必须使用如下的代码才能完成:
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
HibernateUtil.colseSession();
这里还省去了异常处理,同时使用了HibernateUtil类来简化从SessionFactory获取Session,以及关闭Session等处理。
不过,HibernateDaoSupport只能注入一个SessionFactory,要是有多个SessionFactory的话,就可以模仿Spring的处理方式,做一个Hibernate的模板, 使用模板模式来简化我们的开发,其主要的目的就是为了简化开发,使代码达到最大话的重用。
package com.deppon.util;
import java.util.HashMap;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.support.DaoSupport;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
public abstract class BaseHibernateDAO extends DaoSupport {
private HibernateTemplate hibernateTemplate;
private HashMap<String,SessionFactory> sessionFactoryMap;
public static final String FACTORY_NAME_HEFEI="HF";
public static final String FACTORY_NAME_SHANGHAI="SH";
public static final String FACTORY_NAME_SUZHOU="SZ";
public static final String FLAG_SYNC_TRUE="1";
public static final String FLAG_SYNC_FALSE="0";
public HashMap<String, SessionFactory> getSessionFactoryMap() {
return sessionFactoryMap;
}
public void setSessionFactoryMap(
HashMap<String, SessionFactory> sessionFactoryMap) {
this.sessionFactoryMap = sessionFactoryMap;
}
public final SessionFactory getSessionFactory(String factoryName){
SessionFactory sessionFactory= sessionFactoryMap.get(factoryName);
if ((this.hibernateTemplate == null) || (sessionFactory != this.hibernateTemplate.getSessionFactory()))
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
return sessionFactory;
}
protected HibernateTemplate createHibernateTemplate(
SessionFactory sessionFactory) {
return new HibernateTemplate(sessionFactory);
}
public final void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public final HibernateTemplate getHibernateTemplate(String factoryName) {
SessionFactory sessionFactory= sessionFactoryMap.get(factoryName);
if ((this.hibernateTemplate == null) || (sessionFactory != this.hibernateTemplate.getSessionFactory()))
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
return this.hibernateTemplate;
}
protected final void checkDaoConfig() {
}
protected final Session getSession(String factoryName)
throws DataAccessResourceFailureException, IllegalStateException {
if(this.hibernateTemplate==null)getHibernateTemplate(factoryName);
return getSession(this.hibernateTemplate.isAllowCreate(),factoryName);
}
protected final Session getSession(boolean allowCreate,String factoryName)
throws DataAccessResourceFailureException, IllegalStateException {
return !allowCreate ? SessionFactoryUtils.getSession(
getSessionFactory(factoryName), false) : SessionFactoryUtils.getSession(
getSessionFactory(factoryName), this.hibernateTemplate
.getEntityInterceptor(), this.hibernateTemplate
.getJdbcExceptionTranslator());
}
protected final DataAccessException convertHibernateAccessException(
HibernateException ex) {
return this.hibernateTemplate.convertHibernateAccessException(ex);
}
protected final void releaseSession(Session session,String factoryName) {
SessionFactoryUtils.releaseSession(session, getSessionFactory(factoryName));
}
}