【问题标题】:"mappedBy reference an unknown target entity property" using Hibernate使用 Hibernate 的“mappedBy 引用未知的目标实体属性”
【发布时间】:2016-11-16 23:20:18
【问题描述】:

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

我收到“org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: Student.Faculty in Faculty.allStudents”。

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

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

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

【问题讨论】:

    标签: java mysql hibernate


    【解决方案1】:

    在您的 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;
    

    【讨论】:

    • 我更改了它,但它现在显示“mappedBy 引用了一个未知的目标实体属性:Faculty.allStudents 中的 Student.Mentor”。
    【解决方案2】:

    首先你需要定义学生和教师的关系。

    1. 多对多:一个学生可以拥有多个教职员工,一个教职员工可以拥有多个学生。

    这意味着你需要多对多关系。

    学生桌

    id
    name
    bla
    bla
    

    教师表

    id
    name
    bla
    bla
    

    教师学生关系表

    id
    student_id
    faculty_id
    

    这是你的休眠映射:

    Student.class

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
        @JoinTable(name = "faculty_student_rel", catalog = "yourDb", joinColumns = {
                @JoinColumn(name = "STUDENT_ID", nullable = false, updatable = false) },
                inverseJoinColumns = { @JoinColumn(name = "FACULTY_ID",
                        nullable = false, updatable = false) })
        public Set<Faculty> getFaculties() {
            return this.faculties;
        }
    

    Faculty.class

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "faculties")
        public Set<Student> getStudents() {
            return this.students;
        }
    
    1. 多对一:一个学生只能有一个教员,一个教员可以有多个学生。

    对于这种情况,您可以使用@Matteo Baldi 的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2017-11-13
      相关资源
      最近更新 更多