I have gone through multiple questions on this subject and here are a few that I have taken:Hibernate Access Exception. Wikibooks and Another ManyToMany extra column on Hibernate
Now, here are my questions:
When doing the MayToOne mapping, do we need to follow the innerclass syntax on the OneToMany end? Say on the Wikibooks link, why does this link not use the innerclass reference on the OneToMany end? Observe that this uses the IdClass and not the Embeddable approach. Is that the difference? Any pointers on the Embeddable approach?
When persisting, what is the exact sequence? First create the PK, then set the member variables in the outer class, then persist the mapping class instance?
When zeroing in on the exact collection implementation, what are the bases of the choice? For example, I know that when we need to avoid duplicates, HashSet implementation is the option. Second, choosing an ArrayList vs a LinkedList is a choice that will be imposed by the additional domain requirements. What other criteria do I need to consider?
When composing the Pk class, what I know is that the member variables are of the corresponding basic datatypes and not the Entity classes. For example, on this link, the PK class directly uses the Student and TeachingClass. And this has solution-answer that works for Hibernate. (Does this follow the Specification? Cannot infer after reading the JSR).
I am on EclipseLink 2.6. Can anybody help me with a single source of truth?
Thanks in advance
EDIT 1: Here is the example sourceCode with questions. And thanks for the comment. It actually made me ask more questions (mentioned inline).
@Entity
@Table(name="Student")
public class Student{
@Id
@Column(name="roll_number")
private String rollNumber;
private String firstName;
private String lastName;
//Which one is right? The immediate line or the one after?
@OneToMany(mappedBy="student")
@OneToMany(mappedBy="StudentSubjectPK.studentRollNumber")
Collection<StudentSubjectMap> subjecttsForThisStudent;
//Now, when I drill down to the actual Collection implementation to select,
//what are the criteria(twomentioned already. Anymore??)
public Collection<StudentSubjectMap> getSubjectsForThisStudent(){
if(subjectsForThisStudent==null) !CollectionImplementation! subjectsForThisStudent = new !CollectionImplementation!();
return subjectsForThisStudent;
}
....
}
@Entity
@Table(name="Subject")
public class Subject{
@Id
@Column(name="subject_name")
private String subject;
private String teacherName;
private boolean isElective;
private boolean isGraded;
//Which one is right? The immediate line or the one after?
@OneToMany(mappedBy="subject")
@OneToMany(mappedBy="StudentSubjectPK.subjectName")
Collection<StudentSubjectMap> studentsEnrolledInThisSubject
....
}
@Entity
public class StudentSubjectMap{
@EmbeddedId
private StudentSubjectMapPk pk;
//Should the mapping be here or inside the StudentSubjectPK class?
@ManyToOne(targetEntity="Student.class")
//Is the PrimaryKeyJoinColumn annotation column in the next line redundant?
@PrimaryKeyJoinColumn(name="roll_number",referencedColumn="roll_number")
private Student student;
@ManyToOne(targetEntity="Subject.class")
@PrimaryKeyJoinColumn(name="subject_name",referencedColumn="subject_name")
private Subject subject;
//Do I need the setters and getters for the student and subject fields above?
private float finalGrade;
private float attendancePercentage;
@Embeddable
public static class StudentSubjectMapPK{
@Column(name="roll_number")
private String studentRollNumber;
@Column(name="subject_name")
private String subjectName;
//Are getters and setters necessary here? Why?
}
}