org.hibernate.SessionException:会话已关闭

时间:2014-03-04 20:52:09

标签: java swing hibernate

public List<Examination> loadExaminations(int pID) {

    session = sessionFactory.openSession();
    session.setFlushMode(FlushMode.MANUAL);
    ManagedSessionContext.bind(session);
    session.beginTransaction();
    Patient tpatient = (Patient) session.get(Patient.class, pID);
    List<Examination> examinations = new LinkedList<>();
    if (tpatient.getExaminations()!=null)
    if (!tpatient.getExaminations().isEmpty()) { //I get the exception to this line
        examinations = (List<Examination>) tpatient.getExaminations();
    }
    ManagedSessionContext.unbind(sessionFactory);
    session.flush();
    session.getTransaction().commit();
    session.close();
    return examinations;
}

例外:

org.hibernate.SessionException: Session is closed

我得到了例外,但实际上在程序中看起来很好;正在发生的事情应该发生,但例外是困扰我。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

如果写入您的Hibernate Config文件会话关闭标记,则无需使用session.close()方法,否则将会话关闭标记写入hibernate配置文件并删除session.close()语句。

如下面的行写入hibernate配置文件。

<property name='transaction.auto_close_session'>true</property>