//1 加载hibernate.cfg.xml配置文件及映射信息
//Configuration cfg=new Configuration();//这么写表明默认加载文件为properties
Configuration cfg=new Configuration().configure();//默认会在src下搜索hibernate.cfg.xml文件
//2 得到SessionFactory
SessionFactory sf=cfg.buildSessionFactory();
//3得到Session
Session session=sf.openSession();
//4开启事务
Transaction tx=session.beginTransaction();
try{
//Emp emp=(Emp) session.get(Emp.class, id);
//session.delete(emp);
Emp emp=new Emp();
emp.setBirthday(new Date());
emp.setName("王五");
emp.setState(true);
//5.持久化
session.save(emp);
//6 提交事务
tx.commit();
}catch(Exception e){
tx.rollback();
e.printStackTrace();
}finally{
//7释放资源,关闭session
session.close();
}
//Configuration cfg=new Configuration();//这么写表明默认加载文件为properties
Configuration cfg=new Configuration().configure();//默认会在src下搜索hibernate.cfg.xml文件
//2 得到SessionFactory
SessionFactory sf=cfg.buildSessionFactory();
//3得到Session
Session session=sf.openSession();
//4开启事务
Transaction tx=session.beginTransaction();
try{
//Emp emp=(Emp) session.get(Emp.class, id);
//session.delete(emp);
Emp emp=new Emp();
emp.setBirthday(new Date());
emp.setName("王五");
emp.setState(true);
//5.持久化
session.save(emp);
//6 提交事务
tx.commit();
}catch(Exception e){
tx.rollback();
e.printStackTrace();
}finally{
//7释放资源,关闭session
session.close();
}
}