views:

292

answers:

1

I need to generate an identity from a column in a seed table and increment the value in that column.

e.g

Seed Table:

name     "analysisid"
id       1


Analysis Table:

id
name
description

On the analysis mapping I need to ensure that the id is taken from the seed table and on inserting an analysis the analysisid of the seedid is updated.Do I implement a custom class inheriting from TableGenerator?

+2  A: 

NHibernate includes a number of identity generation strategies, and includes an extension mechanism for custom ones via NHibernate.Id.IIdentifierGenerator.

You could do this by implementing an insert trigger which fetches and assigns the next identifier for entity Analysis. In this case use <generator class="trigger-identity" />.

Another approach is to build your own identifier strategy by implementing IIdentifierGenerator and mapping as <generator class="My.Namespace.MyIdentifierGenerator" />.

In fluent, those generators are mapped by:

Id( x => x.Id ).GeneratedBy.Custom<NHibernate.Id.TriggerIdentityGenerator>();
Id( x => x.Id ).GeneratedBy.Custom<My.Namespace.MyIdentifierGenerator>();
Lachlan Roche