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. 原生ajax的请求过程

    原生ajax的请求过程 创建全平台兼容的XMLHttpRequest对象: function getXHR(){ var xhr = null; if(window.XMLHttpRequest) { ...

  2. js常用特效——选项卡效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. Opencv 视频转为图像序列

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50283303 基于OpenCV的视频转 ...

  4. cogs 2060. 除法表达式

    2060. 除法表达式 ★★   输入文件:baoquansl.in   输出文件:baoquansl.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 经过无尽的蘑菇和 ...

  5. git batch

    git batch 不用每次自己写了:不是特别推荐哦: git add . git commit -m "commit" git push git status

  6. Windows 8.1硬盘安装Ubuntu 14.04双系统

    Windows 8.1硬盘安装Ubuntu 14.04双系统 学习了: http://www.jb51.net/os/windows/298507.html http://www.linuxidc.c ...

  7. VS 2013+Qt 5.4.1

    Qt应用能够用Qt Creator开发,也能够使用Visual Studio. 我之前一直用Qt Creator.也始终认为这是最好的选择.只是有人偏爱Visual Studio,我也由于工作须要,要 ...

  8. hdu_4707

    算是水题一道吧,我也没有建树,看别人又用vector,又用bfs,dfs的,对vector不熟,所以就模拟了一下 #include<iostream> #include<string ...

  9. select into in mysql

    http://stackoverflow.com/questions/16809393/select-into-in-mysql Use the CREATE TABLE SELECT syntax. ...

  10. linux ps 命令查看进程状态

    显示其他用户启动的进程(a) 查看系统中属于自己的进程(x) 启动这个进程的用户和它启动的时间(u) 使用“date -s”命令来修改系统时间 比如将系统时间设定成1996年6月10日的命令如下. # ...