openSession
//获取的session没有与本地绑定,必须手动关闭session,Session session =sf.openSession();session.close();
getCurrentSession
用这个方法的时候需要在hibernate.cfg.xml中添加一个配置属性:
<property name=
"hibernate.current_session_context_class">thread</property>
然后在获取session时使用SessionFactory.getCurrentSession()方法:
//获取的session是与本地线程绑定的,当线程结束后就会自动关闭session,不需要手动关闭线程了;Session session=sf.getCurrentSession();
转载于:https://www.cnblogs.com/lk617-home/p/7875754.html