2、openSession和getCurrentSession方法的区别

在hibernate中,SessionFactory的方法openSession和getCurrentSession的区别:
  • 前者是打开一个新的session;后者获取当前的session,如果没有就新建一个;
  • 如果使用前者获取一个session的话,需要手动关闭session;后者事务提交后自动关闭session,如果还执行关闭方法,则会报:Session has already closed;的错误;
  •  sf.getCurrentSession方法需要在hibernate.cfg.xml做如下配置:

    <propertynamepropertyname="current_session_context_class" >thread</property>

    没有配置的话会报如下异常:org.hibernate.HibernateException:No CurrentSessionContext configured!





posted @ 2014-04-06 17:44  zmpandzmp  阅读(184)  评论(0编辑  收藏  举报