不允许在共享EntityManager上创建事务 - 使用Spring事务或EJB CMT

Jåc*_*cob 30 spring hibernate jpa jpa-2.0

这篇文章是继续JPA如何在持久化后从数据库中获取值

当我执行以下操作时,我遇到以下异常,我该如何解决?

Not allowed to create transaction on shared EntityManager - use Spring 
transactions or EJB CMT
Run Code Online (Sandbox Code Playgroud)

DAOImpl代码

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());
    }
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

小智 40

我想这里的问题是虽然你已经为事务管理器定义了bean,但你还没有注释create()方法,@Transactional它启用了spring事务.

同样删除entityManager.getTransaction().commit();语句,因为现在所有的事务管理都将由spring处理,如果你保留语句,那么你将再次得到相同的错误.

  • 当您使用@PersistenceContext自动装入entitymanager时,即使我使用Transactional注释该方法并删除了commit语句,我仍会得到相同的错误 (4认同)

小智 9

entityManager.getTransaction().begin() and annotate the method using 您需要删除@Transactional语句吗?这使得 Spring 能够进行事务处理。


Ani*_*nil 5

在方法上注入EntityManagerFactory而不是EntityManager和javax.transaction.Transactional注释解决了我的问题,如下所示。

//Autowire EntityManagerFactory
@PersistenceUnit(unitName = "readwrite.config")
private EntityManagerFactory entityManagerFactory;


//Use below code on create/update
EntityManager entityManager = entityManagerFactory.createEntityManager();

entityManager.getTransaction().begin();
if (!ObjectUtils.isEmpty(entity) && !entityManager.contains(entity)) {
   entityManager.persist(entity);
   entityManager.flush();
}
entityManager.getTransaction().commit();
Run Code Online (Sandbox Code Playgroud)

  • 它也可以在不提及 `(unitName = "readwrite.config")` 的情况下工作,否则会出现错误 (3认同)
  • org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为“readwrite.config”的可用bean (2认同)