我是第一次使用Hibernate,但很难使其与我的模式一起使用。

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

一些学生共享一个导师。因此,我认为学生与教师的关系将是ManyToOne,但有人告诉我这是一个OneToOne关系。

学生.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;
    }
}


学院

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;

10-06 05:42