您的位置:首页 > 其它

Hibernate学习笔记-ID生成策略

2016-08-29 16:59 423 查看
1. xml生成id

a) generator

<id name="id" >

<generator class="native"></generator>

</id>

b) 常用四个:

(*)native:selects 
identity
sequence
 or 
hilo
 depending
upon the capabilities of the underlying database.(用native就可以让hibernate自动选择生成方式)

 identity :对DB2,MySQL,
MS SQL Server, Sybase和HypersonicSQL的内置标识字段提供支持。 返回的标识符是
long
,
short
 或者
int
类型的。

sequence :在DB2,PostgreSQL,
Oracle, SAP DB, McKoi中使用序列(sequence), 而在Interbase中使用生成器(generator)。返回的标识符是
long
short
或者 
int
类型的。

uuid:uses a 128-bit
UUID algorithm to generate identifiers of type string that are unique within a network (the IP address is used). The UUID is encoded as a string of 32 hexadecimal digits in length.(独一无二的编码)

2.注解方式:@GeneratedValue

a) AUTO(直接写
@GeneratedValue 相当如native)

 (@GeneratedValue(strategy=GenerationType.AUTO))

i. 默认:对 MySQL,使用auto_increment

ii. 对 Oracle使用hibernate_sequence(名称固定)

b) IDENTITY(@GeneratedValue(strategy=GenerationType.IDENTITY))

c) SEQUENCE(@GeneratedValue(strategy=GenerationType.SEQUENCE))

例:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: