I have a many to many association between two tables: STUDENT and
COURSE. I also have an association table say STUDENT_COURSE which contains sid and cid.
I used middlegen to generate the mapping files and persistent class
automatically.I get student.class, sc.class and course.class. I also get
student.hbm.xml,
<!-- associations -->
<!-- bi-directional one-to-many association to STUDENT_COURSE-->
<bag
name="STUDENT_COURSE"
lazy="false"
inverse="true"
cascade="all"
>
<key>
<column name="SID" />
</key>
<one-to-many
class="STUDENT_COURSE"
/>
</bag>
course.hbm.xml
<!-- associations -->
<!-- bi-directional one-to-many association to STUDENT_COURSE-->
<bag
name="STUDENT_COURSE"
lazy="false"
inverse="true"
cascade="all"
>
<key>
<column name="CID" />
</key>
<one-to-many
class="STUDENT_COURSE"
/>
</bag>
The problem occurs when I do add and delete. Using studentDAO.add(student)
and courseDAO.add(course) are quite straightforward. When I try to set up
the relation between student and course, I have to create cs object first
and then call Student_CourseDAO.add(cs). In the Student_CourseDAO, I not only do student.getcs.add(cs), course.getcs.add(cs). I also have to do
session.add(student_course) which is supposed to be transparent implemented by Hibernate.
When I try to delete a student record, the cs record referenced by it are
not deleted at all. I have to traverse the list and manually delete them
one by one. And the list in course still keeps the reference for those
records in student_course. I am totally lost in deletion.
Could anybody please give me any hint on this situation?
thanx a lot.
Ping
|