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

后端 未结 5 2161
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:02

    Duh.

    Session session = sessionFactory.openSession();

    Session session = sessionFactory.getCurrentSession();
    

    Oops.

    (Edited since this was wrong, and getting upvoted).

    0 讨论(0)
  • 2020-12-14 19:05

    Please refer to the Spring documentation. There is a whole chapter on testing, and a section on transaction management:

    http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/testing.html#testing-tx

    I've had success extending AbstractTransactionalJUnit4SpringContextTests, but there's a workaround:

    TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // DAO has access to the session through sessionFactory.getCurrentSession()
            myDao.findSomething(id);
        }
    });
    
    0 讨论(0)
  • 2020-12-14 19:06

    Wrong, that will just fill your code with session management code.

    First, add a transaction management bean in your context:

        <bean id="transactionManager" 
              class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    

    The second thing, extend AbstractTransactionalJUnit4SpringContextTests

        public class BaseDALTest 
               extends AbstractTransactionalJUnit4SpringContextTests{
    

    Third thing, annotate you test class with

        @TransactionConfiguration
        @Transactional
    

    If your transaction demarcation is correct(surrounding your dao or service) you should be done.

    It's not nice to sprinkle session and transaction handling code all around your code (even inside your tests).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-14 19:17

    Spring ignores hibernate.current_session_context_class=thread property (which you don't use) and wraps Hibernate’s SessionFactory in it's own transactional version as explained here

    The solution to this is to set the property

    <property name="exposeTransactionAwareSessionFactory"><value>false</value></property>
    

    in session factory bean configuration

    0 讨论(0)
提交回复
热议问题