2014-02-13 61 views
0

我有一個類,它是用來記錄在我的系統中的所有其他類的變化:NHibernate的 - 映射一個表到許多家長

public class HistoryLogEntry 
{ 
    public virtual long Id { get; set; } 

    public virtual string EntityQualifiedId { get; set; } // <-- this contains both the entity name and entity id to properly identify object 
    public virtual long? IdEntity { get; set; } 
    public virtual DateTime? EntryDate { get; set; } 
    public virtual UserInfo CreatedBy { get; set; } 
    public virtual string LogData { get; set; } 
} 

我想有有HistoryLogEntry對象列表中的其他對象:

public class Person 
{ 
    [...] 

    public virtual IList<HistoryLogEntry> HistoryLogEntryList { get; set; } 
} 

public class Dog 
{ 
    [...] 

    public virtual IList<HistoryLogEntry> HistoryLogEntryList { get; set; } 
} 

請注意,沒有從HistoryLogEntry引用Person或Dog--沒關係,因爲我想在Person和Dog中使用HistoryLogEntry類。

如何映射這個關係,以便NHibernate的導出模式將創建所有必要的列?現在,我想用:

public class HistoryLogEntryMapping : ClassMapping<HistoryLogEntry> 
{ 
    public HistoryLogEntryMapping() 
    { 
     Lazy(true); 
     Table(MappingSettings.GetTableName("HistoryLogEntry", "SYS")); 
     Id(x => x.Id, map => map.Generator(new ISLib3.Storage.NH.MultipleHiLoPerTableGeneratorDef())); 
     Property(x => x.EntityQualifiedId, map => { map.Length(255); map.NotNullable(true); map.Index("HistoryLogEntry_EntityQualifiedId"); }); 
     Property(x => x.IdEntity, map => { map.NotNullable(false); });   

     Property(x => x.EntryDate); 
     ManyToOne(x => x.CreatedBy, map => 
     { 
      map.Cascade(Cascade.None); 
      map.Column("IdCreatedBy"); 
      map.Lazy(LazyRelation.Proxy); 
      map.NotNullable(false); 
      map.NotFound(NotFoundMode.Ignore); 
     }); 

     Property(x => x.LogData, map => { map.NotNullable(false); map.Type(NHibernateUtil.StringClob); }); 
    } 
} 

public class PersonMapping : ClassMapping<Person> 
{ 
    public PersonMapping() 
    { 
     Lazy(true); 
     BatchSize(10); 
     Table(MappingSettings.GetTableName("Person")); 
     Id(x => x.Id, map => map.Generator(new ISLib3.Storage.NH.MultipleHiLoPerTableGeneratorDef())); 

     Bag(x => x.HistoryLogEntryList, map => 
     { 
      // foreign key name MUST be set to "none" - this way ExportSchema will not generate keys in DB! 
      map.Key(k => { k.Column("IdEntity"); k.ForeignKey("none"); k.OnDelete(OnDeleteAction.NoAction); }); 
      map.Cascade(Cascade.None); 
      map.Loader("historyLogLoader"); // <-- note that I have special loader to load HistoryLogEntry based on column EntityQualifiedId 
      map.Lazy(CollectionLazy.Lazy); 
      map.Inverse(true); 
      map.BatchSize(10); 
     }, rm=>rm.OneToMany()); 

    } 
} 

不過,使用NHibernate 3.3和ExportSchemat時,它不會創造HistoryLogEntry標識列和我相信,我需要此列,這樣NHibernate的知道HistoryLogEntries把我的採集。

任何幫助將不勝感激。

回答

0

好吧,它看起來像nHibernate「隱藏功能」造成了所有的麻煩:顯然nHibernate不會創建一個名爲「IDENTITY」的列。一旦我將列名更改爲不同的名稱,一切都開始奏效。