没有事务性EntityManager可用错误

时间:2015-12-02 13:31:16

标签: spring jpa spring-transactions

我有一个弹簧设置。基本上我在这里尝试配置两个事务管理器。一个用hibernate,另一个用JPA。但不知何故,当我尝试运行JPA事务管理器时,我得到" javax.persistence.TransactionRequiredException:没有事务性EntityManager可用" 错误。如果有人在下面的代码中发现问题,请感谢。

data-context.xml文件如下

  <bean id="fundBO" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces" value="com.test.FundBo"/>
        <property name="interceptorNames">
            <list>
                <idref  bean="transactionInterceptor"/>
                <idref bean="fundBOTarget"/>
            </list>
        </property>
    </bean>

    <bean id="fundBOTarget" class="com.test.FundBoImpl" />


<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager">
        <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    </property>
</bean>

AppConfig如下。

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.test.**"})
@ImportResource("classpath:data-context.xml")
public class AppConfig { 

    @Resource
    DataSource dataSource;

    @Bean
    public JpaTransactionManager jpaTransactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        transactionManager.setJpaDialect(new HibernateJpaDialect());
        transactionManager.setNestedTransactionAllowed(true);
        transactionManager.afterPropertiesSet();
        return transactionManager;
    }


    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setPersistenceProviderClass(HibernatePersistence.class);
        factoryBean.setPackagesToScan("com.test.**");
        factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        Properties jpaProperties = new Properties();
        jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
        jpaProperties.put("hibernate.jdbc.batch_size", "20");
        jpaProperties.put("hibernate.show_sql", "false");
        jpaProperties.put("hibernate.hbm2ddl.auto", "validate");
        jpaProperties.put("hibernate.autoReconnect", "true");
        jpaProperties.put("hibernate.autoReconnectForPools", "true");
        jpaProperties.put("hibernate.is-connection-validation-required", "true");
        factoryBean.setJpaProperties(jpaProperties);
        factoryBean.afterPropertiesSet();
        return factoryBean;
    }
}

0 个答案:

没有答案