tx:注释驱动无法正常工作

时间:2014-10-17 05:42:27

标签: spring hibernate

您好朋友我正在开发基于spring(4.0.3)和hibernate(4.3.6)的应用程序。 当我在会话工厂中保存任何对象时,我面临以下错误:

org.hibernate.HibernateException: save is not valid without active transaction
20:38:59,881 ERROR [STDERR]     at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:348)

以下是我在application-context.xml中使用的条目

<bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactoryAthena" />
</bean>
<tx:annotation-driven  transaction-manager="transactionManager"/>

如果我在事务管理器属性中使用任何值而不是用于bean引用的实际事务管理器,那么我想带到这里,然后它不会抛出错误。 所以我认为它没有采用参考bean的价值 请帮帮我!!

2 个答案:

答案 0 :(得分:1)

你应该看看这个link但是使用xml的例子。

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactoryAthena" />
</bean>
<tx:annotation-driven  transaction-manager="txManager"/>

    <!-- the transactional advice (what happens; see the <aop:advisor/> bean below) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
    <!-- the transactional semantics... -->
    <tx:attributes>
        <!-- all methods starting with get are read-only -->
        <tx:method name="get*" read-only="true"/>
        <!-- other methods use the default transaction settings (see below) -->
        <tx:method name=""/>
    </tx:attributes>
</tx:advice>

但是现在我一直在看春天社区使用带注释的声明式事务。如下例所示:

@Transactional(readOnly = true)
public class DefaultFooService implements FooService {

    public Foo getFoo(String fooName) {
        // do something
    }

    // these settings have precedence for this method
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void updateFoo(Foo foo) {
        // do something
    }
}

答案 1 :(得分:-1)

在班级顶部使用@EnableTransactionManagement

@Component
@EnableTransactionManagement
public class Abc{

}
相关问题