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

后端 未结 5 2162
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: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);
        }
    });
    

提交回复
热议问题