Hibernate读书笔记--@ID的配置
原原英文:
incrementgenerates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster.
<em />identitysupports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
sequenceuses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int
hilo<a style="color: #003399;" name="mapping-declaration-id-hilodescription" />uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key andnext_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database.seqhilouses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.
uuiduses a 128-bit UUID algorithm to generate identifiers of type string, unique within a network (the IP address is used). The UUID is encoded as a string of hexadecimal digits of length 32.guiduses a database-generated GUID string on MS SQL Server and MySQL.nativepicks identity, sequence or hilo depending upon the capabilities of the underlying database.assignedlets the application to assign an identifier to the object before save() is called. This is the default strategy if no <generator> element is specified.selectretrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value.foreignuses the identifier of another associated object. Usually used in conjunction with a <one-to-one> primary key association.sequence-identitya specialized sequence generation strategy which utilizes a database sequence for the actual value generation, but combines this with JDBC3 getGeneratedKeys to actually return the generated identifier value as part of the insert statement execution. This strategy is only known to be supported on Oracle 10g drivers targetted for JDK 1.4. Note comments on these insert statements are disabled due to a bug in the Oracle drivers.中文理解
Ø “assigned” :
主键由外部程序负责生成,在 save() 之前指定一个。
Ø “hilo”
通过hi/lo 算法实现的主键生成机制,需要额外的数据库表或字段提供高位值来源。
Ø “seqhilo”
与hilo 类似,通过hi/lo 算法实现的主键生成机制,需要数据库中的 Sequence,适用于支持 Sequence 的数据库,如Oracle。
Ø “increment”
主键按数值顺序递增。此方式的实现机制为在当前应用实例中维持一个变量,以保存着当前的最大值,之后每次需要生成主键的时候将此值加1作为主键。这种方式可能产生的问题是:不能在集群下使用。
Ø “identity”
采用数据库提供的主键生成机制。如DB2、SQL Server、MySQL 中的主键生成机制。
Ø “sequence”
采用数据库提供的 sequence 机制生成主键。如 Oralce 中的Sequence。
Ø “native”
由 Hibernate 根据使用的数据库自行判断采用 identity、hilo、sequence 其中一种作为主键生成方式。
Ø “uuid.hex”
由 Hibernate 基于128 位 UUID 算法 生成16 进制数值(编码后以长度32 的字符串表示)作为主键。
Ø “uuid.string”
与uuid.hex 类似,只是生成的主键未进行编码(长度16),不能应用在 PostgreSQL 数据库中。
Ø “foreign”
使用另外一个相关联的对象的标识符作为主键。
常用example
<!----><!----><!---->
1.Hi/lo
Hi/lo有hilo 和seqhilo2种可选择的方法,hilo需要一个特殊的数据表来保存hi值。而seqhilo2使用的是类似与Oracle-style sequence(需要数据库支持)
<div style="border: 1pt solid #cccccc; padding: 4pt;"><idname="id" type="long" column="cat_id">
<generatorclass="hilo">
<paramname="table">hi_value</param>
<param name="column">next_value</param>
<paramname="max_lo">100</param>
</generator>
</id>
<idname="id" type="long" column="cat_id">
<generatorclass="seqhilo">
<paramname="sequence">hi_value</param>
<paramname="max_lo">100</param>
</generator>
</id>
页:
[1]