Hibernate之主键生成策略

本文深入探讨了Hibernate中四种主键生成策略:人工控制、数据库控制、Hibernate控制及自定义主键生成器,通过具体代码示例展示了每种策略的应用场景与实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

dA前言:
通过对Hibernate入门的了解,我们接下来了解一下它的一个知识点,也就是hibernate的主键生成器。
hibernate的主键生成器:
generator元素:表示了一个主键生成器,它用来为持久化类实例生成唯一的标识 。
先来看一下我们需要的实体类:
Student.java
代码如下:

package com.dengrenli.two.entity;

public class Student {

	private Integer sid;
	private String sname;
	public Integer getSid() {
		return sid;
	}
	public void setSid(Integer sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	@Override
	public String toString() {
		return "Student [sid=" + sid + ", sname=" + sname + "]";
	}
	public Student(Integer sid, String sname) {
		super();
		this.sid = sid;
		this.sname = sname;
	}
	public Student() {
		super();
	}
	
}

Worker.java
代码如下:

package com.dengrenli.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 + "]";
	}
	public Worker(String wid, String wname) {
		super();
		this.wid = wid;
		this.wname = wname;
	}
	public Worker() {
		super();
	}
	
}

两个实体类的唯一不同点就是它们主键id列的类型不一样
1 程序员自己控制:assigned
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>
	<class name="com.dengrenli.two.entity.Student" table="t_hibernate_student">
		<id name="sid" type="java.lang.Integer" column="sid">
			<generator class="assigned" />
		<!-- 	<generator class="identity" /> -->
<!-- 			<generator class="increment" /> -->
			<!-- <generator class="sequence" /> -->
			<!-- <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>

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.dengrenli.two.entity.Worker" table="t_hibernate_worker">
		<id name="wid" type="java.lang.String" column="wid">
		     <generator class="assigned" /> 
		    <!--   <generator class="sequence" />  -->
		     <!--  <generator class="uuid" />  -->
			<!-- <generator class="com.dengrenli.two.id.MyTsGenerator" /> -->
			<!-- <generator class="sequence" /> -->
			<!-- <generator class="sequence" > <param name="sequence_name">aaa</param> 
				</generator> -->
			<!-- <generator class="com.javaxl.two.id.Myts" /> -->
		</id>

		<property name="wname" type="java.lang.String" column="wname">
		</property>
	</class>
</hibernate-mapping>

在hibernate.cfg.xml里面添加我们的映射文件
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/t226?useUnicode=true&amp;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/dengrenli/one/entity/user.hbm.xml"/>
	<!--   主键生成策略 -->
	  <mapping  resource="com/dengrenli/two/entity/Student.hbm.xml"/>
	  <mapping  resource="com/dengrenli/two/entity/Worker.hbm.xml"/>
	</session-factory>
	</hibernate-configuration>

然后编写我们的测试类
DemoDao.java
代码如下:

package com.dengrenli.two.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.dengrenli.two.entity.Student;
import com.dengrenli.two.entity.Worker;
import com.dengrenli.two.util.SessionFactoryUtils;
/**
 * hibernate中的主键生成策略
 * 1.人工控制
 * 2.数据库控制
 * 3.hibernate控制
 * 4.自定义主键生成策略
 * @author machenike
 *
 */
public class DemoDao {
	/**
	 * 新增学生
	 * @param stu
	 */
	public void addStudent(Student stu) {
		Session session = SessionFactoryUtils.openSession();
		Transaction transaction = session.beginTransaction();
		session.save(stu);
		transaction.commit();
		SessionFactoryUtils.closeSession();
		
	}
	/**
	 * 新增工人
	 * @param stu
	 */
	public void addWorker(Worker worker) {
		Session session = SessionFactoryUtils.openSession();
		Transaction transaction = session.beginTransaction();
		session.save(worker);
		transaction.commit();
		SessionFactoryUtils.closeSession();
		
	}
	
	public static void main(String[] args) {
		DemoDao dao=new DemoDao();
		Student stu=new Student();
		stu.setSname("礼");
		stu.setSid(76);
		dao.addStudent(stu);
	}
}

运行结果如下:
在这里插入图片描述
2 数据库控制: identity(标识列/自动增长)
在执行数据库控制的时候,需要把该主键类设为自增长,不设自增长的话执行测试类结果会报错,因为主键自增长要int类型,所以主键id为String类型的Worker.java就不能使用数据库控制
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>
	<class name="com.dengrenli.two.entity.Student" table="t_hibernate_student">
		<id name="sid" type="java.lang.Integer" column="sid">
			<!-- <generator class="assigned" /> -->
			<generator class="identity" />
<!-- 			<generator class="increment" /> -->
			<!-- <generator class="sequence" /> -->
			<!-- <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>

然后编写我们的测试类
DemoDao.java
代码如下:

package com.dengrenli.two.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.dengrenli.two.entity.Student;
import com.dengrenli.two.entity.Worker;
import com.dengrenli.two.util.SessionFactoryUtils;
/**
 * hibernate中的主键生成策略
 * 1.人工控制
 * 2.数据库控制
 * 3.hibernate控制
 * 4.自定义主键生成策略
 * @author machenike
 *
 */
public class DemoDao {
	/**
	 * 新增学生
	 * @param stu
	 */
	public void addStudent(Student stu) {
		Session session = SessionFactoryUtils.openSession();
		Transaction transaction = session.beginTransaction();
		session.save(stu);
		transaction.commit();
		SessionFactoryUtils.closeSession();
		
	}
	/**
	 * 新增工人
	 * @param stu
	 */
	public void addWorker(Worker worker) {
		Session session = SessionFactoryUtils.openSession();
		Transaction transaction = session.beginTransaction();
		session.save(worker);
		transaction.commit();
		SessionFactoryUtils.closeSession();
		
	}
	
	public static void main(String[] args) {
		DemoDao dao=new DemoDao();
		Student stu=new Student();
		stu.setSname("礼1");
		stu.setSid(76);
		dao.addStudent(stu);
	}
}

结果如下:
在这里插入图片描述
3 .hibernate控制:increment uuid/uuid.hex
使用 hibernate控制就不用把主键id设为自增长了,它是hibernate自动生成标识列的
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>
	<class name="com.dengrenli.two.entity.Student" table="t_hibernate_student">
		<id name="sid" type="java.lang.Integer" column="sid">
			<!-- <generator class="assigned" /> -->
			<!-- <generator class="identity" /> -->
			<generator class="increment" />
			<!-- <generator class="sequence" /> -->
			<!-- <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>

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.dengrenli.two.entity.Worker" table="t_hibernate_worker">
		<id name="wid" type="java.lang.String" column="wid">
		     <!-- <generator class="assigned" />  -->
		       <generator class="uuid" />  
			<!-- <generator class="com.dengrenli.two.id.MyTsGenerator" /> -->
			<!-- <generator class="sequence" /> -->
			<!-- <generator class="sequence" > <param name="sequence_name">aaa</param> 
				</generator> -->
			<!-- <generator class="com.javaxl.two.id.Myts" /> -->
		</id>

		<property name="wname" type="java.lang.String" column="wname">
		</property>
	</class>
</hibernate-mapping>

编写测试类DemoDao.java
DemoDao.java
代码如下:

package com.dengrenli.two.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.dengrenli.two.entity.Student;
import com.dengrenli.two.entity.Worker;
import com.dengrenli.two.util.SessionFactoryUtils;
/**
 * hibernate中的主键生成策略
 * 1.人工控制
 * 2.数据库控制
 * 3.hibernate控制
 * 4.自定义主键生成策略
 * @author machenike
 *
 */
public class DemoDao {
	/**
	 * 新增学生
	 * @param stu
	 */
	public void addStudent(Student stu) {
		Session session = SessionFactoryUtils.openSession();
		Transaction transaction = session.beginTransaction();
		session.save(stu);
		transaction.commit();
		SessionFactoryUtils.closeSession();
		
	}
	/**
	 * 新增工人
	 * @param stu
	 */
	public void addWorker(Worker worker) {
		Session session = SessionFactoryUtils.openSession();
		Transaction transaction = session.beginTransaction();
		session.save(worker);
		transaction.commit();
		SessionFactoryUtils.closeSession();
		
	}
	
	/*public static void main(String[] args) {
		DemoDao dao=new DemoDao();
		Student stu=new Student();
		stu.setSname("礼1");
		stu.setSid(76);
		dao.addStudent(stu);
	}*/
	
	public static void main(String[] args) {
		DemoDao dao=new DemoDao();
		Worker wo=new Worker();
		wo.setWname("礼11");
		dao.addWorker(wo);;
	}
}

效果如图所示:
在这里插入图片描述
4 自定义主键生成器
4.1 *.hbm.xml指定主键生成器类
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.dengrenli.two.entity.Worker" table="t_hibernate_worker">
		<id name="wid" type="java.lang.String" column="wid">
		     <!-- <generator class="assigned" />  -->
		       <!-- <generator class="uuid" />   -->
			 <generator class="com.dengrenli.two.id.MyTsGenerator" /> 
			<!-- <generator class="sequence" /> -->
			<!-- <generator class="sequence" > <param name="sequence_name">aaa</param> 
				</generator> -->
			<!-- <generator class="com.javaxl.two.id.Myts" /> -->
		</id>

		<property name="wname" type="java.lang.String" column="wname">
		</property>
	</class>
</hibernate-mapping>

MyTsGenerator.java

package com.dengrenli.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 MyTsGenerator implements IdentifierGenerator {

	@Override
	public Serializable generate(SharedSessionContractImplementor arg0, Object arg1) throws HibernateException {
        SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return "book_order_"+sdf.format(new Date());
	}

}

编写测试类DemoDao.java
DemoDao.java
代码如下:

package com.dengrenli.two.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.dengrenli.two.entity.Student;
import com.dengrenli.two.entity.Worker;
import com.dengrenli.two.util.SessionFactoryUtils;
/**
 * hibernate中的主键生成策略
 * 1.人工控制
 * 2.数据库控制
 * 3.hibernate控制
 * 4.自定义主键生成策略
 * @author machenike
 *
 */
public class DemoDao {
	/**
	 * 新增学生
	 * @param stu
	 */
	public void addStudent(Student stu) {
		Session session = SessionFactoryUtils.openSession();
		Transaction transaction = session.beginTransaction();
		session.save(stu);
		transaction.commit();
		SessionFactoryUtils.closeSession();
		
	}
	/**
	 * 新增工人
	 * @param stu
	 */
	public void addWorker(Worker worker) {
		Session session = SessionFactoryUtils.openSession();
		Transaction transaction = session.beginTransaction();
		session.save(worker);
		transaction.commit();
		SessionFactoryUtils.closeSession();
		
	}
	
	/*public static void main(String[] args) {
		DemoDao dao=new DemoDao();
		Student stu=new Student();
		stu.setSname("礼1");
		stu.setSid(76);
		dao.addStudent(stu);
	}*/
	
	public static void main(String[] args) {
		DemoDao dao=new DemoDao();
		Worker wo=new Worker();
		wo.setWname("礼1");
		dao.addWorker(wo);;
	}
}

效果如图所示:
在这里插入图片描述
谢谢大家,多多指教!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值