0
我有以下情況: 客戶的一個表可以有許多項目。這種關係是雙向的。在休眠狀態下以一對多關係從父表中刪除記錄
我試圖刪除客戶列表的客戶,如果客戶已經有一些項目分配我有一個例外:
" that I cannot delete or update a parent row: a foreign key constraint fails"
這意味着我必須先刪除相關的項目。
但是我需要一個解決方案,我想這個問題與映射文件有關。以下是映射文件:
<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>
我有thge再次出現同樣的錯誤...: 警告:SQL錯誤:1451,SQLState:23000 SEVERE:無法刪除或更新父行:外鍵約束失敗('customer'.'payments',CONSTRAINT'payments_ibfk_1' FOREIGN KEY ('projectId')參考'項目' ('projectId')) SEVERE:無法與會話 –
同步數據庫狀態我解決了它的表項目與withanother表有關,也就是爲什麼我有這個問題;) –