我正在尝试通过Spring Boot应用程序使用JPA连接到Oracle数据库。我最初收到错误消息
java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType
我搜索了如何将其列举出来,并发现要解决相同的问题,我应该使用2.1.0版中的javax.persistence依赖项。
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.2.0</version>
</dependency>
更改相同内容后,该错误已解决,但现在又出现另一个错误:
java.lang.SecurityException: class "javax.persistence.SynchronizationType"'s signer information does not match signer information of other classes in the same package
这是我的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.in28minutes</groupId>
<artifactId>in28Minutes-first-webapp</artifactId>
<version>2.1.3.RELEASE</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2.2</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>12.1.0.2</version>
</dependency>
<!-- Java 8 = JAX-B Version 2.2.8 added to correct missing entity manager error-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<contextReloadable>true</contextReloadable>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
我的应用程序类:
package com.myfirst.classes.from.db;
import java.util.Date;
//import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
//import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Component;
//import com.example.database.databasejdbcdemo.SpringDataDemoApplication;
/*@EnableAutoConfiguration(exclude = {
JndiConnectionFactoryAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
SecurityAutoConfiguration.class,DataSourceAutoConfiguration.class })*/
//@EnableJpaRepositories(basePackages="com.myfirst.classes.from.db")
@ComponentScan("com.myfirst.classes.from.db")
@SpringBootApplication
public class SpringDataDemoApplication implements CommandLineRunner{
//private org.slf4j.Logger logger= LoggerFactory.getLogger(this.getClass());
@Autowired
PersonSpringDataRepository repository ;
public static void main(String[] args) {
SpringApplication.run(SpringDataDemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("hello");
//logger.info("Person with ID 10002 ->{}",repository.findById(10002));
//logger.info("Updating person with ID 10002 ->{}",repository.save(new Person(10002,"Kunjol","Home",new Date())));
//logger.info("Person with ID 10002 ->{}",repository.findById(10002));
//logger.info("Inserting a new person now ->{}",repository.save(new Person(10007,"Karthik","Home",new Date())));
//logger.info("Person with ID 10007 ->{}",repository.findByName("Karthik"));
//repository.deleteById(10001);
//logger.info("Person with ID 10001 ->{}",repository.findById(10001));
}
}
PersonSpringDataRepository.java
package com.myfirst.classes.from.db;
import javax.persistence.EntityManagerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Component
@Transactional
public interface PersonSpringDataRepository extends JpaRepository<Person,Integer> {
}
还有Person.java
package com.myfirst.classes.from.db;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
@Entity
@Table(name="person")
@Configuration
@ConfigurationProperties("oracle")
public class Person {
// private EntityManagerFactory entityManagerFactory;
@Id
@GeneratedValue
// @Bean
private int id;
private String name;
private String location;
private Date birth_date;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Date getBirth_date() {
return birth_date;
}
public void setBirth_date(Date birth_date) {
this.birth_date = birth_date;
}
public Person(int id, String name, String location, Date birth_date) {
super();
this.id = id;
this.name = name;
this.location = location;
this.birth_date = birth_date;
}
public Person( String name, String location, Date birth_date) {
super();
this.name = name;
this.location = location;
this.birth_date = birth_date;
}
public Person()
{
//By default, Spring will try to instantiate beans by calling a default (no-arg) constructor.
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", location=" + location + ", birth_date=" + birth_date + "]";
}
}
该问题的可能原因是什么?任何帮助将不胜感激。
答案 0 :(得分:0)
我现在有相同的错误。根本原因是eclipselink jar包含类javax.persistence.SynchronizationType,但它也带来了另一个依赖项(jakarta.persistence),该依赖项也包含相同的类-实际使用的依赖项来自另一个jar(并由其他证书签名) ),而不是其他产品。
话虽如此,我找不到一个直接的解决方案。 IMO的问题是eclipselink两次将同一个类引入,直到解决该问题为止,我们只能寻找解决方法。
编辑:这是另一个具有解决方案的线程(从eclipselink中排除JPA API,并引入另一个未签名的线程)
答案 1 :(得分:0)
最终排除了jakarta.persistence,转而使用了hibernate-jpa-2.1-api jar!
直到EL团队活跃并响应https://bugs.eclipse.org/bugs/show_bug.cgi?id=549240