Why do I get org.hibernate.HibernateException: No CurrentSessionContext configured
hibernate, java
Solution
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>
Problem
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> ```