Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更正spring启动应用程序的类路径,使其包含一个兼容的javax.persistence.PersistenceContext版本?_Java_Spring Boot_Spring Data Jpa - Fatal编程技术网

如何更正spring启动应用程序的类路径,使其包含一个兼容的javax.persistence.PersistenceContext版本?

如何更正spring启动应用程序的类路径,使其包含一个兼容的javax.persistence.PersistenceContext版本?,java,spring-boot,spring-data-jpa,Java,Spring Boot,Spring Data Jpa,我正在尝试向我的spring boot应用程序添加一个participantRepository接口(它实现了Crudepository)。但是该接口的基本代码在运行时给出了一个错误,说明必须更正应用程序的类路径。我怎样才能做到这一点?是否还有其他修改可以正确运行代码 //学员班 package com.example.spring2.participants; import org.springframework.beans.factory.annotation.Autowired; imp

我正在尝试向我的spring boot应用程序添加一个participantRepository接口(它实现了Crudepository)。但是该接口的基本代码在运行时给出了一个错误,说明必须更正应用程序的类路径。我怎样才能做到这一点?是否还有其他修改可以正确运行代码

//学员班

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;
    }

}
//参与者控制器类

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);
    }

}

首先在存储库maven中删除hibernate目录,然后进行
maven clean安装
,以正确地重新导入依赖项。
如果它仍然不能解决您的问题,那么制作一个
maven tree:dependency
来检测什么依赖项导入hibernate-jpa-2.0-api并将其排除在pom中

只需检查依赖项树,从项目依赖项列表中删除jar即可

尝试转到项目依赖项下加载依赖项的位置.m2文件夹并删除特定文件夹hibernate-jpa-2.0-api

删除文件夹后,请尝试执行maven clean安装

构建项目并再次运行它

发生的原因是因为有两个版本

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


它应该只包含javax.persistence.PersistenceContext的一个兼容版本

检查作为参考的属性和依赖项文件,并从:
C:/Users/Asus/.m2/repository/org/hibernate/
文件夹中删除文件,然后重试构建项目。在某些情况下有效。你的代码看起来很庞大-你确定它是一个吗?我尝试了maven clean安装。我没有工作。然后,我删除了从pom导入hibernate-jpa-2.0-api的依赖项。但现在我得到了这个错误。创建在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定义的名为“entityManagerFactory”的bean时出错
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;
    }
}
<?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>
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