0

我有更新日期列映射的情况:

public abstract class AuditableEntityMapBase<T> : ClassMap<T>
{
    protected AuditableEntityMapBase()
    {
        ...
        OptimisticLock.Version();
        Version(x => (x as AuditableEntityBase).UpdateDT).Nullable();
        ...
    }
}

在 Parent(Person 实体)和 Child(PersonTelephone 实体与 Cascade.None for Person)继承的 AuditableEntityMapBase 基类上,映射如下:

public class PersonTelephoneMap : AuditableEntityMapBase<PersonTelephone>
{
    public PersonTelephoneMap()
    {
        Table("pers_PersonTelephone");

        Id(x => x.Id, "PersonTelephoneId");

        References(x => x.Person, "PersonId")
            .Cascade.None();
        ...
    }
}

public class PersonMap : AuditableEntityMapBase<Person>
{
    public PersonMap()
    {
        Table("pers_Person");

        Id(x => x.Id, "PersonId"); //.Unique().GeneratedBy.Native();

        ...

        HasMany(x => x.Phones)
            .KeyColumn("PersonId")
            .Inverse()
            .Cascade.All();

        ...
    }
}

在以下测试中保存子和刷新会话会导致父级上的“对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例”:

/// <summary>
/// Tests nHibernate for concurrency (dirty read)
/// 1. Telephone1 and Telephone2 entities are loaded in separate sessions
/// 2. Telephone1 is updated - Telephone2 now has a dirty read
/// 3. Update Telephone2 and expect NHibernate.StaleObjectStateException error
/// </summary>
[Test]
[ExpectedException("NHibernate.StaleObjectStateException")] //Assert
public void CanVersionConcurrencyPersonTelephone()
{
    //Arrange
    const string telNo1 = "911";
    const string telNo2 = "999";            
    Person person2 = null;
    PersonTelephone personTelephone2 = null;

    var person = CreatePerson(); //Create a new person entity            
    var personManager = new PersonManager();           

    //Act
    //var person1 = personManager.Read(person.Id);
    var personTelephone1 = person.Phones[0];
    SessionContext.Current.AttachEntity(personTelephone1);
    SessionContext.Flush();

    using (SessionContext.Open())
    {
        person2 = personManager.Read(person.Id);
        personTelephone2 = person2.Phones[0];
        SessionContext.Flush();
    }

    System.Threading.Thread.Sleep(2000); //Arrange for a dirty read by user delay 

    using (SessionContext.Open())
    {
        personTelephone1.Number = telNo1;
        personManager.UpdateTelephone(personTelephone1); //simulate dirty read for personTelephone2
        SessionContext.Flush();
    }

    using (SessionContext.Open())
    {
        personTelephone2.Number = telNo2;
        personManager.UpdateTelephone(personTelephone2); //expect NHibernate.StaleObjectStateException
        SessionContext.Flush();
    }
}

对我来说,在 PersonTelephone 映射上使用 Cascade.SaveUpdate 而不是 Cascade.SaveUpdate 对 Person 实体进行水合并进行 nHibernate 更新是不可行的,如下所示:

 References(x => x.Person, "PersonId")
                .Cascade.SaveUpdate();

我还尝试使用 ReadOnly 方法,该方法最初有效:

References(x => x.Person, "PersonId")
                    .Cascade.None.ReadOnly();

但是,这导致我插入 PersonTelephone 表时出现问题,因为 PersonId 是一个 Not Null 列,并且在 nHibernate 插入期间未注入,因为它是只读的。

悲观锁定不符合我的用户要求,并且 OptimisticLock.All() 会影响性能。我还尝试在 Person 实体映射上使用 .Cascade.None() 。

唯一有效的方法是在 Person 和 PersonTelephone 表上有一个唯一的 Update 字段。这个解决方案对我来说很臭。然后我尝试为 nHibernate 实体字段提供唯一名称,但没有奏效。有没有其他人遇到过这个?有没有优雅的解决方案?

4

1 回答 1

0

您已经撤销了 Person 记录的“所有权”;PersonTelephones“拥有”他们对 Person 的引用。但是,级联关系是自上而下的;PersonTelephones 在他们的 Person 存在时被保存,但不是相反。

所以,你得到的是你有一个新的 Person 和一个新的 PersonTelephone,并且你正在保存 PersonTelephone。数据库中尚不存在此人,并且告诉 NH 在保存 PersonTelephone 时不要保存此人,因此它唯一能做的就是抱怨。

要解决此问题,请保存 Person,而不是 PersonTelephone。这将插入或更新 Person,然后向下级联以插入或更新 PersonTelephones,包括您的新电话。

于 2011-05-12T17:25:36.250 回答