1

I am using hibernate many to many association. i have 3 tables(STUDENT,COURSE and STUDENT_COURSE). among 3 tables 2 are main tables and one table is intermediate table for providing relation ship. when record is deleted from STUDENT then corresponding mapping is deleted from STUDENT_COURSE. my requirement is it should even delete record from COURSE table. consider STUDENT_COURSE entries as below:

S-Id  C_ID
101   201
102   202

when 101 is deleted form STUDENT table, first entry from above table is deleted but record corresponding to 201 in COURSE table stays as is. how can i delete that record? do i need to execute a delete query? does many to many relation ship does not take care? Below is my configuration:

<set name="course" table="STUDENT_COURSE" 
            inverse="false" lazy="false" fetch="join" cascade="all" >
            <key>
                <column name="S_Id" not-null="true" />
            </key>
            <many-to-many entity-name="COURSE">
                <column name="C_Id" not-null="true" />
            </many-to-many>
        </set>

Thanks!

user1016403
  • 12,151
  • 35
  • 108
  • 137
  • how does the relationship look like on student side? Do you also have cascade="all" there? – Korgen Jun 04 '12 at 13:23
  • @Korgen thanks for your reply. above configuration is of student hbm only. – user1016403 Jun 04 '12 at 13:24
  • sorry, I mean on course side... – Korgen Jun 04 '12 at 13:26
  • course side i dont have anything set. my requirement is, even if course is deleted, student should not be deleted but if student is deleted then course should be deleted. – user1016403 Jun 04 '12 at 13:27
  • what do you mean with "course side i dont have anything set"... you don't have a hbm file for course? Does your object graph get loaded correctly (i.e. if you load a student its courses will be populated)? – Korgen Jun 04 '12 at 13:33
  • I don't fully understand the need to delete the course. A course may have many students enrolled, so deleting the course as well does not seem the appropriate action. For instance, if I extend your example had (101,201), (102, 201) and (102, 202) records in the STUDENT_COURSE table, would you still want to delete the COURSE 201 record? What is the business logic driving your code? – rlinden Jun 04 '12 at 13:41

1 Answers1

1

You want to delete Corresponding Course also if I delete a Student, Ok Like you did the configuration in Student hbm on Student => STUDENT_COURSE mapping for inverse and cascade , the same you need to do in STUDENT_COURSE hbm also for STUDENT_COURSE => COURSE mapping.

manurajhada
  • 5,284
  • 3
  • 24
  • 43