如何在 NHibernate 中自动生成 ID [英] How to auto generate IDs in NHibernate

查看:61
本文介绍了如何在 NHibernate 中自动生成 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 NHibernate 自动生成表的唯一 ID?ID 可以是任何 long 值,只要每个值只使用一次即可.

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>

这会创建从 1 开始递增的 ID,但它会在每次应用程序启动时重置为 1.所以每次重新启动后,存储的第一个元素获取 Id 1 并删除前一个 Id 1 元素(不是我想要的).

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).

这是因为我使用了 SchemaExport 而不是 SchemaUpdate,所以每次应用程序启动时我的整个数据库都被删除了.

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

谢谢!

推荐答案

有关于它的明确文档部分.我建议使用 HI-LO(参见 什么是 Hi/Lo 算法?)

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

增量

生成任何整数类型的标识符,这些标识符仅在没有其他进程将数据插入同一个表时才是唯一的.不要在集群中使用.

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.

身份

支持 DB2、MySQL、MS SQL Server 和 Sybase 中的标识列.使用 Convert.ChangeType 将数据库返回的标识符转换为属性类型.因此支持任何整数属性类型.

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.

顺序

使用 DB2、PostgreSQL、Oracle 中的序列或 Firebird 中的生成器.使用 Convert.ChangeType 将数据库返回的标识符转换为属性类型.因此支持任何整数属性类型.

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

使用 hi/lo 算法有效地生成任何整数类型的标识符,给定一个表和列(默认分别为 hibernate_unique_key 和 next_hi)作为 hi 值的来源.hi/lo 算法生成仅对特定数据库唯一的标识符.请勿通过用户提供的连接使用此生成器.

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.

您可以使用where"参数来指定要在表中使用的行.如果您想为标识符使用单个表格,每个表格具有不同的行,这将非常有用.

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

使用 hi/lo 算法有效地生成任何整数类型的标识符,给定一个命名的数据库序列.

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

uuid.hex

使用 System.Guid 及其 ToString(string format) 方法生成字符串类型的标识符.返回的字符串长度取决于配置的格式.

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

使用新的 System.Guid 创建一个被转换为字符串的 byte[].指导

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

使用新的 System.Guid 作为标识符.

uses a new System.Guid as the identifier.

guid.comb

使用该算法生成由 Jimmy Nilsson 在文章 http://www.informit.com/articles/article.asp?p=25862.

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.

原生

根据底层数据库的功能选择身份、序列或 hilo.

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

已分配

让应用程序在调用 Save() 之前为对象分配一个标识符.

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

国外

使用另一个关联对象的标识符.通常与 主键关联一起使用.

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

这篇关于如何在 NHibernate 中自动生成 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆