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

后端 未结 5 2164
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:06

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

    First, add a transaction management bean in your context:

        
            
        
    

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

提交回复
热议问题