不允许在共享EntityManager上创建事务 - 使用Spring事务或EJB CMT [英] Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT
问题描述
这篇文章是继续 JPA如何获得数据库中的值后继续
当我执行以下我遇到以下异常,我该如何解决这个问题?
不允许在共享EntityManager上创建事务 - 使用Spring
事务或EJB CMT
$ b DAOImpl 代码
public void创建(项目项目){
entityManager.persist(project);
entityManager.getTransaction()。commit();
project = entityManager.find(Project.class,project.getProjectId());
entityManager.refresh(project);
System.out.println(Id - + project.getProjectId());
System.out.println(no - + project.getProjectNo());
}
applicationContext.xml
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http:/ /www.w3.org/2001/XMLSchema-instancexmlns:tx =http://www.springframework.org/schema/tx
xmlns:context =http://www.springframework.org / schema / context
xsi:schemaLocation =http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0 .xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http: //www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd\">
< bean id =DataSource
class =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =usernamevalue =scott/>
< property name =passwordvalue =tiger/>
< property name =driverClassNamevalue =oracle.jdbc.driver.OracleDriver/>
< / bean>
< bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =DataSource/>
< property name =packagesToScanvalue =test.entity/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =showSqlvalue =true/>
< property name =generateDdlvalue =false/>
< property name =databasePlatformvalue =org.hibernate.dialect.Oracle10gDialect/>
< / bean>
< / property>
< / bean>
< context:component-scan base-package =test.net/>
< tx:注解驱动的事务管理器=transactionManager/>
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
< context:annotation-config />
< / beans>
我猜这里的问题在于,事务管理器的bean,你没有使用 @Transactional
来注释create()方法,它可以实现spring事务。
同时删除 entityManager.getTransaction()。commit();
语句,因为所有事务管理都将在春季处理,如果您保留原样那么你将再次得到同样的错误。
This post is in continuation of JPA How to get the value from database after persist
When I execute the following I am getting following exception, how can I resolve this?
Not allowed to create transaction on shared EntityManager - use Spring
transactions or EJB CMT
DAOImpl code
public void create(Project project) {
entityManager.persist(project);
entityManager.getTransaction().commit();
project = entityManager.find(Project.class, project.getProjectId());
entityManager.refresh(project);
System.out.println("Id -- " + project.getProjectId());
System.out.println("no -- " + project.getProjectNo());
}
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="DataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="scott" />
<property name="password" value="tiger" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@myserver:1521:ORCL" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="packagesToScan" value="test.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
</bean>
</property>
</bean>
<context:component-scan base-package="test.net" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<context:annotation-config/>
</beans>
I guess the problem here is that although you have defined the bean for the transaction manager, you haven't annotated the create() method with @Transactional
which enables spring transactions.
Also remove the entityManager.getTransaction().commit();
statement as now all the transaction management will be handled by spring, if you leave the statement as it is then you will get the same error again.
这篇关于不允许在共享EntityManager上创建事务 - 使用Spring事务或EJB CMT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!