hibernate ID生成策略配置
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>
nativeselects identity, sequence 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生成策略配置的更多相关文章
- hibernate(四)ID生成策略
一.ID生成策略配置 1.ID生成方式在xml中配置方式: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping P ...
- Hibernate学习笔记2.4(Hibernate的Id生成策略)
通过设置告诉id该怎么设置. 1.通过xml方式 1.assigned 主键由外部程序负责生成,在 save() 之前必须指定一个.Hibernate不负责维护主键生成.与Hibernate和底层数据 ...
- Hibernate系列之ID生成策略
一.概述 hibernate中使用两种方式实现主键生成策略,分别是XML生成id和注解方式(@GeneratedValue),下面逐一进行总结. 二.XML配置方法 这种方式是在XX.hbm.xml文 ...
- Rhythmk 学习 Hibernate 03 - Hibernate 之 延时加载 以及 ID 生成策略
Hibernate 加载数据 有get,跟Load 1.懒加载: 使用session.load(type,id)获取对象,并不读取数据库,只有在使用返回对象值才正真去查询数据库. @Test publ ...
- [Hibernate开发之路](4)ID生成策略
一 对象关系数据库映射之Id 被映射的类必须定义相应数据库表主键字段.大多数类有一个JavaBeans风格的属性, 为每个实例包括唯一的标识. <id> 元素定义了该属性到数据库表主键字段 ...
- mybatis 针对SQL Server 的 主键id生成策略
SQL Server中命令: select newId() ,可以得到SQL server数据库原生的UUID值,因此我们可以将这条指令写到 Mybatis的主键生成策略配置selectKey中. ...
- JPA ID生成策略(转---)
尊重原创:http://tendyming.iteye.com/blog/2024985 JPA ID生成策略 @Table Table用来定义entity主表的name,catalog,schema ...
- 可实现的全局唯一有序ID生成策略
在博客园搜素全局唯一有序ID,罗列出来的文章大致讲述了以下几个问题,常见的生成全局唯一id的常见方法 :使用数据库自动增长序列实现 : 使用UUID实现: 使用redis实现: 使用Twitter的 ...
- 图解Janusgraph系列-分布式id生成策略分析
JanusGraph - 分布式id的生成策略 大家好,我是洋仔,JanusGraph图解系列文章,实时更新~ 本次更新时间:2020-9-1 文章为作者跟踪源码和查看官方文档整理,如有任何问题,请联 ...
随机推荐
- ZJU 2605 Under Control
Under Control Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ...
- ASP.NET-HttpPostedFileBase file为null的问题
MVC使用Ajax.BeginForm上传图片时HttpPostedFileBase file为null,Request.Files获取不到文件,问题分析是页面中存在jquery.unobtrusiv ...
- POJ-1785-Binary Search Heap Construction(笛卡尔树)
Description Read the statement of problem G for the definitions concerning trees. In the following w ...
- 设计网页录入信息与自己定义server数据接收
需求:设计一个注冊网页用于录入username和登录password.并将数据传入server并显示出来. 1.前言:网页提交的 get 和 post 两种方式. (1)对于get提交方式,以本文中样 ...
- hdu2838Cow Sorting(树状数组+逆序数)
题目链接:点击打开链接 题意描写叙述:给定一个长度为100000的数组,每一个元素范围在1~100000,且互不同样,交换当中的随意两个数须要花费的代价为两个数之和. 问怎样交换使数组有序.花费的代价 ...
- m_Orchestrate learning system---十四、数据表中字段命名规则
m_Orchestrate learning system---十四.数据表中字段命名规则 一.总结 一句话总结:a.保证唯一 b.见名知意 1.注意php中的数组类函数和字符串类函数的前缀? 数组类 ...
- vue 如何实现在函数中触发路由跳转
this.$router.push({path:'/index'}) 欢迎加入前端交流群交流知识&&获取视频资料:749539640 methods:{ click(){ if(dat ...
- POJ 3188暴搜
题意: 思路: 裸的暴搜 --. 但是要注意如果你不用所有的按键就能输出最优解的话一定要把所有的字母都安排到一个位置-. 我的一群PE就是这么来的-- 为什么写的人这么少-- // by Sirius ...
- [转]Adobe CC 2018 下载链接 Creative Cloud 2018 - Creative Cloud 2018 – Adobe CC 2018 Download Links
Creative Cloud 2018 – Adobe CC 2018 Download Links – ALL Languages Adobe CC 2018Direct Downloads Win ...
- python 3.x 学习笔记11 (静态、类、属性、特殊成员方法)
1.静态方法通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法.静态方法是不可以访问实例变量或类变量的即没有self,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什 ...