Hibernate主键生成策略
作者:互联网
文章目录
Hibernate主键生成
生成操控的对象
① 程序员自己控制:assigned
② 数据库控制: identity(标识列/自动增长) sequence
③ hibernate控制:increment uuid/uuid.hex
④ 其它:native
主键生成的要求
实体映射文件
<?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>
<class name="com.xy.two.entity.Student" table="t_student">
<id name="sid" type="java.lang.Integer" column="sid">
<!-- <generator class="assigned" /> -->
<!-- <generator class="increment" /> -->
<!-- <generator class="identity" /> -->
<!-- 指定序列 -->
<!-- <generator class="sequence" > <param name="sequence_name">aaa</param>
</generator> -->
<!-- <generator class="com.javaxl.two.id.Myts" /> -->
</id>
<property name="sname" type="java.lang.String" column="sname">
</property>
</class>
</hibernate-mapping>
assigned:数据类型不限、保存前必须赋值,就是id要自己去定义
identity:必须是数字,无需赋值,数据库设置了标识列的情况下自动增长
sequence: 数字,无需赋值, 默认使用hibernate_sequence这个序列,也可以通过序列的name去赋值
increment:数字,无需赋值,自动增长最大值,无需数据库设置标识列
uuid/uuid.hex :必须是字符串类型(是由容器自动生成的一个32位的字符串,.hex代表的是十六进制)
32位的字符串,无需赋值
native:等于identity+sequence
重点
自定义主键生成器:
1、在核心配置文件xx.hbm.xml指定主键生成器类<generator class=“xxx.MyTsGenerator”/ >
2、创建主键生成器类,实现org.hibernate.id.IdentifierGenerator接口即可,并还可以实现org.hibernate.id.Configurable接口来读取一些配置信息
3、然后重写generate()方法,就可以自己定义主键生成的格式了
例子
利用uuid生成主键来增加一条数据
worker实体类
package com.xy.two.entity;
public class Worker {
private String wid;
private String wname;
public String getWid() {
return wid;
}
public void setWid(String wid) {
this.wid = wid;
}
public String getWname() {
return wname;
}
public void setWname(String wname) {
this.wname = wname;
}
@Override
public String toString() {
return "Worker [wid=" + wid + ", wname=" + wname + "]";
}
}
Worker.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>
<class name="com.xy.two.entity.Worker" table="t_worker">
<id name="wid" type="java.lang.String" column="wid">
<generator class="uuid" />
</id>
<property name="wname" type="java.lang.String" column="wname">
</property>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 1. 数据库相关 -->
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.url">jdbc:mysql://localhost:3306/db_pro?useUnicode=true&characterEncoding=UTF-8
</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置本地事务(No CurrentSessionContext configured!) -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 2. 调试相关 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 3. 添加实体映射文件 -->
<mapping resource="com/xy/one/entity/User.hbm.xml" />
<!-- 主键生成策略 -->
<mapping resource="com/xy/two/entity/Student.hbm.xml" />
<mapping resource="com/xy/two/entity/Worker.hbm.xml" />
</session-factory>
</hibernate-configuration>
DemoDao
public static void main(String[] args) {
DemoDao dao=new DemoDao();
Worker wor=new Worker();
// stu.setSid(12);
wor.setWname("晓哈");
dao.addWorker(wor);
System.out.println(wor);
}
自定义主键生成器
Myts
package com.xy.two.id;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
public class Myts implements IdentifierGenerator{
@Override
public Serializable generate(SharedSessionContractImplementor arg0, Object arg1) throws HibernateException {
// TODO Auto-generated method stub
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return "xuyang_shop_book_"+sdf.format(new Date());
}
}
然后把Worker.hbm.xml的配置文件改为
<generator class="com.xy.two.id.Myts" />
然后输出结果为:id自动按照我们定义的格式生成了
标签:Hibernate,String,wname,wid,主键,hibernate,生成,public 来源: https://blog.csdn.net/qq_43517653/article/details/95906544