Session session = getSession();
tx = session.beginTransaction();
            session.save(vo);
            session.flush();
            tx.commit();
            session.close();
-----------------------
你可以在配置中配置自动提交

解决方案 »

  1.   

    可以这样写
    public class ICatDAO() {
      public Cat saveCat(Cat cat);
    }
    public class CatHibernateDAO() extends HibernateDaoTemplate implements ICatDAO {
      public Cat saveCat(Cat cat) {
        try {
          this.getHibernateTemplate().save(cat)
          return cat;
        }
        catch(DataAccessException dae) {
          ...
        }
      }
    }public class ICatService() {
      public Cat saveCat(Cat cat);
    }public class CatServiceImp() implements ICatService {
      private ICatDAO catDAO; //add set, get
      public Cat saveCat(Cat cat) {
        this.getCatDAO().saveCat(cat);
      }
    }还需要一个取得数据连接配置的类,从你的配置里面取得bean id
    //Action in struts
    public class MyAction
        extends Action {
        private ICatService catService
        private WebApplicationContext wac;
        public void setServlet(ActionServlet actionServlet) {
        super.setServlet(actionServlet);
        ServletContext servletContext = actionServlet.getServletContext();
        this.wac = WebApplicationContextUtils.getRequiredWebApplicationContex(servletContext);
        this.catService = (ICatService)wac.getBean("cat");//spring.xml
       }
      public ICatService getCatService() {
        return catService;
    }//
      

  2.   

    getHibernateTemplate()利用 callback 处理了你的session获取,事务,释放等过程。