Reputation: 203
have error when i try use persist with my EntityManager
No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
i use Spring (not boot), jpa, hibernate
Can anybody help me?
entity Course
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 config class
@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;
}
}
Test "Dao" where i get erorr
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);
}
}
line with find complete without error
And my Main class
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();
}
}
oh, my 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>
I used @Transactional over class and method, the result is not changed
please help me, I went through all the options.
Upvotes: 0
Views: 14971
Reputation: 1112
Spring use Spring AOP (Aspect Oriented Programming) to implement transaction which means you must use public
access level on your MyTest2()
method so it can be intercepted by AOP proxy. Otherwise it is like you never annotate your method with @Transactional
Upvotes: 12