2010-01-19 55 views
2

我一直在閱讀Nhibernate in Action,但映射多態集合的章節對於如何執行此操作有點太短。如何在nhibernate中映射抽象類的集合

我有以下代碼

[Class] 
[Discriminator(Column="MachineType",TypeType=typeof(string))] 
public abstract class Machine 
{ 
    [Property] 
    public string Name{get;set;} 
} 

[Subclass(DiscriminatorValue="Heavy",ExtendsType=typeof(Machine))] 
public class HeavyMachine : Machine 
{ 
    [Property] 
    public int Weight { get; set; } 
} 

[Subclass(DiscriminatorValue="Fast",ExtendsType=typeof(Machine))] 
public class FastMachine : Machine 
{ 
    [Property] 
    public float Speed { get; set; } 
} 

[Class] 
public class Module 
{ 
    List<Machine> machines = new List<Machine>(); 

    [Bag(Name = "Machines", Cascade = "all", Lazy = false, Inverse=true)] 
    [Key(1, Column = "Machine")] 
    [OneToMany(2, ClassType = typeof(Machine))] 
    public IList<Machine> Machines 
    { 
     get 
     { 
     return machines.AsReadOnly(); 
     } 
     private set 
     { 
     machines = value.ToList(); 
    } 
    } 
} 

通過上面我沒有得到任何錯誤的代碼,但機器的模塊集合從數據庫retreiving我的對象後,仍然爲空。機器(及其子類)的映射確實沒問題,因爲Machine類型的屬性被正確地返回。

什麼Nhibernate.Mapping.Attributes我需要映射我的抽象類的集合?

thx提前!

回答

1

好吧,我找到了解決方案。 從我的IList映射中移除「Inverse = true」標籤後,它就起作用了。