spring注释错误:没有事务正在进行中

时间:2015-01-24 12:18:19

标签: spring-mvc annotations jpa-2.0

我使用spring,hibernate和maven来构建我的项目并面对这个错误。 javax.persistence.TransactionRequiredException:没有正在进行的事务

    

<jpa:repositories base-package="com.sam.repository" />

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="evaluationSystemUnit"/>
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">  
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
        </bean>
    </property>

    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <entry key="hibernate.hbm2ddl.auto" value="none" />
            <entry key="hibernate.format_sql" value="true" />
        </map>
    </property>

</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<tx:annotation-driven  transaction-manager="transactionManager"/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/sems?autoReconnect=true&amp;characterEncoding=UTF-8" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

在我的servlet-config.xml中,我应该写

<context:component-scan base-package="com.sam.controller"/>

而不是

<context:component-scan base-package="com.sam"/>

在我的jpaContext.xml中,我应该写

<context:component-scan  base-package="com.sam"/>