Hi,
I have got a many-to-many relationship between entities instructor and student. The student is saved and persisted first. Then I create a new instructor and try to add the student to the studentList in instructor. Then I save (persist) the instructor. The instructor is persisted but the jointable is empty. Any ideas?
@Entity
@Table(name = "instructor")
@PersistenceContext(unitName = "processEntities")
public class Instructor extends Person {
private static final long serialVersionUID = 1L;
@ManyToMany
@JoinTable(
name = "instructor_student", joinColumns = @JoinColumn(name = "instructor_id"),
inverseJoinColumns = @JoinColumn(name = "student_id")
)
private List<Student> students = new ArrayList<Student>();
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
ThesisFirstInstructor.java
@Entity
@Table(name = "thesisfirstinstructor")
@PersistenceContext(unitName = "processEntities")
public class ThesisFirstInstructor extends Instructor {}
Student.java
@Entity
@Table(name = "student")
@PersistenceContext(unitName = "processEntities")
public class Student extends Person {
private static final long serialVersionUID = 1L;
private String matrNo;
public String getMatrNo() {
return matrNo;
}
public void setMatrNo(String matrNo) {
this.matrNo = matrNo;
}
BusinessLogic.java (EJB)
{
...
Student currentStudent= studentDAO.findByThmUid(currentStudentThmUid);
ThesisFirstInstructor thesisFirstInstructor = new ThesisFirstInstructor();
thesisFirstInstructor.setFirstName(thesisFirstInstructorCamundaUser.getFirstName());
thesisFirstInstructor.setLastName(thesisFirstInstructorCamundaUser.getLastName());
thesisFirstInstructor.setThmUid(thesisFirstInstructorThmUid);
thesisFirstInstructor.getStudents().add(currentStudent);
thesisFirstInstructorDAO.persist(thesisFirstInstructor);
}
Unfortunately, the join table is empty![grafik|261x170]:
Entity thesisFirstInstructor is persisted.
Any ideas?