我有闲散的实体:
个人公司
然后,有一个实体 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 包含创建的实体。
有人知道,这里会出什么问题吗?
编辑 - 附加信息:
此步骤导致问题:
- 创建一个新的人
Session.Save(person)
- 加载这个人
person = Session.Get(person.Id)
- 从 Person with 获取 personCompany
personCompany = person.PersonCompany.FirstOrDefault()
(这将为空) - 创建一个新的 PersonCompany-Entry
Session.Save(new PersonCompany(Guid.NewGuid()) {Person = person}))
- 加载这个人
person = Session.Get(person.Id)
- 现在,
person.PersonCompany
返回一个空列表。
当我不执行第 3 步时,一切正常。