[英]How to correct the classpath of spring boot application so that it contains a single, compatible version of javax.persistence.PersistenceContext?
I am trying to add a participantRepository interface (which implements CrudRepository) to my spring boot application.我正在尝试向我的 spring 启动应用程序添加一个参与者存储库接口(它实现了 CrudRepository)。 But the basic code for this interface gives an error when it is run saying that the classpath of the application must be corrected.
但是这个接口的基本代码在运行时报错,说必须更正应用程序的类路径。 How can I do this and is there another modification I can do to run the code correctly?
我该怎么做,是否还有其他修改可以正确运行代码?
//Participant class //参与者类
package com.example.spring2.participants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.List;
@Entity
public class Participant {
@Id
private String name;
private int age;
private String job;
@Autowired
private ParticipentService participentService;
public Participant() {
}
public Participant(String name, int age, String job) {
this.name = name;
this.age = age;
this.job = job;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
//ParticipantController class //参与者控制器类
package com.example.spring2.participants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class ParticipantController {
@Autowired
private ParticipentService participentService;
@RequestMapping("/people")
public List<Participant> viewPeople(){
return participentService.getParticipants();
}
@RequestMapping("/people/{name}")
public Participant getAParticipant(@PathVariable String name) {
return participentService.getAParticipant(name);
}
@RequestMapping(method = RequestMethod.POST, value = "/people")
public void addParticipant(@RequestBody Participant participant){
participentService.addParticipant(participant);
}
@RequestMapping(method = RequestMethod.PUT, value = "/people/{name}")
public void updateParticipant(@RequestBody Participant participant, @PathVariable String name) {
participentService.updateParticipant(participant, name);
}
@RequestMapping(method = RequestMethod.DELETE, value = "/people/{name}")
public void deleteParticipant(@PathVariable String name) {
participentService.deleteParticipant(name);
}
}
//ParticipantService class //参与者服务类
package com.example.spring2.participants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
@Service
public class ParticipentService {
private List<Participant> participantList = Arrays.asList(
new Participant("yasas", 23, "Student"),
new Participant("anu", 28, "accountant"),
new Participant("banu", 26, "teacher")
);
@Autowired
private ParticipantRepository participantRepositoty;
public List<Participant> getParticipants(){
return participantList;
}
public void addParticipant(Participant participant) {
participantList.add(participant);
}
public void updateParticipant(Participant participant, String name ){
for (int i=0; i < participantList.size(); i++){
Participant p = participantList.get(i);
if (p.getName().equals(name)){
participantList.set(i, participant);
return;
}
}
}
public void deleteParticipant(String name ){
for (int i=0; i < participantList.size(); i++){
Participant p = participantList.get(i);
if (p.getName().equals(name)){
participantList.remove(p);
}
}
}
public Participant getAParticipant(String name) {
for (int i=0; i < participantList.size(); i++){
Participant p = participantList.get(i);
if (p.getName().equals(name)){
return p;
}
}
return null;
}
}
//POM.xml //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.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I expected the server to be started when the application is run.我希望在应用程序运行时启动服务器。 But it gives following error.
但它给出了以下错误。
Description:
An attempt was made to call the method javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType; but it does not exist. Its class, javax.persistence.PersistenceContext, is available from the following locations:
jar:file:/C:/Users/Asus/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.1.Final/hibernate-jpa-2.0-api-1.0.1.Final.jar!/javax/persistence/PersistenceContext.class
jar:file:/C:/Users/Asus/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.1-api/1.0.0.Final/hibernate-jpa-2.1-api-1.0.0.Final.jar!/javax/persistence/PersistenceContext.class
It was loaded from the following location:
file:/C:/Users/Asus/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.1.Final/hibernate-jpa-2.0-api-1.0.1.Final.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of javax.persistence.PersistenceContext
first remove in your repository maven the directory hibernate and then make a maven clean install
to reimport correctly your dependencies.首先在您的存储库中删除 maven 目录 hibernate,然后进行
maven clean install
以正确重新导入您的依赖项。 if it still doesn't fix your problem make a maven tree:dependency
to detect what dependence import the hibernate-jpa-2.0-api and exclude it in your pom如果它仍然不能解决您的问题,请创建一个
maven tree:dependency
以检测什么依赖项导入 hibernate-jpa-2.0-api 并将其排除在您的 pom 中
只需检查依赖树从项目依赖列表中的依赖中删除jar
Try going to the location where the dependency is been loaded under the .m2 folder and remove the specific folder hibernate-jpa-2.0-api尝试到.m2文件夹下加载依赖的位置,删除特定文件夹hibernate-jpa-2.0-api
Once you remove the folder try doing maven clean install .删除文件夹后,请尝试执行maven clean install 。
Build the project and run it again.构建项目并再次运行它。
The reason why it occurred because there were two version出现的原因是因为有两个版本
hibernate-jpa-2.0-api/ 1.0.0.Final
/hibernate-jpa-2.0-api-1.0.1.Final.jar hibernate-jpa-2.0-api/
1.0.0.Final
/hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-jpa-2.0-api/ 1.0.1.Final
/hibernate-jpa-2.0-api-1.0.1.Final.jar hibernate-jpa-2.0-api/
1.0.1.Final
/hibernate-jpa-2.0-api-1.0.1.Final.jar
and it should contain only a single, compatible version of javax.persistence.PersistenceContext它应该只包含一个单一的、兼容的 javax.persistence.PersistenceContext 版本
There are 4 steps to do that:有 4 个步骤可以做到这一点:
You must find the location of the dependency that you want to delete, in your I think does you want to remove hibernate-jpa-2.0-api-1.0.1.Final.jar您必须找到要删除的依赖项的位置,在您的 I think you want to remove hibernate-jpa-2.0-api-1.0.1.Final.jar
mvn dependency:tree
When you find the location you must put exclusions on the dependency which load the unwanted dependency such as当您找到该位置时,您必须对加载不需要的依赖项的依赖项进行排除,例如
<dependency> ... <exclusions> <exclusion> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </exclusion> </exclusions> </dependency>
In my case I clean my project such as在我的情况下,我清理我的项目,例如
mvn clean
You can verify if it's already present mvn dependency:tree | grep hibernate-jpa-2.0-api
您可以验证它是否已经存在
mvn dependency:tree | grep hibernate-jpa-2.0-api
mvn dependency:tree | grep hibernate-jpa-2.0-api
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.