nhibernate - 将一张桌子映射到许多父母

时间:2014-02-13 20:59:37

标签: c# nhibernate relation mapping-by-code bag

我有一个用于记录系统中所有其他类的更改的类:

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中创建IdEntity列,我相信我需要这个列,以便NHibernate知道我的集合中放置了HistoryLogEntries。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

好吧,看起来nHibernate“隐藏功能”造成了所有麻烦:显然nHibernate不会创建一个名为“IDENTITY”的列。一旦我将列名更改为不同,一切都开始有效。