我有以下情况:一张客户表可以有很多项目。这种关系是双向的。
我正在尝试从客户列表中删除客户,如果客户已经分配了一些项目,我得到了一个例外:
“我无法删除或更新父行:外键约束失败”
这意味着我必须先删除相关项目。
但是,我需要针对这种情况的解决方案,并且我认为问题与映射文件有关。以下是映射文件:
<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>