1.8 id的生成策略(单一主键)

1.ID的生成策略设置方式

<id
        name="propertyName"                                          (1)
        type="typename"                                              (2)
        column="column_name"                                         (3)
        unsaved-value="null|any|none|undefined|id_value"             (4)
        access="field|property|ClassName">                           (5)
        node="element-name|@attribute-name|element/@attribute|."

        <generator class="generatorClass"/>
</id>
1

name (optional): the name of the identifier property.

2

type (可选): 一个Hibernate类型的名字。

3

column (optional - defaults to the property name): the name of the primary key column.

4

unsaved-value (optional - defaults to a "sensible" value): an identifier property value that indicates an instance is newly instantiated (unsaved), distinguishing it from detached instances that were saved or loaded in a previous session.

5

access (optional - defaults to property): the strategy Hibernate should use for accessing the property value.


2.常用的生成策略

native

selects identitysequence or hilo depending upon the capabilities of the underlying database.

identity

对DB2,MySQL, MS SQL Server, Sybase和HypersonicSQL的内置标识字段提供支持。 返回的标识符是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.

3.XML生成策略

<id name="name">

<generator class="uuid">

</id>

在teacher类中,就无需设置id值了。


4.利用annotation生成id

@Id

@GeneratedValue      默认为auto_increatment     (native方式)                     
 public String getName() {
return name;
}


Identity只能在MySQL和sqlserver上做,sequence只能 在oracle上做。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值