修改成员集项时,实体版本不会增加

时间:2015-09-24 18:49:32

标签: nhibernate nhibernate-mapping

我有以下NHibernate映射:

<class name="Person" proxy="Person" table="Person_">

<id name="OID" column="OID_" type="Guid">
  <generator class="guid.comb"/>
</id>

<version name="Version" column="Version_"/>

<set name="Properties" lazy="true" inverse="true" cascade="all-delete-orphan">
  <key column="PersonOID_"/>
  <one-to-many class="Property"/>
</set>

</class>

以下C#代码:

public class Property
{
    public Person Owner;
}

public class Person
{
    private Iesi.Collections.Generic.ISet<Property> _properties;

    public void AddProperty(Property property)
    {
        property.Owner = this;
        _properties.Add(property);
    }

    public void RemoveProperty(Property property)
    {
        property.Owner = null;
        _properties.Remove(property);
    }
}

...

public void TransferProperty(Property p, Person fromPerson, Person toPerson)
{
    //fromPerson.RemoveProperty(p);
    toPerson.AddProperty(p);
}

对TransferProperty的调用导致toPerson的实体版本增加,但是对于fromPerson的实体版本没有变化。

当多个用户同时调用TransferProperty时,这会导致我出现问题 - 因为fromPerson版本没有更改,当两个用户同时尝试从同一个人传输属性时,不会抛出PersistenceException

有关修改此代码的最佳方法的任何想法,以便调用TransferProperty将增加toPerson和fromPerson的实体版本 ???

提前致谢! 彼得

P.S。我在TransferProperty中注释掉了RemoveProperty调用,因为它导致&#34; ObjectDeletedException:删除的对象将通过级联重新保存(从关联中删除已删除的对象)&#34;。这样做会删除属性而不是传输它。

0 个答案:

没有答案