实现自己的HibernateCallBack

来源:互联网 发布:刚注册的淘宝号被冻结 编辑:程序博客网 时间:2024/05/16 08:49
我们知道,Spring中,HibernateTemplate是依靠回调函数HibernateCallBack来实现其功能的,但在我们的系统中,
这个CallBack可能不满足我们的要求,这时,我们就需要实现自己的HibernateCallBack,本文讨论实现
自己的HibernateCallBack的方法.
1:声明回调接口
public interface HongSoftCallback {
    Object doInHongSoft(Connection aConn, Object arg)
            throws HongSoftException;
}
2:定义回调函数(HibernateTemplate.java)
public Object load(final int id) throws DataAccessException {
  return execute(new HongSoftCallback() {
   public Object doInHongSoft(Connection aConn, Object arg)
            throws HongSoftException; {
    return aConn.load(arg.toString(), id);
   }
  });
}
这里定义了回调函数,并通过execute()调用了该回调函数
3:具体的execute()方法
    try {
   Object result = action.doInHongSoft(conn,id);   
   return result;
  }
  catch (HongSoftException ex) {
   throw convertHongSoftException(ex);
  }
  finally
  {
    conn.close();
  }
4:调用回调函数
return getHibernateTemplate().load(3);



原创粉丝点击