0

我有闲散的实体:

个人公司

然后,有一个实体 PersonCompany 在 Person 和 Company 之间建立关系

人的映射是:

public class PersonMap : SubclassMap<Person> {
    public PersonMap() {
        this.Table("Person");
        this.KeyColumn("Id");
        this.HasMany(x => x.PersonCompanys).AsSet().KeyColumn("PersonId").Fetch.Select().Inverse().Cascade.Delete();
    }
}

公司:

public class CompanyMap : SubclassMap<Company> {
    public CompanyMap() {
        this.Table("Company");
        this.KeyColumn("Id");
        this.HasMany(x => x.PersonCompanys).AsSet().KeyColumn("CompanyId").Fetch.Select().Inverse().Cascade.None();
    }
}

和个人公司:

public class PersonCompanyMap : ClassMap<PersonCompany> {
    public PersonCompanyMap() {
        this.Table("PersonCompany");

        this.Version(x => x.ObjectVersion);
        this.Id(x => x.Id).GeneratedBy.Assigned();
        this.References(x => x.Company).Column("CompanyId").Fetch.Select();
        this.References(x => x.Person).Column("PersonId").Fetch.Select();
    }
}

SecondLevel-Caching 通常被激活,并且对于每个具有约定的实体。还有 PersonCompanys-Collection 的缓存:

public class CachableConventions : IClassConvention, IHasManyConvention, IHasManyToManyConvention {
    private static readonly Type[] notCachableTypes = new[] {typeof(Word), typeof(ActivityWord), typeof(DbVersion), typeof(Deleted), typeof(ExchangeSyncState), typeof(Synchronizer), typeof(SerialletterField)};

    public void Apply(IClassInstance instance) {
        if (Array.IndexOf(notCachableTypes, instance.EntityType) >= 0) {
            return;
        }
        instance.Cache.ReadWrite();
    }

    public void Apply(IManyToManyCollectionInstance instance) {
        if (Array.IndexOf(notCachableTypes, instance.ChildType) >= 0) {
            return;
        }
        instance.Cache.ReadWrite();
    }
    public void Apply(IOneToManyCollectionInstance instance) {
        if (Array.IndexOf(notCachableTypes, instance.ChildType) >= 0) {
            return;
        }
        instance.Cache.ReadWrite();
    }
}

现在,我创建了一个新的 Person 并保存了一个新的 Company,然后我将 PersonCompany-Record 添加到 PersonCompanies-Collection。

当我选择创建的 Person 后,PersonCompanys-Collection 将为空。当我在没有 SecondLevel-Caching 的情况下执行相同操作时,PersonCompanys-Collection 包含创建的实体。

有人知道,这里会出什么问题吗?

编辑 - 附加信息:

此步骤导致问题:

  1. 创建一个新的人Session.Save(person)
  2. 加载这个人person = Session.Get(person.Id)
  3. 从 Person with 获取 personCompany personCompany = person.PersonCompany.FirstOrDefault()(这将为空)
  4. 创建一个新的 PersonCompany-EntrySession.Save(new PersonCompany(Guid.NewGuid()) {Person = person}))
  5. 加载这个人person = Session.Get(person.Id)
  6. 现在,person.PersonCompany返回一个空列表。

当我不执行第 3 步时,一切正常。

4

1 回答 1

0
  1. 见上
  2. 见上
  3. 通过 personCompany = person.PersonCompany.FirstOrDefault() 从 Person 获取 personCompany(这将为空)

    这会将集合初始化为空集合

  4. 使用 Session.Save(new PersonCompany(Guid.NewGuid()) {Person = person})) 创建一个新的 PersonCompany-Entry

    将在数据库中创建 PersonCompany 记录

  5. 用 person = Session.Get(person.Id) 加载人

    只会返回缓存的人对象,所以这基本上是“什么都不做”

  6. 现在, person.PersonCompany 返回一个空列表

由于该集合在步骤 3 中被初始化为空集合,因此它将仅返回该集合。nhibernate 不会发出另一个选择来确定是否发生了变化。如果会,它可能会丢弃本地更改或产生冲突,并且效率不高

  • session.Clear();作为步骤 3b
  • session.Refresh(person);作为第 5 步

然后它按预期工作

于 2012-09-14T11:06:00.270 回答