0

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

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 放入我的集合中。

任何帮助将不胜感激。

4

1 回答 1

0

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

于 2014-02-14T08:42:27.517 回答