Configuration conf = newConfiguration().configure();
(2)读取并解析映射信息,创建sessionFactorySessionFactory sf = conf.buildSessionFactory();
(3)打开SessionSession session = sf.openSession();
(4)开始一个事务(增删改操作必须,查询操作可选)Transaction tx = session.beginTransaction();
(5)数据库操作session.save(user);//或其它操作
(6)提交事务(回滚事务)tx.commit();(tx.rollback();
(7)关闭sessionsession.close();
注:如果Hibernate 配置文件中,current_session_context_class 参数设置为thread 并采用SessionFactory 的getCurrentSession()方法获的Session 实例则不需要此步。
如图所示:
新闻热点
疑难解答