java - Why do I get org.hibernate.HibernateException: No CurrentSessionContext configured -


I am writing a simple project, a professional app written in the swing using hibernate for back-end . I came from Spring, which gave me easy ways to use Hibernation and Transactions. Anyway, I managed to work in hibernation. Yesterday, while writing some code to remove a bean from DB, I found this:

  org.hibernate.HibernateException: Invalid attempt to associate a collection with two open sessions   

The deletion code is simply:

  session cs = hibernate utility.jet session (); Transaction tx = sess.beginTransaction (); Try {tx.begin (); Sess.delete (IMS); } Hold (exception e) {tx.rollback (); Throw e } Tx.commit (); Sess.flush ();   

and my HibernateUtil.getSession () :

  public static session getSession () throws HibernateException {session session = null ; Try {Sess = sessionFactory.getCurrentSession (); } Hold (org.hibernate.HibernateException that) {sess = sessionFactory.open session (); } Return Cess; }   

Additional details: I do not close a hibernate session in my code, just close the application. Is this wrong? Why do I get it removed (only for that beans, others have to work), and I am not on other functions (insert, queries, updates)?

I read around and I tried to modify my code getSession in the rule only one sessionFactory.getCurrentSessionCall () , but I got it : org.hibernate.HibernateException: Not Configure CurrentSessionContext!

Hibernate Conf:

  & lt; Hibernate-configuration & gt; & Lt; Session-factory & gt; & Lt; Property Name = "hibernate.dialect" & gt; Org.hibernate.dialect.MySQLDialect & lt; / Property & gt; & Lt; Property Name = "hibernate.connection.driver_class" & gt; Com.mysql.jdbc.Driver & lt; / Property & gt; & Lt; Property Name = "hibernate.connection.url" & gt; Jdbc: mysql: // localhost / jopel & lt; / Property & gt; & Lt; Property Name = "hibernate.connection.username" & gt; Root & lt; / Property & gt; & Lt; Property Name = "hibernate.connection.password" & gt; ****** & lt; / Property & gt; & Lt; Property Name = "hibernate.connection.pool_size" & gt; 1 & lt; / Property & gt; & Lt; Property Name = "show_sql" & gt; True & lt; / Property & gt; & Lt; Property Name = "hibernate.hbm2ddl.auto" & gt; Update & lt; / Property & gt; .. Mapping .. & lt; / Session-factory & gt; & Lt; / Hibernate-configuration & gt; I want to ask you one thing, why are you trying to use "open session", method?  
  Public Session session getSession () throws HibernateException {session session = zero; Try {Sess = sessionFactory.getCurrentSession (); } Hold (org.hibernate.HibernateException that) {sess = sessionFactory.open session (); } Return Cess; }   

You do not need to call openSession () , because getCurrentSession () method always returns the current session (Thread in case if you have configured it)

I've got it! ... You have to specify the current reference in your hybrancet.cf.xml file

It should be:

     

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -