2009-11-12 158 views
2

我使用Fluent NHibernate和每个子类的继承映射。 我想引用特定对象的列表,但我无法弄清楚,如何将结果复原到某个特定类的对象。 流利的NHibernate继承映射问题

class PetMap : ClassMap<Pet> 
{ 
    public PetMap() 
    { 
     Id(c => c.ID).GeneratedBy.Identity();    
    } 
} 

class DogMap : ClassMap<Dog> 
{ 
    public DogMap() 
    { 
     Mac(c => c.DogSpecificProperty);         
    } 
} 

class CatMap : SubclassMap<Cat> 
{ 
    public CatMap() 
    { 
     Mac(c => c.CatSpecificProperty);          
    } 
} 

class PersonMap : ClassMap<Person> 
{ 
    public PersonMap() 
    { 
     Id(c => c.ID).GeneratedBy.Identity(); 

     //this works fine 
     HasMany(c => c.Pets); 

     //this dosen't work, because the result contains dogs and cats 
     //how can I tell NHibernate to only fetch dogs or cats? 
     HasMany<Pet>(c => c.Cats); 
     HasMany<Pet>(c => c.Dogs); 
    } 
} 

class Pet 
{ 
    int ID; 
} 
class Dog : Pet 
{ 
    object DogSpecificProperty; 
} 
class Cat : Pet 
{ 
    object CatSpecificProperty; 
} 
class Person 
{ 
    int ID; 
    IList<Pet> Pets; 
    IList<Dog> Dogs; 
    IList<Cat> Cats; 
} 

任何人都可以帮助我吗?请原谅我可怜的英语。

回答

1

我不流利的NH的专家,但在我看来,你的个人地图应该是这样的:

class PersonMap : ClassMap<Person> 
{ 
    public PersonMap() 
    { 
     Id(c => c.ID).GeneratedBy.Identity(); 

     //this works fine 
     HasMany(c => c.Pets); 

     //this dosen't work, because the result contains dogs and cats 
     //how can I tell NHibernate to only fetch dogs or cats? 
     HasMany<Cat>(c => c.Cats); 
     HasMany<Dog>(c => c.Dogs); 
    } 
} 

因为你是人有狗属性,它是一个IList不IList的,而对于猫来说则相反。

+0

HasMany(c => c.Cats) 这看起来有点痘痘愚蠢,但我认为这是正确的,因为宠物表有Person表的参考。我从其他堆栈溢出问题中得到这个解决方案。 在我看来,HasMany (c => c.Cat)与HasMany (c => c.Cat).KeyColumn(“PetID”)的做法相同。在这两种情况下,我都得到了猫和狗的结果。 – trichterwolke 2009-11-12 18:23:03

+0

@trichterwolke如果你说猫和狗类实际上来自数据库中的宠物表,那么我认为你会说在CatMap和DogMap中,而不是在PersonMap中。我相信这就是我以前在使用FNH时所做的。 – Joseph 2009-11-12 18:29:43