【发布时间】:2020-04-23 12:50:11
【问题描述】:
尝试在我的 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