No transactional EntityManager available Error

别来无恙 提交于 2021-02-11 14:30:41

问题


I have a below spring setup. Basically I am trying to configure two transaction managers here. One with hibernate and other with JPA. But somehow when I try to run JPA transaction manager, there I get "javax.persistence.TransactionRequiredException: No transactional EntityManager available" error. Appreciate if somebody finds the problem in below code.

data-context.xml file as below

  <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>

And AppConfig as Below.

@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;
    }
}

来源:https://stackoverflow.com/questions/34044011/no-transactional-entitymanager-available-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!