日期:2014-05-19 浏览次数:20656 次
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; } }