0

I have one class that is used to log changes in all other classes in my system:

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; }
}

I want to have other objects that have list of HistoryLogEntry objects:

public class Person
{
    [...]

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

public class Dog
{
    [...]

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

Note that there's no reference from HistoryLogEntry to either Person or Dog - and that's ok, because I want to use HistoryLogEntry class in both Person and Dog.

How can I map this relation, so that NHibernate's export schema will create all necessary columns? Right now I'm trying with:

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() );

    }
}

However, when using NHibernate 3.3 and ExportSchemat, it doesn't create IdEntity column in HistoryLogEntry and I belive that I need this column so that NHibernate will know HistoryLogEntries put in my collection.

Any help will be appreciated.

1 Answers1

0

OK, it looks like nHibernate "hidden feature" was causing all that trouble: apparently nHibernate won't create a column named "IDENTITY". Once I changed column name to different, everything started to work.