Hibernate操作数据库的回调机制--Callback
2014-08-21 21:37
387 查看
1:一般情况下,在使用Hibernate Session存取数据库的代码中,基本上大部分是相同的,如下两个方法所示,
[html]
view plaincopyprint?
//查询Teacher操作
ublic Teacher getTeacher(Long id) throws DataAccessException {
Session session = getSession();
Teacher teacher = null;
try {
teacher = (Teacher)session.get(Teacher.class, id);
...
} catch(HibernateException ex) {
throw convertHibernateAccessException(ex);
} catch(SQLException ex) {
throw convertJdbcAccessException(ex);
} catch(RuntimeException ex) {
throw ex;
} finally {
session.close();
return teacher;
}
[html]
view plaincopyprint?
//查询Class操作
ublic classInfo getTeacher(Long id) throws DataAccessException {
Session session = getSession();
ClassInfo classInfo = null;
try {
classInfo = (ClassInfo)session.get(ClassInfo.class, id);
...
} catch(HibernateException ex) {
throw convertHibernateAccessException(ex);
} catch(SQLException ex) {
throw convertJdbcAccessException(ex);
} catch(RuntimeException ex) {
throw ex;
} finally {
session.close();
return classInfo;
}
上面这两种方法的做法是不对的,大量重复的代码会导致后期维护困难,这里可以考虑把具体的业务逻辑处理部分剥离出来,而只对公共的Session获取及释放
和异常处理部分进行封装,形成一个公共的方法,如下面代码所示,
[html]
view plaincopyprint?
//封装后公用方法
ublic Object process(HibernateCallback action) throws DataAccessException {
Session session = getSession();
Object obj = null;
try {
obj = action.doInHibernate(session);
} catch(HibernateException ex) {
throw convertHibernateAccessException(ex);
} catch(SQLException ex) {
throw convertJdbcAccessException(ex);
} catch(RuntimeException ex) {
throw ex;
}
[html]
view plaincopyprint?
//查询Teacher操作
public Teacher getTeacher(final Long id) throws DataAccessException {
return (Teacher)process(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Teacher teacher = (Teacher)session.get(Teacher.class, id);
return teacher;
}
});
}
回调,就是由被调用者调用由调用者提供的对象实现具体的业务操作。回调机制一般与模板方法结合起来作为一种模式使用,回调机制让应用程序人员只关心业务逻辑实现,
而不用处理一些底层通用操作,从而可以实现通用操作和业务逻辑相分离。比如在Java数据库操作中,使用回调机制,不用关心数据库连接的打开和关闭,只需要在Callback实现相关的数据存取即可,这样可以从一定程度上保证数据库连接总是被及时关闭。
1:一般情况下,在使用Hibernate Session存取数据库的代码中,基本上大部分是相同的,如下两个方法所示,
[html]
view plaincopyprint?
//查询Teacher操作
ublic Teacher getTeacher(Long id) throws DataAccessException {
Session session = getSession();
Teacher teacher = null;
try {
teacher = (Teacher)session.get(Teacher.class, id);
...
} catch(HibernateException ex) {
throw convertHibernateAccessException(ex);
} catch(SQLException ex) {
throw convertJdbcAccessException(ex);
} catch(RuntimeException ex) {
throw ex;
} finally {
session.close();
return teacher;
}
//查询Teacher操作 public Teacher getTeacher(Long id) throws DataAccessException { Session session = getSession(); Teacher teacher = null; try { teacher = (Teacher)session.get(Teacher.class, id); ... } catch(HibernateException ex) { throw convertHibernateAccessException(ex); } catch(SQLException ex) { throw convertJdbcAccessException(ex); } catch(RuntimeException ex) { throw ex; } finally { session.close(); return teacher; } }
[html]
view plaincopyprint?
//查询Class操作
ublic classInfo getTeacher(Long id) throws DataAccessException {
Session session = getSession();
ClassInfo classInfo = null;
try {
classInfo = (ClassInfo)session.get(ClassInfo.class, id);
...
} catch(HibernateException ex) {
throw convertHibernateAccessException(ex);
} catch(SQLException ex) {
throw convertJdbcAccessException(ex);
} catch(RuntimeException ex) {
throw ex;
} finally {
session.close();
return classInfo;
}
//查询Class操作 public classInfo getTeacher(Long id) throws DataAccessException { Session session = getSession(); ClassInfo classInfo = null; try { classInfo = (ClassInfo)session.get(ClassInfo.class, id); ... } catch(HibernateException ex) { throw convertHibernateAccessException(ex); } catch(SQLException ex) { throw convertJdbcAccessException(ex); } catch(RuntimeException ex) { throw ex; } finally { session.close(); return classInfo; } }
上面这两种方法的做法是不对的,大量重复的代码会导致后期维护困难,这里可以考虑把具体的业务逻辑处理部分剥离出来,而只对公共的Session获取及释放
和异常处理部分进行封装,形成一个公共的方法,如下面代码所示,
[html]
view plaincopyprint?
//封装后公用方法
ublic Object process(HibernateCallback action) throws DataAccessException {
Session session = getSession();
Object obj = null;
try {
obj = action.doInHibernate(session);
} catch(HibernateException ex) {
throw convertHibernateAccessException(ex);
} catch(SQLException ex) {
throw convertJdbcAccessException(ex);
} catch(RuntimeException ex) {
throw ex;
}
//封装后公用方法 public Object process(HibernateCallback action) throws DataAccessException { Session session = getSession(); Object obj = null; try { obj = action.doInHibernate(session); } catch(HibernateException ex) { throw convertHibernateAccessException(ex); } catch(SQLException ex) { throw convertJdbcAccessException(ex); } catch(RuntimeException ex) { throw ex; } }
[html]
view plaincopyprint?
//查询Teacher操作
public Teacher getTeacher(final Long id) throws DataAccessException {
return (Teacher)process(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Teacher teacher = (Teacher)session.get(Teacher.class, id);
return teacher;
}
});
}
//查询Teacher操作 public Teacher getTeacher(final Long id) throws DataAccessException { return (Teacher)process(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException, SQLException { Teacher teacher = (Teacher)session.get(Teacher.class, id); return teacher; } }); }
回调,就是由被调用者调用由调用者提供的对象实现具体的业务操作。回调机制一般与模板方法结合起来作为一种模式使用,回调机制让应用程序人员只关心业务逻辑实现,
而不用处理一些底层通用操作,从而可以实现通用操作和业务逻辑相分离。比如在Java数据库操作中,使用回调机制,不用关心数据库连接的打开和关闭,只需要在Callback实现相关的数据存取即可,这样可以从一定程度上保证数据库连接总是被及时关闭。
相关文章推荐
- Hibernate操作数据库的回调机制--Callback
- Hibernate操作数据库的回调机制--Callback
- spring对hibernate的集成中的回调(CallBack)机制
- 关于Hibernate中HibernateCallback()的回调
- 基于.NET 2.0的GIS开源项目SharpMap分析手记(十四):ASP.NET2.0实现无刷新客户端回调的Callback机制及例子代码下载
- 使用Hibernate编写通用数据库操作代码
- Hibernate 数据库操作 DAO 实例
- 利用Hibernate编写通用的数据库操作代码
- Spring用回调HibernateCallBack方法
- Hibernate编写通用数据库操作代码
- java回调机制及Hibernate中的HibernateTemplate实现!
- Hibernate通用数据库操作代码
- 使用Struts的Action来通过Hibernate对数据库进行增、删、改、查四项操作
- 简单Hibernate数据库操作
- JAVA操作数据库总结之组件篇-Hibernate
- 通过Ant控制Hibernate操作数据库
- Hibernate 对数据库Blob图片的查询操作---页面显示图片,文件的输出
- [讨论]有关Spring+Hibernate更新数据库的操作
- JDO、EJB/CMP、Hibernate和Amber等数据库映射操作等框架的比较
- ASP.NET2.0实现无刷新客户端回调的Callback机制(示例源码)