I'm using Hibernate 3.0 with Java.
I have the following classes.
Teacher.java
private long id;
private String teacherName;
private List<Student> students;
// getter-setter of all
Subject.java
private long id;
private String subjectName;
private List<Student> students;
// getter-setter of all
Student.java
private long id;
private String studentName;
// getter-setter of both
Teacher.hbm.xml
<class name="Teacher" table="teacher_master">
<!--other mappings-->
<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">
<!--other mappings-->
<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 id & studentName properties.
Problem I'm facing is:
I delete a row from student_master through Hibernate.
Student stu = new Student();
stu.setId(1l);
session.delete(stu);
transaction.commit();
But the references of the deleted student (id = 1) are not deleted from the tables teacher_student_master and subject_student_master.
How can I overcome this issue?
Note: It would be great if I can overcome this issue by doing some kind of configuration with Hibernate instead of coding & query firing.
Edit: I've seen this link. But in that, it's mentioned that I need to do some coding like, first get all the teachers associated with student=1, then remove the student=1 from the list of student & then update teacher. I want to avoid this coding. Is it possible?