Spring中的HibernateTemplate

最近在用Spring + Hibernate开发,在写DAO的时候用到了Spring的HibernateDaoSupport类,然后在实现的时候就可以很轻松的使用getHibernateTemplate()方法之后就可以调用save()、delete()、update()等 Hibernate的Session的操作,很简单。比如:

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));
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值