【问题标题】:Yet another one on "deleted object would be re-saved by cascade (remove deleted object from associations)"还有一个关于“已删除的对象将通过级联重新保存(从关联中删除已删除的对象)”
【发布时间】:2013-07-15 19:41:10
【问题描述】:

大多数对这个 NHibernate 错误的引用(在标题中)都提到子对象被删除,同时仍包含在父集合中,这只会在以后重新保存它们,否定它们的“删除”(这就是错误状态以及从重新保存对象的关联中删除对象的建议)。

但在我的情况下,删除父记录时发生错误(然后通过关联删除“子”,因此我不希望删除关联,如错误所示)

如果每次都发生这种情况会很有意义,但我只观察到此错误间歇性,运行完全相同的代码,但仅针对特定记录。其他相同的记录不会触发条件并且删除会通过(我一直处理一个Customer 和两个address-es。)

不确定要显示多少和显示什么代码。这是我的“父”对象映射:

我有一个 OneCustomer-to-ManyAddresses 关系映射为一组 CustomerAddress “复合元素”

<class name="Customer" table="Customer">
        <id name="Id" column="[Id]" type="Guid">
            <generator class="guid" />
        </id>
        ...
        <set name="AddressList" lazy="true" table="CustomerAddress" cascade="all-delete-orphan" >
            <key column="[CustomerId]"/>
            <composite-element class="CustomerAddress">
                <parent name="Customer"/>
                <many-to-one name="Address"
                class="Address"
                column="[AddressId]"
                not-null="true"
                cascade="all"/>
                ...
            </composite-element>
        </set>

这是“复合”对象CustomerAddress的映射:

<class name="CustomerAddress" table="CustomerAddress" >
        <id name="Id" column="[Id]" type="Guid">
            <generator class="guid" />
        </id>
        <many-to-one
            name="Address"
            column="[AddressId]"
            class="Address"
            not-null="true"
            cascade="all"/>

        <many-to-one
            name="Customer"
            column="[CustomerId]"
            class="Customer"
            not-null="true"
            cascade="all"/>
            ...
    </class>

错误的解释及其间歇性的性质是什么?

附加信息

经过进一步检查 - 寻找可能包含对同一“地址”子项的引用的另一个集合,我看到了以下映射:

<class name="Address" table="Address">
        <id name="Id" column="[id]" type="Guid" >
            <generator class="guid" />
        </id>

        <!-- REMOVING THE FOLLOWING <BAG> SEEMS TO BE FIXING MY ISSUE -->
        <bag name="CustomerAddressList" inverse="true" cascade="none" lazy="false" >
            <key>
                <column name="[AddressId]" />
            </key>
            <one-to-many class="CustomerAddress" />
        </bag>

    </class>

删除&lt;bag name="CustomerAddressList"... 似乎解决了我的问题。解释?

【问题讨论】:

    标签: nhibernate


    【解决方案1】:

    我来猜一猜:

    您有一个双向关联,但您没有指定其中之一是非所有者(通过设置“inverse = true”)。

    尝试如下修改 CustomerAddress 关系:

    <class name="CustomerAddress" table="CustomerAddress" >
        <id name="Id" column="[Id]" type="Guid">
            <generator class="guid" />
        </id>
        <many-to-one
            name="Address"
            column="[AddressId]"
            class="Address"
            not-null="true"
            cascade="all"/>
    
        <many-to-one
            name="Customer"
            column="[CustomerId]"
            class="Customer"
            not-null="true"
            cascade="all"
            inverse="true"
             />
            ...
    </class>
    

    【讨论】:

    • 谢谢。这听起来很有道理。其他东西似乎已经解决了我的问题 - 按照你的答案,但不同 - 请查看我对原始问题的编辑。
    【解决方案2】:

    为什么 CustomerAddress 表有两个映射(一个作为复合元素,一个作为实体)?删除客户地址的实体映射,它应该可以工作。

    【讨论】:

    • 因为我有时会自己使用 CustomerAddress 类 - 我认为我需要独立映射。感谢您查看这个。我试试
    猜你喜欢
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 2013-10-03
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多