为什么我得到 org.hibernate.HibernateException: No CurrentSessionContext 配置
我正在编写一个简单的项目,一个用Swing编写的商业应用程序,使用Hibernate作为后端。我来自Spring,这给了我使用休眠和交易的简单方法。无论如何,我设法让Hibernate工作。昨天,在编写一些代码以从DB中删除Bean时,我得到了这个:
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
删除代码很简单:
Session sess = HibernateUtil.getSession();
Transaction tx = sess.beginTransaction();
try {
tx.begin();
sess.delete(ims);
} catch (Exception e) {
tx.rollback();
throw e;
}
tx.commit();
sess.flush();
而我的是:HibernateUtil.getSession()
public static Session getSession() throws HibernateException {
Session sess = null;
try {
sess = sessionFactory.getCurrentSession();
} catch (org.hibernate.HibernateException he) {
sess = sessionFactory.openSession();
}
return sess;
}
其他详细信息:我从不在代码中关闭休眠会话,只是在应用程序关闭时关闭。这是错的吗?为什么我在删除时得到这个(只针对该bean,其他人确实工作),而我没有在其他操作(插入,查询,更新)上得到这个?
我四处阅读,我试图简单地在,中修改我的方法,但我得到了:getSession
sessionFactory.getCurrentSessionCall()
org.hibernate.HibernateException: No CurrentSessionContext configured!
Hibernat conf:
<hibernate-configuration>
<session-factory >
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/joptel</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">******</property>
<property name="hibernate.connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
..mappings..
</session-factory>
</hibernate-configuration>