2014-03-04 93 views
3
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:會話關閉

org.hibernate.SessionException: Session is closed 

我得到的例外,但實際上它似乎是精細的程序;正在發生永恆的事情,但例外是困擾着我。

感謝您的幫助!

+1

'session.getTransaction()。提交()'會自動關閉會話,所以我認爲除了正在由'session.close()'拋出? 'flush()'也是不必要的。 – MattR

+0

不,這個tpatient.getExaminations()。isEmpty()語句拋出異常! – BLama

+0

您可能正在處理分離的對象。 – Jay

回答

0

如果寫入您的Hibernate Config文件會話關閉標記中,則不需要使用session.close()方法,否則將會話關閉標記寫入hibernate配置文件並刪除session.close()語句。

像下面一行寫入休眠配置文件。

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