2009-11-12 157 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