首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

HibernateTemplate之模版方式和回调函数

2012-11-05 
HibernateTemplate之模版模式和回调函数spring中的HibernateTemplate提供了Hibernate DAO模式的支持,通过

HibernateTemplate之模版模式和回调函数
spring中的HibernateTemplate提供了Hibernate DAO模式的支持,通过配置,很容易的就能使用一些常用的功能。此处个人觉得优秀的是模版模式的运用,还有回调函数的使用。看这些优秀框架的源代码实在令人惊叹,非常值得好好学习。废话少说,开始唠叨一些自己的浅见。

要想使用HibernateTemplate,可以extends类HibernateDaoSupport,然后在需要进行持久操作的话,调用getHibernateTemplate().save()|update()|等函数。
下面以例子说明:

public interface DAO{   save();}public DAOImpl extends HibernateDaoSupport implements DAO {    public void save() {        POJO pojo = new POJO();        pojo.setName("test");        this.getHibernateTemplate().save(pojo);    }}


查看源代码:
public final void setSessionFactory(SessionFactory sessionFactory) {
  this.hibernateTemplate = createHibernateTemplate(sessionFactory);
}
spring通过此set方法来注入SessionFactory,进而得到HibernateTemplate实例,对应程序中的getHibernateTemplate()方法。
说的有点啰嗦,呵呵,记下现在自己的见解,以便提高,下面才是重点:

所谓的模版模式就是在父类中定义一个操作的算法的骨架或者是操作顺序,而将一些具体步骤的细节延迟到子类中。
HibernateTemplate中最核心的是execute method,下面以save作为叙述的线索:
    用户调用    this.getHibernateTemplate().save(pojo);
public Serializable save(final Object entity) throws DataAccessException {return (Serializable) execute(new HibernateCallback() {public Object doInHibernate(Session session) throws HibernateException {checkWriteOperationAllowed(session);return session.save(entity);}}, true);}

execute方法接受一个HibernateCallback的匿名类作为参数,期中的doInHibernate就是回调函数,将在execute方法中得到调用
public Object execute(HibernateCallback action) throws DataAccessException {return execute(action, isExposeNativeSession());}public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {Assert.notNull(action, "Callback object must not be null");Session session = getSession();boolean existingTransaction = SessionFactoryUtils.isSessionTransactional(session, getSessionFactory());if (existingTransaction) {logger.debug("Found thread-bound Session for HibernateTemplate");}FlushMode previousFlushMode = null;try {previousFlushMode = applyFlushMode(session, existingTransaction);enableFilters(session);Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session));Object result = action.doInHibernate(sessionToExpose);flushIfNecessary(session, existingTransaction);return result;}catch (HibernateException ex) {throw convertHibernateAccessException(ex);}catch (SQLException ex) {throw convertJdbcAccessException(ex);}catch (RuntimeException ex) {// Callback code threw application exception...throw ex;}finally {if (existingTransaction) {logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate");disableFilters(session);if (previousFlushMode != null) {session.setFlushMode(previousFlushMode);}}else {// Never use deferred close for an explicitly new Session.if (isAlwaysUseNewSession()) {SessionFactoryUtils.closeSession(session);}else {SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());}}}}

在execute方法内部就跟使用Hibernate差不多了,只是一些管理方面的事不用自己完全操心,交给spring来处理。

HibernateCallback 是一个接口,必须实现Object doInHibernate(Session session) throws HibernateException, SQLException;方法。


自己的表达不怎么样,可自己看源代码的时候,一步一步理清思路,理解设计的精妙之处时,真是忍不住感叹,这些写框架的程序员真是天才!

热点排行