经过getCurrentSession()获得的Session提交时自动关闭

通过getCurrentSession()获得的Session提交时自动关闭初学Hibernate时,遇到了一个异常:org.hibernate.Sess

通过getCurrentSession()获得的Session提交时自动关闭

初学Hibernate时,遇到了一个异常:

org.hibernate.SessionException: Session was already closed。

?

Session session = new AnnotationConfiguration().config().buildSessionFactory().getCurrentSession();session.beginTransaction();session.getTransaction.commit();session.close();

?

?

原因:

?

通过getCurrentSession()方法获得Session对象,当这个对象提交commit()时,Session自动关闭,如果再关闭就会发生这个异常。