我是一個hibernate新手,我不完全知道如何獲得我期待的級聯行爲。休眠多對多級聯不起作用
我有兩個班學生和類與單向多對多映射。當我刪除一個學生,我有此異常
Cannot delete or update a parent row: a foreign key constraint fails (projet.T_CLASS_STUDENT, CONSTRAINT FK5DBF3D8967BCDD8B FOREIGN KEY (PERSON_ID) REFERENCES T_STUDENT (PERSON_ID))
我不明白爲什麼,我設置級聯「刪除」,但它不工作! 事實上,當我刪除一個學生時,我想刪除所有在asoociation表中的Student。
我的映射文件是:
<class name="persistenceClass.Class" table="T_CLASS">
<id name="Id" column="CLASS_ID">
<generator class="native" />
</id>
<many-to-one name="Formation" column="CLASS_FORMATION" class="persistenceClass.Formation" />
<many-to-one name="Year" column="CLASS_YEAR" class="persistenceClass.Year" />
<set name="Students" table="T_CLASS_STUDENT" cascade="delete" >
<key column="CLASS_ID" />
<many-to-many class="persistenceClass.Student" column="PERSON_ID" />
</set>
</class>
和:
<class name="persistenceClass.Person" table="T_PERSON" >
<id name="Id" column="PERSON_ID" >
<generator class="native" />
</id>
<property name="FirstName" column="PERSON_FIRST_NAME" not-null="true" />
<property name="LastName" column="PERSON_LAST_NAME" not-null="true" />
<property name="Type" column="PERSON_TYPE" not-null="true" />
<property name="BirthDate" column="PERSON_BIRTH_DATE" />
<property name="BirthCity" column="PERSON_BIRTH_CITY" />
<property name="PhoneNumber" column="PERSON_PHONE_NUMBER" />
<property name="MobileNumber" column="PERSON_MOBILE_NUMBER" />
<property name="Mail" column="PERSON_MAIL" />
<property name="Address" column="PERSON_ADDRESS_ADDRESS" />
<property name="ZipCode" column="PERSON_ADDRESS_ZIPCODE" />
<property name="City" column="PERSON_ADDRESS_CITY" />
<property name="Image" column="PERSON_IMAGE" type="image" />
<many-to-one name="Country" column="PERSON_ADDRESS_COUNTRY" class="persistenceClass.Country" />
<many-to-one name="BirthCountry" column="PERSON_BIRTH_COUNTRY" class="persistenceClass.Country" />
<many-to-one name="Civility" column="PERSON_CIVILITY" class="persistenceClass.Civility" />
<many-to-one name="Sex" column="PERSON_SEX" class="persistenceClass.Sex" />
<joined-subclass name="persistenceClass.Student" table="T_STUDENT">
<key column="PERSON_ID" />
</joined-subclass>
<joined-subclass name="persistenceClass.Teacher" table="T_TEACHER">
<key column="PERSON_ID" />
</joined-subclass>
</class>
感謝很多像你描述