How to auto generate IDs in NHibernate
Asked Answered
D

1

5

How can I make NHibernate autogenerate unique IDs for a table? The IDs can be any long values, as long as each one is only used once.

My current mapping looks like this:

<id name="Id">
    <generator class="increment"/>
</id>

This creates increasing IDs starting at 1, but it resets to 1 at each application startup. So the after each restart, the first element that is stored gets the Id 1 and the previous Id 1 element is deleted (not what I want).

Edit: The reason for this is that I used SchemaExport instead of SchemaUpdate, so my entire database was deleted at each application startup.

Thank you!

Directorate answered 11/12, 2015 at 9:9 Comment(0)
S
7

There is clear documentation section about it. I would suggest to use HI-LO (see What's the Hi/Lo algorithm?)

5.1.5.1. generator

increment

generates identifiers of any integral type that are unique only when no other process is inserting data into the same table. Do not use in a cluster.

identity

supports identity columns in DB2, MySQL, MS SQL Server and Sybase. The identifier returned by the database is converted to the property type using Convert.ChangeType. Any integral property type is thus supported.

sequence

uses a sequence in DB2, PostgreSQL, Oracle or a generator in Firebird. The identifier returned by the database is converted to the property type using Convert.ChangeType. Any integral property type is thus supported.

hilo

uses a hi/lo algorithm to efficiently generate identifiers of any integral type, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. Do not use this generator with a user-supplied connection.

You can use the "where" parameter to specify the row to use in a table. This is useful if you want to use a single tabel for your identifiers, with different rows for each table.

seqhilo

uses a hi/lo algorithm to efficiently generate identifiers of any integral type, given a named database sequence.

uuid.hex

uses System.Guid and its ToString(string format) method to generate identifiers of type string. The length of the string returned depends on the configured format.

uuid.string

uses a new System.Guid to create a byte[] that is converted to a string. guid

uses a new System.Guid as the identifier.

guid.comb

uses the algorithm to generate a new System.Guid described by Jimmy Nilsson in the article http://www.informit.com/articles/article.asp?p=25862.

native

picks identity, sequence or hilo depending upon the capabilities of the underlying database.

assigned

lets the application to assign an identifier to the object before Save() is called.

foreign

uses the identifier of another associated object. Usually used in conjunction with a <one-to-one> primary key association.

Safranine answered 11/12, 2015 at 9:13 Comment(4)
Is the increment generator not supposed to generate unique IDs? I don't like the hilo generator because it needs two additional columns as I understand it.Directorate
Honestly, my experience is, that increment is working properly. It does not start from 1 on app restart. It gets the last max value and continues incrementing it. So, maybe you do not experience that kind of problem, but something else. BUT. Increment is good only in single thread world (very unique cases) HI-LO is safe all the time, but you must be ready to accept not linear IDs... some numbers will be skipped. Hope this helps a bit...Crabb
I was using SchemaExportinstead of SchemaUpdate, that's why I lost my database on application startup. I'm now using Hilo as you suggested and it works fine!Directorate
Great to see that, really. Enjoy NHibernate ;)Crabb

© 2022 - 2024 — McMap. All rights reserved.