日期:2014-05-19  浏览次数:20656 次

Hibernate 一点问题
Java code


public class HibernateUtil {
    private static Configuration cfg;
    private static ServiceRegistry sr;
    private static SessionFactory sf;
    static {
        try {
            cfg = new Configuration();
            cfg.configure();
            sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
            sf = cfg.buildSessionFactory(sr);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    
    public static Configuration getConfiguration() {
        return cfg;
    }
    
    public static SessionFactory getSessionFactory() {
        return sf;
    }
    
    public static Session getSession() throws HibernateException {
        return sf.getCurrentSession();
    }
}

public class EcrPartsDaoImpl extends HibernateUtil implements EcrPartsDao {
    //添加数据
    public boolean addData(EcrParts ecr) {
        boolean flag = false;
        Transaction tx = null;
        try {
            tx = getSession().beginTransaction();
            getSession().save(ecr);
            tx.commit();
            flag = true;
        } catch (Exception e) {
            // TODO: handle exception
            tx.rollback();
            e.printStackTrace();
        } finally {
            getSessionFactory().close();
        }
        return flag;
    }
}


一到tx = getSession().beginTransaction();这里就出现异常了。不知道怎么回事,求解

------解决方案--------------------
报什么异常?
------解决方案--------------------
连接好像没打开吧。