为什么我会得到org.hibernate.HibernateException:没有配置CurrentSessionContext [英] Why do I get org.hibernate.HibernateException: No CurrentSessionContext configured
问题描述
我正在编写一个简单的项目,一个用Swing编写的商业应用程序,使用Hibernate作为后端。我来自Spring,这让我轻松地使用休眠和事务。无论如何,我设法让Hibernate工作。昨天,在编写一些代码从DB中删除一个bean时,我得到了这个结果:
org.hibernate.HibernateException:非法尝试将一个集合与两个打开的会话相关联
删除代码很简单:
Session sess = HibernateUtil.getSession();
Transaction tx = sess.beginTransaction();
尝试{
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;
尝试{
sess = sessionFactory.getCurrentSession();
} catch(org.hibernate.HibernateException he){
sess = sessionFactory.openSession();
}
return sess;
$ p 其他细节:我从不在我的代码中关闭一个hibernate会话,只是在应用程序中收盘。这是错的吗?为什么我在删除时只有这个(只为那个bean,其他人在工作),并且我不在其他操作上(插入,查询,更新)?
I阅读,我试图修改我的 getSession
方法只需在 sessionFactory.getCurrentSessionCall()
中,但我得到: org.hibernate.HibernateException:没有配置CurrentSessionContext!
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>根< / 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>更新< / property>
..mappings ..
< / session-factory>
< / hibernate-configuration>
解决方案我想问你一件事,为什么你试图使用OpenSession方法?
public static Session getSession()throws HibernateException {
Session sess = null;
尝试{
sess = sessionFactory.getCurrentSession();
} catch(org.hibernate.HibernateException he){
sess = sessionFactory.openSession();
}
return sess; (
)
您不必调用 openSession() ,因为 getCurrentSession()
方法总是返回当前会话(如果已将其配置为线程,则为线程)。
我知道了!...
您必须在您的hibernate.cfg.xml文件中指定当前上下文
应该是:
< property name =hibernate.current_session_context_class>线程< / property>
I'm writing a simple project, a business app written in Swing, using Hibernate for back-end. I come from Spring, that gave me easy ways to use hibernate and transactions. Anyway I managed to have Hibernate working. Yesterday, while writing some code to delete a bean from DB, I got this:
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
The deletion code is simply:
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();
and my HibernateUtil.getSession()
is:
public static Session getSession() throws HibernateException {
Session sess = null;
try {
sess = sessionFactory.getCurrentSession();
} catch (org.hibernate.HibernateException he) {
sess = sessionFactory.openSession();
}
return sess;
}
additional details: I never close a hibernate session in my code, just on application closing. Is this wrong? Why do I get this on delete (only for that bean, others do work), and I don't on other operations (Insert, query, update)?
I read around and I tried to modify my getSession
method simply in a sessionFactory.getCurrentSessionCall()
, but I got: 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>
解决方案 I wanted to ask you one thing, why are you trying to use "OpenSession" method?
public static Session getSession() throws HibernateException {
Session sess = null;
try {
sess = sessionFactory.getCurrentSession();
} catch (org.hibernate.HibernateException he) {
sess = sessionFactory.openSession();
}
return sess;
}
You don't have to call openSession()
, because getCurrentSession()
method is always returns current session (Thread in case if you have configured it to be).
I got it!...
You have to specify current context in your hibernate.cfg.xml file
it should be:
<property name="hibernate.current_session_context_class">thread</property>
这篇关于为什么我会得到org.hibernate.HibernateException:没有配置CurrentSessionContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!