Hibernate-Identifier generator
来源:互联网 发布:csv导入mysql 编辑:程序博客网 时间:2024/05/17 02:14
5.1.2.2. Identifier generator
Hibernate offers various generation strategies, let's explore the most common ones first that happens to be standardized by JPA:
IDENTITY: supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type
long
,short
orint
.SEQUENCE (called
seqhilo
in Hibernate): uses a hi/lo algorithm to efficiently generate identifiers of typelong
,short
orint
, given a named database sequence.TABLE (called
MultipleHiLoPerTableGenerator
in Hibernate) : uses a hi/lo algorithm to efficiently generate identifiers of typelong
,short
orint
, given a table and column as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database.- AUTO: selects
IDENTITY
,SEQUENCE
orTABLE
depending upon the capabilities of the underlying database.
Important
We recommend all new projects to use the new enhanced identifier generators.
They are deactivated by default for entities using annotations but can be activated using hibernate.id.new_generator_mappings=true
.
These new generators are more efficient and closer to the JPA 2 specification semantic.
However they are not backward compatible with existing Hibernate based application (if a sequence or a table is used for id generation).
@Entitypublic class Customer { @Id @GeneratedValue Integer getId() { ... };}@Entity public class Invoice { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) Integer getId() { ... };}
These are the four standard JPA generators. Hibernate goes beyond that and provide additional generators or additional options as we will see below. You can also write your own custom identifier generator by implementing org.hibernate.id.IdentifierGenerator
.
The hbm.xml approach uses the optional <generator>
child element inside <id>
. If any parameters are required to configure or initialize the generator instance, they are passed using the <param>
element.
<id name="id" type="long" column="cat_id"> <generator class="org.hibernate.id.TableHiLoGenerator"> <param name="table">uid_table</param> <param name="column">next_hi_value_column</param> </generator></id>
5.1.2.2.1. Various additional generators
All generators implement the interface org.hibernate.id.IdentifierGenerator
. This is a very simple interface. Some applications can choose to provide their own specialized implementations, however, Hibernate provides a range of built-in implementations. The shortcut names for the built-in generators are as follows:
increment
generates identifiers of type
long
,short
orint
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, Sybase and HypersonicSQL. The returned identifier is of type
long
,short
orint
.sequence
uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type
long
,short
orint
hilo
uses a hi/lo algorithm to efficiently generate identifiers of type
long
,short
orint
, given a table and column (by defaulthibernate_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.seqhilo
uses a hi/lo algorithm to efficiently generate identifiers of type
long
,short
orint
, given a named database sequence.uuid
Generates a 128-bit UUID based on a custom algorithm. The value generated is represented as a string of 32 hexidecimal digits. Users can also configure it to use a separator (config parameter "separator") which separates the hexidecimal digits into 8{sep}8{sep}4{sep}8{sep}4. Note specifically that this is different than the IETF RFC 4122 representation of 8-4-4-4-12. If you need RFC 4122 compliant UUIDs, consider using "uuid2" generator discussed below.
uuid2
Generates a IETF RFC 4122 compliant (variant 2) 128-bit UUID. The exact "version" (the RFC term) generated depends on the pluggable "generation strategy" used (see below). Capable of generating values as
java.util.UUID
,java.lang.String
or as a byte array of length 16 (byte[16]
). The "generation strategy" is defined by the interfaceorg.hibernate.id.UUIDGenerationStrategy
. The generator defines 2 configuration parameters for defining which generation strategy to use:uuid_gen_strategy_class
Names the UUIDGenerationStrategy class to use
uuid_gen_strategy
Names the UUIDGenerationStrategy instance to use
Out of the box, comes with the following strategies:
org.hibernate.id.uuid.StandardRandomStrategy
(the default) - generates "version 3" (aka, "random") UUID values via therandomUUID
method ofjava.util.UUID
org.hibernate.id.uuid.CustomVersionOneStrategy
- generates "version 1" UUID values, using IP address since mac address not available. If you need mac address to be used, consider leveraging one of the existing third party UUID generators which sniff out mac address and integrating it via theorg.hibernate.id.UUIDGenerationStrategy
contract. Two such libraries known at time of this writing include http://johannburkard.de/software/uuid/ andhttp://commons.apache.org/sandbox/id/uuid.html
guid
uses a database-generated GUID string on MS SQL Server and MySQL.
native
selects
identity
,sequence
orhilo
depending upon the capabilities of the underlying database.assigned
lets the application assign an identifier to the object before
save()
is called. This is the default strategy if no<generator>
element is specified.select
retrieves a primary key, assigned by a database trigger, by selecting the row by some unique key and retrieving the primary key value.
foreign
uses the identifier of another associated object. It is usually used in conjunction with a
<one-to-one>
primary key association.sequence-identity
a specialized sequence generation strategy that utilizes a database sequence for the actual value generation, but combines this with JDBC3 getGeneratedKeys to return the generated identifier value as part of the insert statement execution. This strategy is only supported on Oracle 10g drivers targeted for JDK 1.4. Comments on these insert statements are disabled due to a bug in the Oracle drivers.
5.1.2.2.2. Hi/lo algorithm
....- Hibernate Identifier generator
- Hibernate-Identifier generator
- hibernate---generator
- Hibernate generator
- Hibernate --- generator的用法!
- Hibernate 中的 Generator 说明
- hibernate id Generator
- hibernate id Generator
- hibernate id Generator
- Hibernate的Generator属性
- Hibernate的generator属性
- Hibernate Id Generator
- Hibernate的generator属性
- Hibernate generator element
- Hibernate主键生成器generator
- Hibernate <generator>元素
- Hibernate的generator属性
- Hibernate的generator属性
- 孙鑫VC++深入详解:Lesson6 Part3 ---创建右键弹出菜单 TrackPopupMenu
- hibernate在myeclipse中的配置过程详解
- 阿森纳
- 开启windows 7虚拟WiFi
- Android图片资源转换代码
- Hibernate-Identifier generator
- android的颜色对应的XML配置值,颜色表
- subclipse显示英文的方法
- AS3适用函数、AS3经常使用算法年夜齐
- Javascript 正则表达式之全局匹配
- Word2010取消和开启首字母自动大写
- JAVA创建快捷方式
- 利用GoAhead构建嵌入式web应用
- Oracle “CONNECT BY” 使用