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