• hibernate的id生成策略


    hibernate的id生成策略  

    hibernate文档写道

    1、自动增长identity

    适用于MySQL、DB2、MS SQL Server,采用数据库生成的主键,用于为long、short、int类型生成唯一标识
    使用SQL Server 和 MySQL 的自增字段,这个方法不能放到 Oracle 中,Oracle 不支持自增字段,要设定sequence(MySQL 和 SQL Server 中很常用)
    数据库中的语法如下:
    MySQL:create table t_user(id int auto_increment primary key, name varchar(20));
    SQL Server:create table t_user(id int identity(1,1) primary key, name varchar(20));

    <id name="id" column="id" type="long">
      <generator class="identity" />
    </id>

    2、sequence

    DB2、Oracle均支持的序列,用于为long、short或int生成唯一标识
    数据库中的语法如下:
    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, ‘Jimliu’);

    <id name="id" column="id" type="long">
      <generator class="sequence">
        <param name="sequence">seq_name</param>
      </generator>
    </id>

    如果我们没有指定sequence参数,则Hibernate会访问一个默认的sequence,是hibernate_sequence,我们也需要在数据库中建立这个sequence
    此外,sequence还可以有另外一个参数是paramters,可以查看Hibernate的API了解它的用法,见org.hibernate.id.SequenceGenerator
    调用数据库的sequence来生成主键,要设定序列名,不然hibernate无法找到:
    <param name="sequence">NAME_SEQ</param>(Oracle中很常用)

    3、native(常用)

    会根据底层数据库的能力,从identity、sequence中选择一个,灵活性更强,如果能支持identity则使用identity,如果支持sequence则使用sequence。例如MySQL使用identity,Oracle使用sequence

    但此时,如果选择sequence,则所有的表的主键都会从Hibernate默认的sequence表中取。并且,有的数据库对于默认情况主键生成测试的支持,效率并不是很高
    对于 oracle 采用 Sequence 方式,对于MySQL 和 SQL Server 采用identity(自增主键生成机制),native就是将主键的生成工作交由数据库完成,hibernate不管(很常用)

    <id name="id" column="id">
        <generator class="native" />
    </id>

    4、assigned(常用)

    自己赋值,由应用程序负责生成主键标识符,往往使用在数据库中没有代理主键,使用的主键与业务相关的情况,如:

    <id name="id" column="id" type="string">
        <generator class="assigned" />
    </id>

    这种主键的生成方式不建议使用,在数据库表设计时就应该使用代理主键(surrogate key),不应使用自然主键(natural key具有业务含义),在没有指定<generator>标签时,默认就是assigned主键的生成方式

    在插入数据的时候主键由用户自己添加,hibernate也不管

    5、uuid

    UUID:Universally Unique Identifier,是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字,标准的UUID格式为:

    xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12)   ,  其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。

    <id name="id" column="id">
        <generator class="uuid" />
    </id>

    Hibernate在保存对象时,生成一个UUID字符串作为主键,保证了唯一性,但其并无任何业务逻辑意义,只能作为主键,唯一缺点长度较大,32位(Hibernate将UUID中间的“-”删除了)的字符串,占用存储空间大,但是有两个很重要的优点,Hibernate在维护主键时,不用去数据库查询,从而提高效率,而且它是跨数据库的,以后切换数据库极其方便。

    特点:uuid长度大,占用空间大,跨数据库,不用访问数据库就生成主键值,所以效率高且能保证唯一性,移植非常方便,推荐使用。不推荐使用guid,所以不再介绍guid

    MySQL中使用select uuid()语句获得的为36位(包含标准格式的“-”)

    Oracle中,使用select rawtohex(sys_guid()) from dual语句获得的为32位(不包含“-”) 

    5、项目用到:

    安装有oracle数据库,创建数据库,总是要创建一个主键ID,唯一标示各条记录,但oracle不支持自动编号,所以还得创建一个SEQUENCE(序列)语句如
    mysql不支持序列 直接@GenericGenerator(name = "generator",strategy = "native")

    sql增加序列:
    create sequence bign nocycle maxvalue 9999999999 start with1; //增加数据
     
     
    liquibase 增加序列:
    <changeSet author="zhaoyanhao" id="pub-20190827-001" dbms="oracle">
        <comment>新增序列so_pub_dic_checkopinion</comment>
        <createSequence schemaName="${schema.dlmis}" sequenceName="so_pub_dic_checkopinion" startValue="1" incrementBy="1" />
    </changeSet>
    为了兼容oracle,对应mapping类的主键上使用上边定义的序列so_pub_dic_checkopinion如下:
    @Id
    @Column(name = "opinion_id",unique = true,nullable = false)
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator",strategy = "native",parameters = {@Parameter(name = "sequence", value = SchemaConst.DLMIS_ + "so_pub_dic_checkopinion")})
    private Integer opinionId;

  • 相关阅读:
    期望dp+高斯消元优化——uvalive4297好题
    期望dp+高斯消元+bfs——hdu4418
    倒车入库太难了?掌握技巧,其实它也可以很简单
    倒车影像辅助线怎么看_倒车影像怎么看图解
    教你手动挡驾驶技术如何提高驾车技巧
    【搜狐驾校】手动更安全 如何换档最合理
    专家告诉您:手动挡汽车驾驶技术
    Mybatis if test中字符串比较和Mybatis的like查询
    性能优化书籍
    微信就能查社保、开个税证明啦
  • 原文地址:https://www.cnblogs.com/zhaoyanhaoBlog/p/11427923.html
Copyright © 2020-2023  润新知