假设我有课程:
class Person
{
String FirstName;
String LastName;
Pet Pet;
}
class Pet
{
String Name;
Person Owner;
}
class Cat : Pet
{
Int32 MousesCaught;
}
class Dog : Pet
{
Int32 CatsCaught;
}
和映射:
public class PetMap : IAutoMappingOverride<Pet>
{
public void Override(AutoMapping<Pet> mapping)
{
mapping.HasOne(x => x.Owner).PropertyRef(x => x.Pet).Constrained().Cascade.All();
mapping.JoinedSubClass<Cat>("PetId");
mapping.JoinedSubClass<Dog>("PetId");
}
}
问题是:如果DB中有Cat或Dog的Person,Person.Pet的类型是'Pet {PetBroxyBlaBlaBla}'。所以我不能将Person.Pet转换为类型Cat(Person.Pet为Cat == null)。
var person = this.Session.Get<Person>(personId);
// person.Pet as Cat == null
但是如果我在获得Person之前从DB获得此宠物,则该类型有效:
var a = new this.Session.Get<Pet>(petId);
var person = this.Session.Get<Person>(personId);
// person.Pet as Cat != null
是否有办法告诉NHibernate使用有效类型初始化此属性?