当前位置: 首页 > 知识库问答 >
问题:

spring:创建在类路径资源中定义名为“Entity ManagerFactory”的bean时出错

薛鹏飞
2023-03-14

我试图使用spring JPA在数据库中的两个不同表之间建立一个关系。但是,每次我尝试执行@OneTomany或@ManyToOne时,都会出现标题中提到的错误。

学生:

    package EIC.com.example.Backend.Entity;
    import org.hibernate.annotations.Cascade;
    import javax.persistence.*;

    @Entity
    @Table(name = "etudiant")

    public class Student {
        @Id
        @Column(name="Apogee")
        private int id;
        @Column(name="Prenom")
        private String first_name;
        @Column(name="Nom")
        private String last_name;
        @Column(name="Classement")
        private int rank;
        @Column(name="Filiere")
        private String speciality;
        @ManyToOne
        @Column(name = "Offre")
        private Offer school; 
        /* GETTERS SETTERS CONSTRUCTORS */
    }

报价:

    package EIC.com.example.Backend.Entity;
    import javax.persistence.*;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    @Entity
    public class Offer {
        @Id
        @Column(name = "ID_OFFRE")
        private int id;
        @Column(name = "Type")
        private int type; // 0 for DD 1 for MOBILITY
        @Column(name = "Etablissement")
        private String school;
        @Column(name = "Filiere_Concerne")
        private String target;
        @Column(name = "Nb_place")
        private int seats;
        @Column(name = "Delai")
        private Date limit;
        @OneToMany
        @JoinColumn(name = "Offre")
        private List<Student> students=new ArrayList<>();
        /* GETTERS SETTERS CONSTRUCTORS */
    }

依赖项:

http://maven.apache.org/xsd/maven-4.0.0.xsd“>4.0.0 org.springframework.Boot spring-boot-starter-parent 2.1.2.release EIC.com.example Backend 0.0.1-snapshot Backend EIC项目与spring boot和Angular

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </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-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-rest-hal-browser</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

null

共有1个答案

林德华
2023-03-14

U不能同时具有@column@ManyToOne

使用@joincolumn代替@column。您的学生类应为:

@Entity
@Table(name = "etudiant")

    public class Student {
        @Id
        @Column(name="Apogee")
        private int id;
        @Column(name="Prenom")
        private String first_name;
        @Column(name="Nom")
        private String last_name;
        @Column(name="Classement")
        private int rank;
        @Column(name="Filiere")
        private String speciality;
        @ManyToOne
        @JoinColumn(name = "Offre")
        private Offer school; 
        /* GETTERS SETTERS CONSTRUCTORS */
    }
 类似资料: