How to auto generate IDs in NHibernate

混江龙づ霸主 提交于 2021-02-07 03:33:05

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/34219735/how-to-auto-generate-ids-in-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!