Search code examples
c#nhibernateormid-generation

In NHibernate ID Generator section in mapping files, What is the meaning of assigned and select?


In NHibernate ID Generator section in mapping files, What is the meaning of assigned and select?


Solution

  • There's identity/sequence and assigned. From the NH documentation:

    5.1.5.1. generator

    For databases which support identity columns (DB2, MySQL, Sybase, MS SQL), you may use identity key generation. For databases that support sequences (DB2, Oracle, PostgreSQL, Interbase, McKoi, SAP DB) you may use sequence style key generation. Both these strategies require two SQL queries to insert a new object.

    uid_sequence For cross-platform development, the native strategy will choose from the identity, sequence and hilo strategies, dependent upon the capabilities of the underlying database.

    5.1.5.7. Assigned Identifiers

    If you want the application to assign identifiers (as opposed to having NHibernate generate them), you may use the assigned generator. This special generator will use the identifier value already assigned to the object's identifier property. Be very careful when using this feature to assign keys with business meaning (almost always a terrible design decision).

    Due to its inherent nature, entities that use this generator cannot be saved via the ISession's SaveOrUpdate() method. Instead you have to explicitly specify to NHibernate if the object should be saved or updated by calling either the Save() or Update() method of the ISession.