Hibernate 事务统一管理
这里是利用 cglib 的代理功能实现业务类的统一事务管理,以达到在业务类的每个方法上自动开启与关闭事务,而免区在每个业务方法里写事务代码的烦恼。且业务类既可以实现接口使代理返回接口,或不实现接口使代理返回对象。
?
在这里用到了 Hibernate 的?getCurrentSession() ,因此需要在 hibernate.cfg.xml 里加上下面这句,仅示例:
?
?
获取 hibernate factory 的工具类:
?
// 得到业务代理对象,接口代理, 用于实现接口的业务类等StudentBusinessIf sb = (StudentBusinessIf)HibernateBusiness.getBusiness(StudentBusiness.class);// 或是下面这样 // 得到业务代理对象, 实际对象代理,用于未实现接口的业务类等StudentBusiness sb = (StudentBusiness)HibernateBusiness.getBusiness(StudentBusiness.class);?