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

前端 未结 3 1717
误落风尘
误落风尘 2020-12-15 04:56

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. A

3条回答
  •  独厮守ぢ
    2020-12-15 05:52

    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:

    thread
    

提交回复
热议问题