先写个HibernateUtil
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class HibernateUtil {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final SessionFactory sessionFactory;
static{
sessionFactory = new Configuration().configure(CONFIG_FILE_LOCATION).buildSessionFactory();
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
然后在DAO类里写
Session sn = HibernateUtil.getSessionFactory().getCurrentSession();
sn.beginTransaction();
写要执行的代码
sn.getTransaction().commit();
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class HibernateUtil {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final SessionFactory sessionFactory;
static{
sessionFactory = new Configuration().configure(CONFIG_FILE_LOCATION).buildSessionFactory();
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
然后在DAO类里写
Session sn = HibernateUtil.getSessionFactory().getCurrentSession();
sn.beginTransaction();
写要执行的代码
sn.getTransaction().commit();
Transaction trans=equipmentTypeDAO.getSession().getTransaction();
这是从equipmentTypeDAO中获取session,然后起事务
而equiSubModRelationshDAO\equipmentTypeDAO是两个不同的DAO,估计里面也都有getsession方法,那他们就都有自己的session,你的事务只针对equipmentTypeDAO,当然就不起作用了.
如果你想用事务,就在外面单独生成一个session,把这个session传到两个DAO里去,让这两个DAO都用相同的session操作数据库