Hibernate学习笔记之主键生成策略和SchemaExport

一、Hibernate的主键生成策略
二、SchemaExport

一、Hibernate的主键生成策略
1.Assigned(常用)
Assigned方式由程序生成主键值,并且要在save()之前指定,否则会抛出异常。
特点:主键的生成值完全由用户决定,与底层数据库无关,用户需要维护主键值,在调用session.save()之前要指定主键值,注意:int auto_increment类型主键除外。

<id name="属性id" column="字段id" type="string">
<generator class="assigned" />
</id>

2.Hilo
Hilo使用高低位算法生成主键,高低位算法使用一个高位值和一个低位值,然后把算法得到的两个值拼接起来作为数据库中的唯一主键。Hilo方式需要额外的数据库表和字段提供高位值来源,默认情况下使用的表是:
hibernate_unique_key,默认的字段叫做next_hi,next_hi必须有一条记录 否则会出错误。
特点:需要额外的数据库表的支持,能保证同一个数据库中的主键的唯一性,但不能保证多个数据库之间主键的唯一性。Hilo主键生成方式由Hibernate维护,所以Hilo方式与底层数据库无关,但不应该手动修改hi/lo算法使用的表值,否则会引起主键重复的异常。

create hi_value(next_hi int not null);

insert into hi_value values(1);

<id name="属性id" column="字段id">
<generator class="hilo">
<param name="table">hi_value</param>
<param name="column">next_hi</param>
<param name="max_lo">100</param>
</generator>
</id>

3.Increment
Increment方式对主键值采取自动增长的方式生成新的主键值,但要求底层数据库支持Sequence。如Oracle,DB2等。需要在映射文件*.hbm.xml中加入Increment标识符的设置。
特点:由Hibernate本身维护,适用于所有的支持Sequence的数据库,不适合多进程并发更新数据库,适合单一进程访问数据库。不能用于集群环境。

<id name="属性id" column="字段id">
<generator class="increment" />
</id>

4.Identity(常用)
Identity是根据底层数据库来支持自动增长,不同的数据库用不同的主键增长方式。
特点:与底层数据库有关,适用于MySQL、DB2、MS、SQLServer,采用数据库生成的主键,用于为long、short、int类型生成唯一标识,使用MySQL和SQLServer的自增字段。这个方法不能放到Oracle,Oracle不支持自增字段。
Identity无需Hibernate和用户的干涉,使用较为方便,但不便于在不同的数据库间进行移植程序。

create table t_user(
id int auto_increment primary key,
name varchar(20)
);
<id name="属性id" column="字段id" type="long">
<generator class="identity" />
</id>

5.Sequence(常用)
Sequence方式需要底层数据库支持Sequence方式,例如Oracle数据库等。
特点:需要底层数据库支持序列,支持序列的数据库有DB2、Postgresql、Oracle、SAPDb等,在它们之间可以移植程序。从支持序列的数据库移植到不支持序列的数据库需要修改配置文件。

Oracle:create sequence seq_name increment by 1 start with 1;

需要主键时可以调用seq_name.nextval或者seq_name.curval得到,数据库会帮助我们维护这个sequence序列,保证每次取到的值唯一,如:

insert into tbl_name(id,name) values (seq_name.nextval,'Jim');
<id name="属性id" column="字段id" type="long">
<generator class="sequence">
<param name="sequence">seq_name</param>
</generator>
</id>

6.Native(常用)
Native主键生成方式会根据不同的底层数据库自动选择Identity、Sequence、Hilo主键生成方式。
特点:根据不同的底层数据库采用不同的主键生成方式。由于Hibernate会根据底层数据库采用不同的映射方式,因此便于程序移植,项目中如果用到多个数据库时,可以使用这种方式。

<id name="属性id" column="字段id">
<generator class="native" />
</id>

7.UUID
UUID使用128位UUID算法生成主键,能够保证网络环境下的主键唯一性,也就是能够保证在不同数据库及不同服务器下主键的唯一性。
特点:能够数据库中的主键唯一性,生成的主键占用比较多的空间。

<id name="属性id" column="字段id">
<generator class="uuid.hex" />
</id>

有关主键策略的Annotation注解:
@Entity
@Id
@GeneratedValue
@GenericGenerator
这里写图片描述
注意:应当尽量使用Annotation注解的方式生成主键,这样可以简化代码,不用写*.hbm.xml文件了。

二、SchemaExport
使用SchemaExport工具的作用是可以在控制台输出数据库操作语句,便于我们调试。

@Test
    public void testSchemaExport() {

        SchemaExport se = new SchemaExport(new AnnotationConfiguration().configure());
        se.create(true, true);
        //第一个true就是把DDL语句输出到控制台,第二个true就是根据持久化类和映射文件先执行删除再执行创建操作
    }

运行结果:

setUp()...
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Thu Jul 28 11:47:06 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Thu Jul 28 11:47:06 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
drop table if exists Students
create table Students (sid integer not null, sname varchar(255), primary key (sid))
tearDown()...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值