我是第一次使用 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/