1.Student.hbm.xml配置

<hibernate-mapping package="com.wxh.hibernate.model">
<class name="Student" >
<id name="id">
<generator class="uuid"></generator>
</id>
<property name="age"></property>
<property name="name"></property>
</class>
</hibernate-mapping>

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.

id的类型要为String类型。终于在mysql中产生的id为varchar类型。

<hibernate-mapping package="com.wxh.hibernate.model">
<class name="Student" >
<id name="id">
<generator class="uuid"></generator>
</id>
<property name="age"></property>
<property name="name"></property>
</class>
</hibernate-mapping>
native

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

2.Annotation配置_IDENTITY_SEQUENCE

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}

Sequence生成器,使用oracle数据库。

@SequenceGenerator(name=”teacherSEQ”,sequenceName=”teacherSEQ_DB”)加在类前

@GeneratedValue(strategy=GenerationType.SEQUENCE,generator=”teacherSEQ”)加在方法前

3.TableGenerator(跨数据库平台)

@javax.persistence.TableGenerator(

name="Teacher_GEN",

table="GENERATOR_TABLE",

pkColumnName="pk_key",

valueColumnName="pk_value",

pkColumnValue="Teacher",

allocationSize=1

)

Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.wxh.pojo">
<!-- name相应类名称 -->
<class name="Student" >
<!-- name相应属性名字 -->
<id name="stuNo">
<!--
ID生成策略:针对不同的数据库对象使用不同的id生成方式
identity:mysql,mssqlservler(整型列)
sequence:oracle,db2(整型列)
uuid:一般用于字符串类型的主键
guid:同上(一般用于mysql,sqlserver)
native:经常使用配置(依据不同的数据库自己主动选择使用identity,sequence。hilo中的当中一个作为生成策略)
-->
<generator class="native">
<!-- 设置自己定义序列的名字 -->
<param name="sequence">auto_sid</param>
</generator>
</id>
<property name="name" column="stuname" length="16" not-null="true" unique="false">
</property>
<property name="major" length="32"></property>
<property name="comingYear"></property>
<property name="type" length="16"></property>
</class>
</hibernate-mapping>

Teacher.hbm.xml

<?xml version="1.0" encoding="UTF-8"?

>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.wxh.pojo">
<!-- name相应类名称 -->
<class name="Teacher" >
<!-- name相应属性名字 -->
<id name="tid">
<!--
ID生成策略:针对不同的数据库对象使用不同的id生成方式
identity:mysql,mssqlservler(整型列)
sequence:oracle,db2(整型列)
uuid:一般用于字符串类型的主键
guid:同上(一般用于mysql,sqlserver)
native:经常使用配置(依据不同的数据库自己主动选择使用identity,sequence,hilo中的当中一个作为生成策略)
-->
<generator class="native">
<!-- 设置自己定义序列的名字 -->
<param name="sequence">auto_tid</param>
</generator>
</id>
<property name="tname" length="16" not-null="true" unique="false">
</property> </class>
</hibernate-mapping>

hibernate ID生成策略配置的更多相关文章

  1. hibernate(四)ID生成策略

    一.ID生成策略配置 1.ID生成方式在xml中配置方式: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping P ...

  2. Hibernate学习笔记2.4(Hibernate的Id生成策略)

    通过设置告诉id该怎么设置. 1.通过xml方式 1.assigned 主键由外部程序负责生成,在 save() 之前必须指定一个.Hibernate不负责维护主键生成.与Hibernate和底层数据 ...

  3. Hibernate系列之ID生成策略

    一.概述 hibernate中使用两种方式实现主键生成策略,分别是XML生成id和注解方式(@GeneratedValue),下面逐一进行总结. 二.XML配置方法 这种方式是在XX.hbm.xml文 ...

  4. Rhythmk 学习 Hibernate 03 - Hibernate 之 延时加载 以及 ID 生成策略

    Hibernate 加载数据 有get,跟Load 1.懒加载: 使用session.load(type,id)获取对象,并不读取数据库,只有在使用返回对象值才正真去查询数据库. @Test publ ...

  5. [Hibernate开发之路](4)ID生成策略

    一 对象关系数据库映射之Id 被映射的类必须定义相应数据库表主键字段.大多数类有一个JavaBeans风格的属性, 为每个实例包括唯一的标识. <id> 元素定义了该属性到数据库表主键字段 ...

  6. mybatis 针对SQL Server 的 主键id生成策略

    SQL Server中命令: select newId()  ,可以得到SQL server数据库原生的UUID值,因此我们可以将这条指令写到 Mybatis的主键生成策略配置selectKey中. ...

  7. JPA ID生成策略(转---)

    尊重原创:http://tendyming.iteye.com/blog/2024985 JPA ID生成策略 @Table Table用来定义entity主表的name,catalog,schema ...

  8. 可实现的全局唯一有序ID生成策略

    在博客园搜素全局唯一有序ID,罗列出来的文章大致讲述了以下几个问题,常见的生成全局唯一id的常见方法 :使用数据库自动增长序列实现 : 使用UUID实现:  使用redis实现: 使用Twitter的 ...

  9. 图解Janusgraph系列-分布式id生成策略分析

    JanusGraph - 分布式id的生成策略 大家好,我是洋仔,JanusGraph图解系列文章,实时更新~ 本次更新时间:2020-9-1 文章为作者跟踪源码和查看官方文档整理,如有任何问题,请联 ...

随机推荐

  1. 单元测试Struts2的Action(包含源码)

    很久没有从头搭建Struts2的环境了.最近,认真实践了单元测试Struts2.Spring等Java项目. 今天特意写的是单元测试Struts2的Action,遇到了不少问题,果然是实践出真知啊. ...

  2. SQL SERVER-identity | @@identity | scope_identity

    主键自增 IDENTITY(1,1),MS SQL Server 使用 IDENTITY 关键字来执行 auto-increment 任务. 在上面的实例中,IDENTITY 的开始值是 1,每条新记 ...

  3. ZOJ 3329

    方程很明显有 d[i]=sum(pk*d[i+k])+p0*d[0]; 其中pi可以在开始时枚举求出. 设d[i]=A[i]*d[0]+B[i], 代入上式 d[i]=(sum(pk*A[i+k])+ ...

  4. JDK1.7中的ThreadPoolExecutor源代码剖析

    JDK1. 7中的ThreadPoolExecutor 线程池,顾名思义一个线程的池子,池子里存放了非常多能够复用的线程,假设不用线程池相似的容器,每当我们须要创建新的线程时都须要去new Threa ...

  5. [Tools] Using mobile device for debugging your mobile web site

    1. First you have enable "Developer mode" on your mobile device. (Different device might b ...

  6. HDU 4415 Assassin&#39;s Creed(贪心)

    pid=4415">HDU 4415 题意: 壮哉我Assassin! E叔有一柄耐久度为m的袖剑,以及n个目标士兵要去解决. 每解决掉一个士兵,消耗袖剑Ai的耐久度.且获得该士兵的武 ...

  7. php,二维数组的输出出现了问题,提示:Notice: Array to string conversion

    <?php $arr=array(array("111","222","333"),array("444",&qu ...

  8. MongoDB数据查询详解

    查询全部 ​ db.infos.find(); db.infos.find({"url":"www.baidu.com"}); id不要显示出来 db.info ...

  9. MySQL循环语句之while循环测试

    转自:http://www.nuoweb.com/database/7614.html MySQL有循环语句操作,while 循环.loop循环和repeat循环,目前我只测试了 while 循环,下 ...

  10. (转载)打开一个本地apk进行安装

    1 2 3 4 5 6 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); File file = new File ...