0

I have the following situation: One table of Customers can have many projects. the relationship is bidirectional.

I am trying to delete a customer from a list of Customers and if the customers has already assigned with some projects I got an exception:

" that I cannot delete or update a parent row: a foreign key constraint fails"

meaning that I have first to delete the relevant projects.

However I need a solution for that case and I suppose the problem is related with mapping files. Here are the mapping files:

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

And the child:

<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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nikos Sideris
  • 27
  • 2
  • 7

1 Answers1

0

Change the cascade on your projects set to "all-delete-orphan" instead of "all"

bjc2406
  • 85
  • 2
  • i have thge same error again ... : WARNING: SQL Error: 1451, SQLState: 23000 SEVERE: Cannot delete or update a parent row: a foreign key constraint fails (`customer`.`payments`, CONSTRAINT `payments_ibfk_1` FOREIGN KEY (`projectId`) REFERENCES `projects` (`projectId`)) SEVERE: Could not synchronize database state with session – Nikos Sideris Aug 04 '13 at 17:44
  • i solved it the table project is related withanother table also that's why i had the problem ;) – Nikos Sideris Aug 04 '13 at 18:01