在hibernate中以一对多关系从父表中删除记录



我有以下情况:一张客户表可以包含多个项目。这种关系是双向的。

我正试图从客户列表中删除一个客户,如果客户已经分配了一些项目,我会得到一个例外:

"我无法删除或更新父行:外键约束失败"

这意味着我必须首先删除相关项目。

然而,我需要一个解决方案来解决这种情况,我想这个问题与映射文件有关。以下是映射文件:

<hibernate-mapping>
<class name="com.nisid.entities.Customers" table="customers">
<meta attribute="class-description"> This class contains the customer records. </meta> 
<id name="customerId" type="int" column="customerId"> 
    <generator class="native"/> 
</id> 
<many-to-one name="fkuser" class="com.nisid.entities.Worker"
        fetch="select">
        <column name="fk_userID" not-null="true"/>
</many-to-one>
<set name="projects" lazy="false" inverse="true" fetch="select" cascade="all" > 
<key column="customerId" not-null="true"/> 
<one-to-many class="com.nisid.entities.Project"/> 
</set>
<property name="customerName" column="customerName" type="string"/> 
<property name="customerSurname" column="customerSurname" type="string"/> 
<property name="adress" column="adress" type="string"/> 
<property name="email" column="email" type="string"/>
<property name="phoneNumber" column="contactDetails" type="string"/>
</class>

</hibernate-mapping>

和孩子:

<hibernate-mapping>
<class name="com.nisid.entities.Project" table="projects">
<meta attribute="class-description"> This class contains the project records. </meta> 
<id name="projectId" type="int" column="projectId"> 
    <generator class="native">
    </generator> 
</id> 
<many-to-one name="fkCustomer" class="com.nisid.entities.Customers"
        fetch="select">
        <column name="customerId" not-null="true"/>
</many-to-one>

<set name="payments" lazy="true"  fetch="select" inverse="true" > 
<key column="projectId" not-null="true"/> 
<one-to-many class="com.nisid.entities.Payment"/> 
</set>

<property name="projectName" column="projectName" type="string" /> 
<property name="projectDescription" column="description" type="string"/> 

</class>

</hibernate-mapping>

将项目上的级联设置为"all-delete-overn",而不是"all"

相关内容

  • 没有找到相关文章

最新更新