6

我有以下模型:

<class name="Person" table="Person" optimistic-lock="version">
  <id name="Id" type="Int32" unsaved-value="0">
    <generator class="native" />
  </id>
  <!-- plus some properties here -->
</class>

<class name="Event" table="Event" optimistic-lock="version">
  <id name="Id" type="Int32" unsaved-value="0">
    <generator class="native" />
  </id>
  <!-- plus some properties here -->
</class>

<class name="PersonEventRegistration" table="PersonEventRegistration" optimistic-lock="version">
  <id name="Id" type="Int32" unsaved-value="0">
    <generator class="native" />
  </id>
  <property name="IsComplete" type="Boolean" not-null="true" />
  <property name="RegistrationDate" type="DateTime" not-null="true" />
  <many-to-one name="Person" class="Person" column="PersonId" foreign-key="FK_PersonEvent_PersonId" cascade="all-delete-orphan" />
  <many-to-one name="Event" class="Event" column="EventId" foreign-key="FK_PersonEvent_EventId" cascade="all-delete-orphan" />
</class>

在 Person 和 Event 中都没有指向 PersonEventRegistration 的属性。

当我尝试从 PersonEventRegistration 中删除条目时,出现以下错误:

"deleted object would be re-saved by cascade"

问题是,我没有将此对象存储在任何其他集合中 - 删除代码如下所示:

public bool UnregisterFromEvent(Person person, Event entry)
{
    var registrationEntry = this.session
        .CreateCriteria<PersonEventRegistration>()
        .Add(Restrictions.Eq("Person", person))
        .Add(Restrictions.Eq("Event", entry))
        .Add(Restrictions.Eq("IsComplete", false))
        .UniqueResult<PersonEventRegistration>();

    bool result = false;
    if (null != registrationEntry)
    {
        using (ITransaction tx = this.session.BeginTransaction())
        {
            this.session.Delete(registrationEntry);
            tx.Commit();
            result = true;
        }
    }
    return result;
}

我在这里做错了什么?

4

2 回答 2

3

据我所知,cascade="all-delete-orphan"属于集合映射元素,而不是many-to-one. 您尚未显示映射的其他两个部分,因此我不能肯定地说,但这可能(可能)是问题所在。

我认为Person应该看起来像:

<!-- other properties -->
<set name="Events" inverse="true" cascade="all-delete-orphan">
    <key column="Person_id" />
    <one-to-many class="PersonEventRegistration" />
</set>

Event

<!-- similar mapping for Event -->

PersonEventRegistration

<!-- other properties -->
<many-to-one name="Person" class="Person" column="PersonId" foreign-key="FK_PersonEvent_PersonId" cascade="delete" <!-- or many ="all" ? --> />

实际上,以上可能是相互冲突的级联(这可能是您所拥有的)。所以说真的,我的回答是两件事:

  1. cascade="all-delete-orphan"上没有任何意义many-to-one
  2. 确保您已经真正考虑过如何与您的实体合作以及它们应该如何级联操作。
于 2009-06-01T14:05:05.090 回答
1

尝试在删除中取消引用 Person 和 Event:

    public bool UnregisterFromEvent(Person person, Event entry)
    {
        var registrationEntry = this.session
            .CreateCriteria<PersonEventRegistration>()
            .Add(Restrictions.Eq("Person", person))
            .Add(Restrictions.Eq("Event", entry))
            .Add(Restrictions.Eq("IsComplete", false))
            .UniqueResult<PersonEventRegistration>();

        bool result = false;
        if (null != registrationEntry)
        {
            using (ITransaction tx = this.session.BeginTransaction())
            {
                registrationEntry.Person = null;
                registrationEntry.Event = null;
                this.session.Delete(registrationEntry);
                tx.Commit();
                result = true;
            }
        }
        return result;
    }

另外,我不知道您可以对对象添加限制,我会使用别名和 ID 来编写它。

            .Add(Restrictions.Eq("Person", person))
            .Add(Restrictions.Eq("Event", entry))
于 2010-02-21T14:19:09.413 回答