title: Hibernate and Foreign Key Constraints
body: <p>I have several Hibernate mapping files with many-to-many and many-to-one sets referring to other objects. I am trying to implement a back office scheme that can delete objects but I constantly run into java.sql.SQLIntegrityConstraintViolationException ...violation of foreign key ...  The statement has been rolled back.</p>
  
  <p>I am wondering what I need to do to delete these entries. here is an example of a one of my XML mapping files </p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
      &lt;hibernate-mapping package="com.floreantpos.model"&gt;
        &lt;class lazy="false" name="com.floreantpos.model.MenuItem" optimistic-lock="version" table="MENU_ITEM"&gt;
          &lt;id column="ID" name="id" type="java.lang.Integer"&gt;
            &lt;generator class="assigned"/&gt;
          &lt;/id&gt;
          &lt;version column="MODIFIED_TIME" name="modifiedTime" type="timestamp"/&gt;
          &lt;property column="NAME" length="30" name="name" not-null="true" type="java.lang.String"/&gt;
          &lt;property column="PRICE" length="52" name="price" not-null="true" type="java.lang.Double"/&gt;    
          &lt;property column="PLU" length="6" name="plu" not-null="false" type="java.lang.Integer"/&gt;
          &lt;property column="VIEW_ORDER_VALUE" name="viewOrderValue" type="java.lang.Integer"/&gt;
          &lt;property column="BUTTON_COLOR" name="buttonColor" type="java.lang.Integer"/&gt;
          &lt;property column="NEXT_SCREEN" length="30" name="nextScreen" type="java.lang.String"/&gt;
          &lt;property column="VISIBLE" name="visible" type="java.lang.Boolean"&gt;
            &lt;meta attribute="defaultValue"&gt;true&lt;/meta&gt;
          &lt;/property&gt;
          &lt;!-- uni-directional many-to-one association to FoodGroup --&gt;
          &lt;many-to-one class="com.floreantpos.model.MenuGroup" name="parent"&gt;
            &lt;column name="GROUP_ID"/&gt;
          &lt;/many-to-one&gt;
          &lt;list cascade="all" inverse="false" lazy="true" name="shifts" table="MENUITEM_SHIFT"&gt;
            &lt;key column="MENUITEM_ID"/&gt;
            &lt;list-index column="SHIFT_ORDER"/&gt;
            &lt;one-to-many class="MenuItemShift"/&gt;
          &lt;/list&gt;
          &lt;list cascade="all" inverse="false" lazy="true" name="menuItemModiferGroups"&gt;
            &lt;key column="MENUITEM_MODIFIERGROUP_ID"/&gt;
            &lt;list-index column="GR_ORDER"/&gt;
            &lt;one-to-many class="MenuItemModifierGroup"/&gt;
          &lt;/list&gt;
          &lt;!-- uni-directional many-to-one association to Tax --&gt;
          &lt;many-to-one class="com.floreantpos.model.Tax" lazy="false" name="tax"&gt;
            &lt;column name="TAX_ID"/&gt;
          &lt;/many-to-one&gt;
          &lt;set cascade="all" lazy="false" name="menuItemModifiers" table="MENUITEM_MENUMODIFIERS"&gt;
            &lt;key column="MENU_ITEM_ID"/&gt;
            &lt;many-to-many class="com.floreantpos.model.MenuModifier" column="MENU_MODIFIER_ID"/&gt;
          &lt;/set&gt;
        &lt;/class&gt;
      &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>I've seen posts regarding the @OnDelete(...) annotation but that doesn't seem to fit into the xml mapping scheme. In the deletion methods, I am currently looping through any set that contains a foreign key constraint and deleting each individual object, but I'm still having very little luck deleting.</p>