I am using jpa. I establish a many to many relationship on Course and Student. So I have a bridge table CourseStudent.
The code in Course,java @Entity @Table(name = "COURSE") public class Course implements Serializable { @ManyToMany(fetch = FetchType.EAGER) @JoinTable( // name = "COURSESTUDENT", // joinColumns = { @JoinColumn(name = "COURSE_FK", nullable = false, updatable = false) }, // inverseJoinColumns = { @JoinColumn(name = "STUDENT_FK", nullable = false, updatable = false) }) public Set<Student> getStudents() { return students; }
public void setStudents(Set<Student> students) { this.students = students; } @Transient public void addStudent(Student student) { Set<Student> students = getStudents(); if(students == null) { students = new HashSet<Student>(0); setStudents(students); } students.add(student); }
@Transient public void removeStudent(Student student) { Set<Student> students = getStudents(); if(students != null) { students.remove(student); } } Code in Student.java @Entity @Table(name = "STUDENT") public class Student implements Serializable { @ManyToMany(fetch = FetchType.EAGER, mappedBy = "students") public Set<Course> getCourses() { return courses; }
public void setCourses(Set<Course> courses) { this.courses = courses; }
The problem is when I want to remove one student record from the student collection of the course, no corresponding record got removed. the code is looks like: course.removeStudent(student);
Any help would be appreciated. Thank you so much. Note: insert and update is fine.
|