大家好,这两天在用ssh框架写个项目。我在sring的配置文件中配置了aop的事物声明。但是单元测试时一直报
 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:695)
 at com.zgh.shop.dao.impl.BaseDaoImpl.saveEntity(BaseDaoImpl.java:37)
 at com.zgh.shop.service.impl.BaseServiceImpl.saveEntity(BaseServiceImpl.java:69)
 at com.zgh.shop.test.TestDao.save(TestDao.java:32)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
 at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
 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)不知道是什么问题?我的beans.xml的配置文件为:
  <!-- 配置扫描配置文件 -->
     <context:property-placeholder location="classpath:jdbc.properties"/>
     <!-- 扫描组件 -->
     <context:component-scan base-package="com.zgh.shop.dao,com.zgh.shop.service,com.zgh.shop.struts.action" />
     <!-- 配置数据源 -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     <property name="driverClass" value="${jdbc.driverclass}" />
     <property name="jdbcUrl" value="${jdbc.url}"/>
     <property name="user" value="${jdbc.username}"/>
     <property name="password" value="${jdbc.password}" />
 
<property name="maxPoolSize" value="${c3p0.pool.size.max}" />
 <property name="minPoolSize" value="${c3p0.pool.size.min}" />
 <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
 <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
     </bean>
     
     <!-- 配置本地会话工厂bean,hibernate的核心入口 -->
 <bean id="sessionFactory"
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 <property name="dataSource" ref="dataSource" />
 <!-- hibernate自身属性 -->
 <property name="hibernateProperties">
 <props>
 <prop key="hibernate.dialect">${hibernate.dialect}</prop>
 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
 </props>
 </property>
 <!-- 映射目录位置集 -->
 <property name="mappingDirectoryLocations">
 <list>
  <value>classpath:com/zgh/shop/domain</value>
 </list>
 </property>
 </bean> <!-- hibernate事物管理器,在service层面上实现事物管理 -->
 <bean id="hibernateTransactionManager"
           class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory" />
     </bean>
     
      <!-- 事务通知 -->
     <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
         <tx:attributes>
             <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
             <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
             <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
             <tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/>
             
             <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
             <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
             <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
             
             <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
         </tx:attributes>
     </tx:advice>
     
     <!-- aop配置 -->
     <aop:config>
         <aop:pointcut id="txPointcut" expression="execution(* *..*Service.*(..))"/>
         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
     </aop:config>
 
以前我就是这样配置的,在其他项目中就能运行成功。但是现在一直报错,请帮我找找原因,谢谢了!!! hibernatespring

解决方案 »

  1.   

    <context:component-scan base-package="com.zgh.shop.dao,com.zgh.shop.service,com.zgh.shop.struts.action" />
    我也是个新手,也有这样的问题,如果在spring里面用注解就没事,可是他们写的和你的差不多,是用配置文件来管理事务的,我写的代码也就有了问题了,如果和你这样用 
    <!-- 事务通知 -->
         <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
             <tx:attributes>          
                 <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
             </tx:attributes>
         </tx:advice>
    在类里面是不是也要加入注解呢?@Service @Compnent @Transactional...
      

  2.   

    是呀,在service层要加service注解才行。