Spring框架学习[HibernateTemplate对Hibernate的封装](二)

2014-11-24 03:05:48 · 作者: · 浏览: 10
this.sessionFactory.close(); } } //获取单态模式的Hibernate会话工厂 public SessionFactory getObject() { return this.sessionFactory; } //获取Hibernate会话工厂的对象类型 public Class getObjectType() { return (this.sessionFactory != null this.sessionFactory.getClass() : SessionFactory.class); } public boolean isSingleton() { return true; } //转换异常 public DataAccessException translateExceptionIfPossible(RuntimeException ex) { //如果异常时Hibernate异常类型 if (ex instanceof HibernateException) { //转换Hibernate异常 return convertHibernateAccessException((HibernateException) ex); } //如果不是Hibernate异常,则返回null return null; } //转换Hibernate异常 protected DataAccessException convertHibernateAccessException(HibernateException ex) { //如果Hibernate会话工厂设置了Jdbc异常转换器,且给定的异常是jdbc异常类型, //则使用设置的jdbc异常转换器转换给定的Hibernate异常 if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) { JDBCException jdbcEx = (JDBCException) ex; return this.jdbcExceptionTranslator.translate( "Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException()); } //如果Hibernate会话工厂没有设置jdbc异常转换器,或者给定异常不是jdbc异常 //类型,则使用Hibernate默认的异常转换器转换 return SessionFactoryUtils.convertHibernateAccessException(ex); } //创建Hibernate会话工厂 protected abstract SessionFactory buildSessionFactory() throws Exception; //钩子方法,Hibernate会话工厂成功创建后操作处理 protected void afterSessionFactoryCreation() throws Exception { } //钩子方法,Hibernate会话工厂关闭前操作处理 protected void beforeSessionFactoryDestruction() { } }