尝试使用EntityManager时出错
当前线程没有实际事务可用的EntityManager-无法可靠地处理'persist'调用
我使用Spring(不是boot)、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;
}
}
Spring配置类
@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);
}
}
使用find complete for error命令行
和我的主班
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();
}
}
哦,我的朋友。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(面向方面编程)来实现事务,这意味着您必须在MyTest2()方法上使用公共访问级别,以便AOP代理可以拦截它。否则,就像你从来没有注释你的方法与@Transactional
列出需要保存的数据。保存前必须删除现有数据并保存新数据。如果有任何删除 在这里,每次删除都会添加新事务 在删除时,它会引发异常“由以下原因引起:javax.persistence.TransactionRequiredException:没有具有实际事务的实体管理器可用于当前线程 - 无法可靠地处理'remove'调用” 我可以通过添加一个@ Transactional for public la
问题内容: 当尝试调用“ persist”方法将实体模型保存到Spring MVC Web应用程序中的数据库时,出现此错误。在Internet上找不到与该特定错误相关的任何帖子或页面。似乎似乎EntityManagerFactory bean出了点问题,但是我对Spring编程还是比较陌生,所以对我来说,似乎一切都已初始化好,并且根据Web上的各种教程文章。 dispatcher-servlet.
此错误发生在,当方法运行时。 向添加注释不能解决问题-https://stackoverflow.com/a/32552558/3536552 你知道怎么修吗?
我想发出POST请求以将用户保存到我的Oracle DB中。 我在jpa和hibernate中使用Spring MVC。 这是我的jpaContext.xml 这是我的UserRepoImpl类: 这是UserServiceImpl类: 这是来自REST控制器的方法: 我在服务图层上有注释。我尝试了许多在线找到的解决方案,使用存储库层上的@Transactional,或者使用@Scope(prox
在SpringMVCWeb应用程序中,试图调用“persist”方法将实体模型保存到数据库时,我遇到了这个错误。在internet上找不到任何与此特定错误相关的帖子或页面。看起来EntityManagerFactoryBean有点问题,但我对Spring编程相当陌生,所以对我来说,根据web上的各种教程文章,似乎一切都初始化得很好。 调度器servlet。xml 注册控制器。JAVA
问题内容: 项目使用Hibernate(JPA),Spring和Maven。我的实体和DAO在单独的JAR中。 pom.xml: 道: } 我有一个使用Spring的模块。 pom.xml: AppContext.xml: 服务: 当我尝试从EntityManager中获取会话时,遇到以下异常: 问题答案: 您必须在方法周围加上@Transactional批注: 并在您的Spring的xml配置文