0

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.

I am wondering what I need to do to delete these entries. here is an example of a one of my XML mapping files

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.floreantpos.model">
      <class lazy="false" name="com.floreantpos.model.MenuItem" optimistic-lock="version" table="MENU_ITEM">
        <id column="ID" name="id" type="java.lang.Integer">
          <generator class="assigned"/>
        </id>
        <version column="MODIFIED_TIME" name="modifiedTime" type="timestamp"/>
        <property column="NAME" length="30" name="name" not-null="true" type="java.lang.String"/>
        <property column="PRICE" length="52" name="price" not-null="true" type="java.lang.Double"/>    
        <property column="PLU" length="6" name="plu" not-null="false" type="java.lang.Integer"/>
        <property column="VIEW_ORDER_VALUE" name="viewOrderValue" type="java.lang.Integer"/>
        <property column="BUTTON_COLOR" name="buttonColor" type="java.lang.Integer"/>
        <property column="NEXT_SCREEN" length="30" name="nextScreen" type="java.lang.String"/>
        <property column="VISIBLE" name="visible" type="java.lang.Boolean">
          <meta attribute="defaultValue">true</meta>
        </property>
        <!-- uni-directional many-to-one association to FoodGroup -->
        <many-to-one class="com.floreantpos.model.MenuGroup" name="parent">
          <column name="GROUP_ID"/>
        </many-to-one>
        <list cascade="all" inverse="false" lazy="true" name="shifts" table="MENUITEM_SHIFT">
          <key column="MENUITEM_ID"/>
          <list-index column="SHIFT_ORDER"/>
          <one-to-many class="MenuItemShift"/>
        </list>
        <list cascade="all" inverse="false" lazy="true" name="menuItemModiferGroups">
          <key column="MENUITEM_MODIFIERGROUP_ID"/>
          <list-index column="GR_ORDER"/>
          <one-to-many class="MenuItemModifierGroup"/>
        </list>
        <!-- uni-directional many-to-one association to Tax -->
        <many-to-one class="com.floreantpos.model.Tax" lazy="false" name="tax">
          <column name="TAX_ID"/>
        </many-to-one>
        <set cascade="all" lazy="false" name="menuItemModifiers" table="MENUITEM_MENUMODIFIERS">
          <key column="MENU_ITEM_ID"/>
          <many-to-many class="com.floreantpos.model.MenuModifier" column="MENU_MODIFIER_ID"/>
        </set>
      </class>
    </hibernate-mapping>

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    1. Simplify your mapping and find out what relationship causes this. 2. Examine the exception, it usually says where is the error. 3. See sql server log to find out what is not deleted while it needs to be. – Vitaly Apr 17 '13 at 06:23
  • thank you for your response, i found the foreign keys that are causing the error, i guess what my question is, is there an easier way to automatically delete any associated db entries other than looping through and deleted objects referencing the item being deleted? – Teh Good God Apr 17 '13 at 18:23
  • this might help http://stackoverflow.com/questions/306144/jpa-cascadetype-all-does-not-delete-orphans – Vitaly Apr 17 '13 at 18:26

0 Answers0