阅读更多
以下是一个例子
==============================
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;
}
}