通常情况下,ORM用的最多的是Hibernate。使用它,除了需要处理像Session、SessionFactory这些Hibernate类之外,还需要处理诸如事务处理、打开Session和关闭Session这样的问题,在某种程度上增加了使用Hibernate的难度。而Spring提供的Hibernate封装,如HibernateDaoSupport、HIbernateTemplate等,简化了这些通用过程。
Spring的ORM包提供了对许多ORM产品的支持。通常使用Spring提供的Template类。在这些模板类里,封装了主要的数据操作方法,比如query、update等,并且在Templdate封装中,已经包含了Hibernate中Session的处理,Connection的处理、事务的处理等。通过封装将Hibernate的持久化数据操作纳入到Spring统一的事务处理框架中,这部分是通过Spring的AOP来实现的。
类图:
DaoSupport是一个核心类,通过HIbernateTemplate支持对HIbernate的操作。
Spring的ORM模块并不是重新开发的,通过IOC容器和AOP模块对Hibernate的使用进行封装。使用HIbernate,需要对HIbernate进行配置,这些配置通过SessionFactory来完成,在Spring的HIbernate模块中,提供了LocalSessionFactoryBean来封装SessionFactory的配置,通过这个LocalSessionFactory封装,可以将SessionFactory的配置信息通过Bean定义,注入到IOC容器中实例化好的SessionFactory单例对象中。这个LocalSessionFactoryBean设计为HIbernate的使用奠定了基础。
以hibernateTemplate为例
与JdbcTemplate的使用类似,Spring使用相同的模式,通过execute回调来完成。如下:
代码
public <T> T execute(HibernateCallback<T> action) throws DataAccessException{
return doExecute(action,false,false);
}
protected <T> T doExecute(HIbernateCallback<T> action,boolean enforceNewSession,boolean enforceNativeSession) throws DataAccessException{
Assert.notNull(action,"Callback object must not be null");
//这里是取得HIbernate的Session,判断是否强制需要新的Session,
//如果需要,则直接通过SessionFactory打开一个新的session,否则需要结合配置和当前的Transaction的情况来使用Session
Session session = (enforceNewSession ? SessionFactoryUtils.getNewSession(getSessionFactory(),getEntityInterceptor()):getSession());
//判断Transaction是否已经存在,如果是,则使用的就是当前的Transaction的session
boolean existingTransaction = (!enforceNewSession &&
(!isAllowCreate()||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 = (enforceNativeSession || isExposeNativeSession() ? session : createSessionProxy(session));
//这里是对HIbernateCallback中回调函数的调用,Session作为参数可以由回调函数使用
T result = action.doInHibernate(sessionToExpose);
flushIfNecessary(session,existingTransaction);
return result;
} catch (HibernateException ex) {
throw convertHibernateAccessException(ex);
}catch(SQLException ex){
throw convertJdbcAccessException(ex);
}catch(RuntimeException ex){
throw ex;
//如果存在Transaction,当前回调完成使用完session后,不关闭这个session
}finally{
if(existingTransaction){
logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate");
disableFilters(session);
if(previousFlushMode != null){
session.setFlushMode(previousFlushMode);
}
}
//如果不存在Transaction,那么关闭当前Session
else{
if(isAlwaysUseNewSession()){
SessionFactoryUtils.closeSession(session);
}else{
SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session,getSessionFactory());
}
}
}
}总结
Spring封装了事务处理,以及通过HIbernateTemplate封装了Session,不直接对Session进行操作。
Spring不提供具体的ORM实现,只为应用提供对ORM产品的集成环境和使用平台。并且Spring封装的Hibernate的API,方便了用户。
Spring Template(3) ——ORM 设计与实现
原文:http://blog.csdn.net/liutengteng130/article/details/46241259