0

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...
}
VMAtm
  • 27,943
  • 17
  • 79
  • 125
  • 2
    Not what you asked, but: If the object hierarchy is yours to design, you have a classic opportunity to revise this one. You can consider designing a person who has (references to) roles, or a role that has a person to fill it, or both, rather than a person who IS a role. – Aaron Mansheim Apr 10 '17 at 19:48
  • Hmmmm. First of all, thank you for the answer. Yes it isn't what i want, but i think that you advice me to design a Person Class with References to Role objects as a List or a Collection generally speaking. This comes little far apart from the approach with Teachers that have relationship with Students and viceversa. What about build any kind of ManyToMany Relationship between subclasses in general that based on a primary key inherited from Superclass, which can define column in a new joined table ? – PanStath Apr 10 '17 at 21:18
  • You shouldn't be using an attribute override; why define the ID field in a superclass if it isn't really going to be used in the child classes? If you want a TeacherID and studentId to be child IDs, don't define one in the mappedSuperclass. Your join columns need to use the fields marked as ID, so if you leave it as is, it would need to use name="tid", referencedColumnName="id" and name="sid", referencedColumnName="td" – Chris Apr 11 '17 at 13:52
  • Thanks, i'll try this way instead! – PanStath Apr 12 '17 at 06:51

0 Answers0