有时使用getCurrentSession()不好用,提示是session已经关闭,后来改为openSession()就好用了,为什么?老师说是延迟加载的问题,但也调不出来注:SessionFactory已经在Spring配置文件配置好了

解决方案 »

  1.   

    在 SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在 getCurrentSession() 被调用的时候,实际被执行的方法是 CurrentSessionContext.currentSession() 。在 currentSession() 执行时,如果当前 Session 为空, currentSession 会调用 SessionFactory 的 openSession 。所以 getCurrentSession() 对于 Java EE 来说是更好的获取 Session 的方法
      

  2.   

    1. getCurrentSession()   获的当前的session2. openSession()  也是获的session   是重新打开一个新的session