java - spring + jpa+hibernate = 没有 EntityManager 与当前线程可用的实际事务 - 无法可靠地处理“持久”调用
问题描述
当我尝试对我的 EntityManager 使用持久性时出现错误
No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
我使用 Spring(不是引导)、jpa、hibernate
有谁能够帮助我?
实体课程
package com;
import javax.persistence.*;
@Entity
@Table(name="courses")
public class Course {
private int id;
@Column(name = "title")
private String title;
@Column(name = "length")
private int length;
@Column(name = "description")
public String description;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Column(name="length")
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
@Column(name="description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
弹簧配置类
@Configuration
@ComponentScan(basePackages = "com")
@EnableTransactionManagement
public class AppConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/lib33");
dataSource.setUsername("postgres");
dataSource.setPassword("root");
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[]{"com"});
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(hibernateProperties());
return em;
}
@Bean
public PlatformTransactionManager txManager(EntityManagerFactory emf) {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(emf);
txManager.setDataSource(dataSource());
return txManager;
}
private Properties hibernateProperties() {
Properties props = new Properties();
props.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL94Dialect");
props.setProperty("hibernate.hbm2ddl.auto", "update");
props.setProperty("hibernate.globally_quoted_identifiers", "true");
props.setProperty("hibernate.show_sql", "true");
return props;
}
}
测试我出错的“道”
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Repository
@Transactional
public class MyTest3 {
@PersistenceContext
private EntityManager entityManager;
void MyTest2(){
System.out.println("hello");
Course course2 = new Course();
course2.setDescription("test desc");
course2.setTitle("test title");
entityManager.persist(course2); // <--- ERROR
// System.out.println(entityManager.find(Course.class, 1).description);
}
}
与查找完成一致且没有错误
还有我的主班
public class Main {
public static void main(String[] args) throws SQLException {
System.out.println("hello");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyTest3 myTest3 = context.getBean(MyTest3.class);
myTest3.MyTest2();
}
}
哦,我的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test9</groupId>
<artifactId>test9</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>test9 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.13.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.13.Final</version>
</dependency>
</dependencies>
<build>
<finalName>test9</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
我在类和方法上使用了@Transactional,结果没有改变
请帮助我,我经历了所有的选择。
解决方案
Spring 使用 Spring AOP(面向方面编程)来实现事务,这意味着您必须在方法public
上使用访问级别,MyTest2()
以便它可以被 AOP 代理拦截。否则就像你从来没有用@Transactional
推荐阅读
- geolocation - 如何在cefsharp浏览器上允许地理定位访问?
- arrays - 从生成器填充一个已知大小的字节数组
- javascript - 如何在 Odoo 14 中为字段设置默认值
- java - JPA - 在 EclipseLink 中的 namedQuery 中忽略连接操作
- r - 从列表中提取一系列值,但不包括 R 中的一些值
- bash - Bash:在管道中执行两个命令之间记录一条消息
- blazor - 为什么在将增量器传递给 Blazor 事件处理程序时总是得到它的最后一个值
- spring - 为什么不推荐使用 StreamListener
- netbeans - Gluon 单视图客户端:构建
- javascript - JS如何深拷贝两个对象