欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate3 的事务管理 HibernateORMDAO

程序员文章站 2022-07-12 18:27:39
...
以下是一个例子
==============================
package com.adasoft.sms.dao.hibernate;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class test extends HibernateDaoSupport{

/**
*
* @param insertList
* @return
*/
  public boolean InsertEmployees(List insertList){
  boolean result=true;
//   SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
//   Session session= sessionFactory.openSession();
  Session session=SessionFactoryUtils.getSession(getSessionFactory(), true);

  session.setFlushMode(FlushMode.COMMIT );
  Transaction tx = null;
  try {
      tx = session.beginTransaction();
      for(int i=0;i<insertList.size();i++){
      //Employee employee=new Employee();
      //employee=(Employee)insertList.get(i);
      //this.saveEmployee(employee);
                  //.....
      }
      tx.commit();
  }
  catch (RuntimeException e) {
      if (tx != null) tx.rollback();
      result=false;
      throw e; // or display error message
    
  }
  finally {
      session.close();     
  }
  return result;
  }
}
相关标签: Hibernate ORM DAO