做dao层的单元测试时报出No Hibernate Session bound to thread异常信息

BaseDao

抽取出来的Dao接口

必须通过子类来实例化,因此自身设置为懒加载。lazy-init=true

 

Spring 默认情况下(及时加载+单例模式)

 

 

 

 

 

 

 

在做dao层的单元测试时报出以下异常信息。

【原因】

No Hibernate Session bound to thread。没有提供事务。

【解决方法】

 

applicationContext.xml主配置文件中  默认切service

 

 

 

applicationContext.xml主配置文件中  默认切service层
<aop:config>
	<aop:pointcut expression=" execution(* com.myapps.service.impl.*.*(..))" id="pointcut"/>
	<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
	
</aop:config>

修改为:
<aop:config>
	<aop:pointcut expression=" execution(* com.myapps.*.*.*(..))" id="pointcut"/>
	<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
	
</aop:config>

【测试通过后再修改回来即可】






错误:




org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
	at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
	at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
	at com.myapps.dao.impl.BaseDaoImpl.save(BaseDaoImpl.java:30)
	at com.myapps.dao.impl.CategoryDaoImplTest.testSave(CategoryDaoImplTest.java:37)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

 

你可能感兴趣的:(Hibernate)