开发者

hibernate many-to-many cascade doesn't work

开发者 https://www.devze.com 2023-01-26 03:55 出处:网络
I\'m a hibernate newbie and I\'m not entirely sure how to get the cascade behavior I\'m looking for. I have two classes Student and Class with unidirectional many-to-many mapping. When I delete a Stu

I'm a hibernate newbie and I'm not entirely sure how to get the cascade behavior I'm looking for.

I have two classes Student and Class with unidirectional many-to-many mapping. When I delete a Student, I've this exception

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))

I don't understand why, I set cascade to "delete" but it's doesn't work ! In fact, when I delete a student I want to开发者_如何学编程 delete all Student which are in the association table.

My mapping files are:

<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>

and:

<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>


As you described, you have an unidirectional many-to-many relationship between class and student with a cascade on delete. That means cascade on delete only works if you delete a class and not a student. If you want to delete a student, you have to first remove it from the collection in the corresponding class object. Otherwise you get the exception described in your question.

If you delete a class all students in that class should be deleted as well - unless these students participate in other associations.


I dont know, if I fully understand your code (I am using annotations in my project). But you have an unidirected association class => student with implicit join table. When you delete student, there is no way for the person entity to delete itself from the association.

This way of making many to many isnt recomended in documentation. Recomended is to explicitly specify the joining entity, bacause its more flexible. You can then easily query or delete the association, and you cas assign association parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消