I am using Hibernate 3.0 with Java.
I have the following classes.
Teacher.java
private long id; private String teacherName; private List<Student> students;
Subject.java
private long id; private String subjectName; private List<Student> students;
Student.java
private long id; private String studentName;
Teacher.hbm.xml
<class name="Teacher" table="teacher_master"> <list name="students" cascade="refresh" table="teacher_student_master"> <key column="teacher_id"/> <index column="student_teacher_position" type="integer"/> <many-to-many class="Student" column="student_id"/> </list> </class>
Subject.hbm.xml
<class name="Subject" table="subject_master"> <list name="students" cascade="refresh" table="subject_student_master"> <key column="subject_id"/> <index column="student_subject_position" type="integer"/> <many-to-many class="Student" column="student_id"/> </list> </class>
Student.hbm.xml contains mappings for the id and studentName properties .
The problem I am facing:
I am deleting a row from student_master via Hibernate.
Student stu = new Student(); stu.setId(1l); session.delete(stu); transaction.commit();
But references of remote students (id = 1) are not deleted from the tables teacher_student_master and subject_student_master .
How can I solve this problem?
Note. It would be great if I could overcome this problem by doing some kind of configuration with Hibernate instead of coding and request.
Edit: I saw this link. But in this he mentioned that I need to do some encoding, for example, first get all the teachers associated with the student = 1, and then remove the student = 1 from the list of students, and then update the teacher. I want to avoid this coding. Is it possible?