Qwurticus
Qwurticus

Reputation: 897

"mappedBy reference an unknown target entity property" using Hibernate

I am using Hibernate for the first time and am having trouble getting it to work with my schema.

I am getting "org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: Student.Faculty in Faculty.allStudents".

Some students share a single mentor. I would think the student relation to faculty would be ManyToOne because of this, but I was told it is a OneToOne relation.

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

Database schema:

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

I have tried several different annotations after looking at documentation but I don't see what I am doing wrong.

Upvotes: 0

Views: 4875

Answers (2)

hurricane
hurricane

Reputation: 6724

First of all you need to define student and faculty relation.

  1. Many To Many: A student can have more than one faculty and a faculty can have more than one student.

That means you need to ManyToMany relation.

Student Table

id
name
bla
bla

Faculty Table

id
name
bla
bla

Faculty Stundent Relation Table

id
student_id
faculty_id

And here is your hibernate mapping:

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

Faculty.class

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "faculties")
    public Set<Student> getStudents() {
        return this.students;
    }
  1. Many to One: A student can have only one faculty and a faculty can have more than one student.

For this situation you can use @Matteo Baldi's solution.

Upvotes: 0

Matteo Baldi
Matteo Baldi

Reputation: 5828

In your Faculty entity you should point the right property, not the type:

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

plus, in your Student entity, the relationship with Faculty is ManyToOne, not OneToOne

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

Upvotes: 2

Related Questions