I face a problem with building a ManyToMany Relationship between subclasses-entities of my app, which inherit the id (database id) field from superclass and also other 2 variables. @MappedSuperclass
used for mapping only the sublasses on the database tables, a single table for each subclass Entity
. Finally i want to produce a Joined Table builded on the ManyToMany Relationship that i mentioned.
I can't figure out the definition of the JoinTable
Annotation and i tried to use @AttributeOverride
in order to override the inherited id, in each subclass.
Can you guide me please?
Person
SUPERCLASS:
@MappedSuperclass
abstract public class Person {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false, unique=true)
private int id;
@Column(name = "First_Name")
private String firstName;
@Column(name = "Last_Name")
private String lastName;
//Constructors Goes Here...
//Getters and Setters Goes Here....
}
Teacher
SUBCLASS:
@Entity
@Table(name="Teacher")
@AttributeOverride(name = "id", column = @Column(name = "tid"))
public class Teacher extends Person {
@Column(name="Teacher_Id", unique=true)
private int teacherId;
@Column(name="Years_Experience")
private double yearsExperience;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="Teachers_Students",
joinColumns=@JoinColumn(name="tid", referencedColumnName="tid"),
inverseJoinColumns=@JoinColumn(name="sid", referencedColumnName="sid"))
private ArrayList<Student> students = null;
//Constructors Goes Here...
//Getters and Setters Goes Here...
}
Student SUBCLASS:
@Entity
@Table(name="Student")
@AttributeOverride(name = "id", column = @Column(name = "sid"))
public class Student extends Person {
@Column(name="Student_Id", nullable = false, unique=true)
private int studentId;
@Column(name="Year_Average_Grade")
private double yearAverageGrade;
@ManyToMany(mappedBy = "students", cascade = CascadeType.ALL)
ArrayList<Teacher> teachers= null;
//Constructors Goes Here...
//Getters and Setters Goes Here...
}