Spring / Hibernate / JUnit - No Hibernate Session bound to Thread

后端 未结 5 2160
Happy的楠姐
Happy的楠姐 2020-12-14 18:37

I\'m trying to access the current hibernate session in a test case, and getting the following error:

org.hibernate.HibernateException: No Hibernate

5条回答
  •  [愿得一人]
    2020-12-14 19:13

    With the above Spring configuration, it should be sufficient to code

    Session session = sessionFactory.getCurrentSession();
    

    in your method and class to test. Session management is done by the Hibernate / Spring /JUnit test configuration, as later is done in the Hibernate / Spring configuration in the real application.

    This is how it worked for my tests. In the final web application there will automatically be a Hibernate session associated with the current web request and therefore in testing there should be no sessionFactory.openSession() call.

提交回复
热议问题