【IT168技术文档】
快速搭建起基于Webwork与Hibernate的web应用.
主要是一下几个文件完成了我们将Hibernate的Session与Webwork的Action绑定从而快速搭建web应用的关键:
通用类:
HibernateSessionFactoryImp.java
HibernateSession.java
HibernateSessionImp.java
AbstractAction.java
拦截器:
HibernateInterceptor.jva
此外还需要在配置文件中加入相应的申明和相关的资源文件,下面一一介绍.
一.通用类
1.HibernateSessionFactory.java中主要封装了Hibernate的SessionFactory实例,我们知道SessionFactory相比Session是重量级别,所以生成相当耗费资源,而一个应用也只能生成一个SessionFactory实例.
2.HibernateSessionFactoryImp.java是一个Interface,供HibernateSession实现,从而让webwork拦截器自动调用其中的方法.public class HibernateSessionFactory implements Initializable, Disposable, Serializable ...{
![]()
private static final Log LOG = LogFactory.getLog(SessionFactoryService.class);
![]()
private SessionFactory sf;
![]()
//返回SessionFactory实例
public HibernateSessionFactory getSessionFactory() ...{
return sf;
}
![]()
//初始化生成SessionFactory
public void init() ...{
try ...{
sf = new Configuration().configure().buildSessionFactory();
}
catch (Exception e) ...{
LOG.error("error generate SessionFactory", e);
throw new RuntimeException( e.getMessage() );
}
}
![]()
//关闭SessionFactory
public void dispose() ...{
try ...{
sf.close();
}
catch (Exception e) ...{
LOG.error("error closing HibernateSession", e);
}
}
![]()
public String getDialect() ...{
return Environment.getProperties().getProperty(Environment.DIALECT);
}
![]()
}
3. HibernateSession.java封装了一个Session实例,并且实现了HibernateSessionFactoryImp接口从而在加入拦截器申明以后(后文会介绍)会通过webwork的拦截器自动调用其中的setHibernateSessionFactory()方法,从而自动将HibernateSessionFactory实例注入到了HibernateSession中,供HibernateSession调用public interface HibernateSessionFactoryImp ...{
public void setSessionFactoryService(SessionFactoryService sfs);
}
public class HibernateSession implements HibernateSessionFactoryImp...{
![]()
private static final Log LOG = LogFactory.getLog(SessionService.class);
![]()
private Session session;
private Transaction transaction;
private HibernateSessionFactory hsf;
private boolean rollBackOnly;
![]()
//返回一个Session实例,并开始一个事务
public Session getSession() throws HibernateException ...{
if (session==null) ...{
session = hsf.getSessionFactory().openSession();
transaction = session.beginTransaction();
}
return session;
}
![]()
//注入HibernateSessionFactory实例
public void setHibernateSessionFactory(HibernateSessionFactory hsf) ...{
this.hsf = hsf;
}
![]()
![]()
//关闭一个Session
public void disposeSession() throws HibernateException ...{
![]()
LOG.debug("disposing");
if (session==null) return;
if (rollBackOnly) ...{
try ...{
LOG.debug("rolling back");
if (transaction!=null) transaction.rollback();
}
catch (HibernateException e) ...{
LOG.error("error during rollback", e);
throw e;
}
finally ...{
session.close();
session=null;
transaction=null;
}
}
else ...{
try ...{
LOG.debug("committing");
if (transaction!=null) transaction.commit();
}
catch (HibernateException e) ...{
LOG.error("error during commit", e);
transaction.rollback();
throw e;
}
finally ...{
session.close();
session=null;
transaction=null;
}
}
}
//事务回滚
public boolean isRollBackOnly() ...{
return rollBackOnly;
}
public void setRollBackOnly(boolean rollBackOnly) ...{
this.rollBackOnly = rollBackOnly;
}
}
