If you want to be able to use both assigned and native id generation try something like this (although it's hard-coded to use type long for ids, and 0 as the unsaved-value, so you might want to mess around with it):
Code:
public class IdGenerator : NHibernate.Id.IdentityGenerator, NHibernate.Id.IIdentifierGenerator
{
public new object Generate(NHibernate.Engine.ISessionImplementor session, object obj)
{
IEntityPersister persister = session.GetEntityPersister(obj);
// Determine if an ID has been assigned.
object id = persister.GetIdentifier(obj);
if (id != null && (long)id != 0) return id;
return IdentifierGeneratorFactory.IdentityColumnIndicator;
}
}
In your mapping file:
Code:
<class name="TestClass" table="test_class">
<id name="Id" column="id" type="long">
<generator class="Namespace.IdGenerator, AssemblyName" />
</id>
...