我正在编写一个简单的项目,一个用Swing编写的业务应用程序,使用Hibernate作为后端。我来自Spring,这让我轻松地使用休眠和事务。无论如何,我设法让Hibernate工作。昨日,一边写一些代码来删除DB豆,我得到这个:为什么我会得到org.hibernate.HibernateException:没有配置CurrentSessionContext
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;
}
附加细节:我从来没有在我的代码中关闭一个hibernate会话,只是在应用程序关闭时。这是错的吗?为什么我在删除(只为该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>
你的hibernate配置文件是怎么样的? – Santosh