我有几个 Hibernate 映射文件,其中包含引用其他对象的多对多和多对一集。我正在尝试实现一个可以删除对象的后台方案,但我经常遇到 java.sql.SQLIntegrityConstraintViolationException ...违反外键 ...该语句已回滚。
我想知道我需要做什么才能删除这些条目。这是我的一个 XML 映射文件的示例
<?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>
我看过有关 @OnDelete(...) 注释的帖子,但这似乎不适合 xml 映射方案。在删除方法中,我目前正在遍历包含外键约束的任何集合并删除每个单独的对象,但我仍然没有删除的运气。