java - "mappedBy reference an unknown target entity property"使用 hibernate

标签 java mysql hibernate

我是第一次使用 Hibernate,但无法让它与我的架构配合使用。

我收到“org.hibernate.AnnotationException:mappedBy引用未知目标实体属性:Faculty.allStudents中的Student.Faculty”。

有些学生共用一位导师。因此,我认为学生与教师的关系是多对一的,但我被告知这是一对一的关系。

学生.java

import javax.persistence.*;

@Entity  
@Table(name = "Student")  
@PrimaryKeyJoinColumn(name="StudentID")
public class Student extends Person
{
    @Column(name = "Classification")  
    private String Classification;

    @Column(name = "GPA")
    private double GPA;

    @OneToOne(mappedBy="Student")
    @JoinColumn(name = "FacultyID")
    private Faculty Mentor;

    @Column(name = "CreditHours")
    private int CreditHours;

    public Student()
    {

    }
    public Student(String studentID, String classification, double gpa, String mentorID, int creditHours)
    {
        //this.StudentID = studentID;
        this.Classification = classification;
        this.GPA = gpa;
        //this.MentorID = mentorID;
        this.CreditHours = creditHours;
    }
    public String getClassification()
    {
        return Classification;
    }
    public void setClassification(String classification)
    {
        this.Classification = classification;
    }
    public double getGPA()
    {
        return GPA;
    }
    public void setGPA(int gpa)
    {
        this.GPA = gpa;
    }
    public Faculty getMentor()
    {
        return Mentor;
    }
    public void setMentor(Faculty mentor)
    {
        this.Mentor = mentor;
    }
    public int getCreditHours()
    {
        return CreditHours;
    }
    public void setCreditHours(int creditHours)
    {
        this.CreditHours = creditHours;
    }
}

Faculty.java

import javax.persistence.*;
import java.util.Set;

@Entity  
@Table(name = "Faculty")  
@PrimaryKeyJoinColumn(name="FacultyID")
public class Faculty extends Person
{
    @Column(name = "Rank")  
    private String Rank;
    @Column(name = "Salary")
    private int Salary;

    @OneToMany(mappedBy="Faculty")
    private Set<Student> allStudents;

    public Faculty()
    {

    }
    public Faculty(String facultyID, String rank, int salary)
    {
        //this.FacultyID = facultyID;
        this.Rank = rank;
        this.Salary = salary;
    }
    public String getRank()
    {
        return Rank;
    }
    public void setName(String rank)
    {
        this.Rank = rank;
    }
    public int getSalary()
    {
        return Salary;
    }
    public void setSalary(int salary)
    {
        this.Salary = salary;
    }

    public Set<Student> getAllStudents()
    {
        return allStudents;
    }
    public void setAllStudents(Set<Student> allStuds)
    {
        this.allStudents = allStuds;
    }
}

数据库架构:

CREATE TABLE Person (
Name    char (20),
ID      char (9) NOT NULL,
Address char (30),
DOB     date,
PRIMARY KEY (ID));

CREATE TABLE Faculty (
FacultyID       char (9) NOT NULL,
Rank            char (12),
Salary          int,
PRIMARY KEY (FacultyID),
FOREIGN KEY (FacultyID) REFERENCES Person(ID));

CREATE TABLE Student (
StudentID       char (9) NOT NULL,
Classification  char (10),
GPA             double,
MentorID        char (9),
CreditHours     int,
PRIMARY KEY (StudentID),
FOREIGN KEY (StudentID) REFERENCES Person(ID),
FOREIGN KEY (MentorID) REFERENCES Faculty(FacultyID));

查看文档后,我尝试了几种不同的注释,但我不明白我做错了什么。

最佳答案

在您的Faculty实体中,您应该指向正确的属性,而不是类型:

@OneToMany(mappedBy="Mentor", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Student> allStudents;

另外,在您的 Student 实体中,与 Faculty 的关系是 ManyToOne,而不是 OneToOne

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MentorID")
private Faculty Mentor;

关于java - "mappedBy reference an unknown target entity property"使用 hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40644075/

相关文章:

java - 将平面集合映射到层次结构的最佳方法

php - 如何用 PHP 创建库存管理

Hibernate 作为 WebSphere 7 上的 JPA 2.0 提供者

java - 为什么 Hibernate 认为 @GeneratedValue 从根本上是错误的?

java - JPA 查询 ElementCollection 键

java - Android 打开 pdf 不工作

java - 我如何使用 JGit 找到分支的第一次提交?

php - 将表单数据插入数据库

php - mysql:如果表和/或字段不存在,则忽略 INSERT?

java - hibernate 一对多插入