2016-11-16 77 views
0

我第一次使用Hibernate,並且無法使其與我的模式一起工作。使用休眠「mappedBy引用一個未知的目標實體屬性」

我得到「org.hibernate.AnnotationException:mappedBy引用未知目標實體屬性:Student.Faculty in Faculty.allStudents」。

有些學生分享一位導師。因爲這個原因,我認爲學生與教師的關係應該是ManyToOne,但我被告知這是OneToOne關係。

Student.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)); 

我嘗試了好幾種不同的註解看文件之後,但我沒有看到我做錯了什麼。

回答

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; 
+0

我改變了它,但它現在說「mappedBy引用一個未知的目標實體屬性:Faculty.allStudents中的Student.Mentor」。 – Qwurticus

0

第一你需要定義學生和教師關係。

  1. 多對多:學生可以有一個以上的教師和一名教師可以有不止一個學生。

這意味着你需要ManyToMany關係。

學生表

id 
name 
bla 
bla 

系表

id 
name 
bla 
bla 

學院Stundent關係表

id 
student_id 
faculty_id 

這裏是你的Hibernate映射:

Stundent.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; 
    } 

學院。類

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "faculties") 
    public Set<Student> getStudents() { 
     return this.students; 
    } 
  • 多對一:學生只能有一個教師和教員可以有一個以上的學生。
  • 對於這種情況,您可以使用@Matteo Baldi的解決方案。

    相關問題