1

Parent Table

    <property name="buyerGroupName"    type="string"        column="BUYER_GROUP_NAME" />
    <property name="description"       type="string"        column="DESCRIPTION" />
    <property name="approvalPathId"    type="int"           column="APPROVAL_PATH_ID" />
    <property name="active"            type="int"           column="ACTIVE" />
    <property name="createdOn"         type="timestamp">
            <column name="CREATED_ON" length="20" not-null="true" />
    </property>

    <set name="buyers" table="buyers" cascade="all" >
        <key>
            <column name="BG_ID" not-null="true" />
        </key>
        <one-to-many class="com.sg.beans.Buyers" />
    </set>


</class>
  </hibernate-mapping>

Child Table Mapping

  <hibernate-mapping>
<class name="com.sg.beans.Buyers" table="buyers" >
    <id  name="id" type="int"  column="ID">
        <generator class="increment" />
    </id>

    <property    name="loginId" type="int" column="LOGIN_ID" />

    <many-to-one name="buyerGroup" class="com.sg.beans.BuyerGroup"    fetch="select" cascade="all"  >
        <column name="BG_ID" not-null="true" />
    </many-to-one> 

</class>
     </hibernate-mapping>

Delete Method .. Passing Parent object to delete

   public Boolean deleteBuyerGroup(BuyerGroup bg){

    try {
        session.delete(bg);
        session.getTransaction().commit();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

Below is the error

   buyerGroup.getBuyerGroupId()1

   Hibernate: update buyers set BG_ID=null where BG_ID=?
   org.hibernate.exception.ConstraintViolationException: Column 'BG_ID' cannot be null

I did debug and made sure that BG_ID is set to the value that it is expecting... Please help!

ashlesha
  • 109
  • 1
  • 2
  • 12

1 Answers1

1

I think you would have to use the all,delete-orphan value on the cascade property on the mapping of your parent entity. You also have to set the responsible of the bidirectional relationship by setting inverse="true" on the parent entity mapping.

benzonico
  • 10,635
  • 5
  • 42
  • 50
  • did you put : `cascade='all,delete-orphan'` ? – benzonico Feb 28 '13 at 22:12
  • yes I did all that cascade in parent.hbm file for the Buyers tag – ashlesha Feb 28 '13 at 22:18
  • I edited my answer as I see there is not the `inverse="true"` on your mapping. – benzonico Feb 28 '13 at 22:27
  • I did add that and now it does not throw any error .. I see in console that it is running the delete query.. but when I check the database (MYSQL) , the columns are still there in the parent and child table – ashlesha Feb 28 '13 at 22:31
  • I think that now you may have a transaction issue, check http://www.mkyong.com/hibernate/hibernate-transaction-handle-example/ – benzonico Feb 28 '13 at 22:40