spring注解的事务不开启

<context:annotation-config />
<context:component-scan base-package="com.shopping" />
<!-- 采用注解形式的声明式事务管理 -->
<tx:annotation-driven transaction-manager="txManager"/>

<!-- 基于hibernate注解的sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 基于hibernate的事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

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


这是applicationContext.xml的配置,但我在Service调用hibernateTemplate.getSessionFactory().getCurrentSession().createQuery("from Person");
这个方法的时候就报:org.hibernate.HibernateException: createQuery is not valid without active transaction
说事务没开启,但是我在方法上配置了@Transactional,@Transactional这个注解不会帮开事务的吗?

然后我用手动管理事务又OK了,不报错
hibernateTemplate.getSessionFactory().getCurrentSession().beginTransaction();
hibernateTemplate.getSessionFactory().getCurrentSession().createQuery("from Person");
hibernateTemplate.getSessionFactory().getCurrentSession().getTransaction().commit();


这个问题很经典了
在住容器中,将Controller的注解排除掉
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

而在servlet的spring配置文件中将server给去掉
<context:component-scan base-package="com">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>

应为spring的context是父子容器,所以会产生冲突

如果这样还不对那就是其他问题了 呵呵 祝你好运
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值