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

问题描述 投票:1回答:1

在我的springboot应用程序中,我试图向我的orcale数据库写入时得到以下错误。

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

到目前为止,我们一直在使用EntityManager来访问数据库,通过 createNativeQuery我们通过服务类属性上的@PersistenceContext注解注入@PersistenceContext EntityManager.我们从数据库读取没有问题,但现在我们找不到向数据库写入的方法。下面是写到数据库的方法的样子。

 EntityTransaction trans = em.getTransaction();
 trans.begin();

 em.createNativeQuery("insert into Employee (name,surname,) values('Phil','Bob'").executeUpdate();      
 trans.commit();

这是我的pom. xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath />
    </parent>
    <groupId>com.employee</groupId>
    <artifactId>Employee</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>employee</name>

    <repositories>
        <repository>
            <id>official-http-repo</id>
            <url>http://insecure.repo1.maven.org/maven2/</url>
        </repository>
    </repositories>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
    <dependency>                 <!-- ojdbc8.jar -->
        <groupId>com.oracle</groupId>
        <artifactId>0jdbc8</artifactId>
        <version>8.0.0</version>
    </dependency>

    <dependency>                 <!-- ons.jar -->
        <groupId>com.oracle</groupId>
        <artifactId>0ons8</artifactId>
        <version>8.0.0</version>
    </dependency>

        <dependency>                 <!-- oraclepki.jar -->
        <groupId>com.oracle</groupId>
        <artifactId>0oraclepki8</artifactId>
        <version>8.0.0</version>
    </dependency>

    <dependency>                 <!-- orai18n.jar -->
        <groupId>com.oracle</groupId>
        <artifactId>0orai18n8</artifactId>
        <version>8.0.0</version>
    </dependency>

        <dependency>                 <!-- osdt_cert.jar -->
        <groupId>com.oracle</groupId>
        <artifactId>0osdt_cert</artifactId>
        <version>8.0.0</version>
    </dependency>

    <dependency>                 <!-- osdt_core.jar -->
        <groupId>com.oracle</groupId>
        <artifactId>0osdt_core</artifactId>
        <version>8.0.0</version>
    </dependency>

    <dependency>                 <!-- simplefan.jar -->
        <groupId>com.oracle</groupId>
        <artifactId>0simplefan</artifactId>
        <version>8.0.0</version>
    </dependency>

    <dependency>                 <!-- ucp.jar -->
        <groupId>com.oracle</groupId>
        <artifactId>0ucp</artifactId>
        <version>8.0.0</version>
    </dependency>

    <dependency>                 <!-- xdb6.jar -->
        <groupId>com.oracle</groupId>
        <artifactId>0xdb6</artifactId>
        <version>8.0.0</version>
    </dependency>


    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.factories</resource>
                            </transformer>
                        </transformers>
                    </configuration>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这是我的代码。

@Service
@Persistent
public class ProjectService {
    @PersistenceContext
    private EntityManager em;
    private Session session;

    public void insertEmployee throws Exception
    {

         EntityTransaction trans = em.getTransaction();
         trans.begin();

         em.createNativeQuery("insert into Employee (name,surname,) values('Phil','Bob'").executeUpdate();      
         trans.commit();
    }
}
java hibernate spring-boot jpa entitymanager
1个回答
0
投票

你不能像这样启动事务。

你应该像这样使用声明式事务。

@Service
@Persistent
public class ProjectService {
    @PersistenceContext
    private EntityManager em;
    private Session session;

    @Transactional
    public void insertEmployee throws Exception
    {
         em.createNativeQuery("insert into Employee (name,surname,) values('Phil','Bob'").executeUpdate();      
    }
}

请阅读更多关于Spring文档中的事务。https:/docs.spring.iospringdocscurrentspring-framework-referencedata-access.html#transaction。

© www.soinside.com 2019 - 2024. All rights reserved.