码迷,mamicode.com
首页 > Web开发 > 详细

hibernate主键生成策略

时间:2019-08-03 10:45:28      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:build   prope   配置文件   mat   utf-8   package   nbsp   over   comm   

创建2个实体类:

Worker.java:

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

  Student.java:

package com.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();
	}
	
}

  在同包下放入Student.hbm.xml 和Worker.hbm.xml2个配置

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.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" > <param name="sequence_name">aaa</param> 
				</generator> -->
		</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.entity.Worker" table="t_hibernate_worker">
		<id name="wid" type="java.lang.String" column="wid">
			<generator class="com.id.MyTsGenerator" />
<!-- --> <!-- <generator class="sequence" /> --> <!-- <generator class="sequence" > <param name="sequence_name">aaa</param> </generator> --> </id> <property name="wname" type="java.lang.String" column="wname"> </property> </class> </hibernate-mapping>

  要用到的工具类:

SessionFacyoryUtils:

package com.util;

/**
 * 仅在学习hibernate的工程中使用,进入spring的学习就用不到了,后面会有ssh替代
 * 作用:
 * 	用来检测hibernate中的配置文件的准确性
 * 			hibernate.cfg.xml
 * 			*.hbm.xml
 * @author xyls
 */
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SessionFactoryUtils {
	private static SessionFactory sessionFactory;
	static {
		Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
		sessionFactory = cfg.buildSessionFactory();
	}
	
	public static Session openSession() {
		//从本地的线程中获取session会话,(第一次需要创建)
		//第二次就能反复利用,节约性能
		Session session = sessionFactory.getCurrentSession();
		if(session == null) {
			session = sessionFactory.openSession();
		}
		return session;
	}
	
	public static void closeSession() {
		Session session = sessionFactory.getCurrentSession();
		if(session != null && session.isOpen()) {
			session.close();
		}
	}
	
	public static void main(String[] args) {
		Session session = SessionFactoryUtils.openSession();
		session.beginTransaction();
		System.out.println(session.isConnected());
		SessionFactoryUtils.closeSession();
		System.out.println(session.isConnected());
	}
}

  测试:

DemoDao:

    package com.dao;

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

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

  

程序员自行控制:assigned(数据类型不限、保存前必须赋值)

技术图片

 

数据库控制:identity(标识列/自动增长、  数字,无需赋值)

技术图片

 

 数据库控制:sequence(数字,无需赋值, 默认使hibernate_sequence这个序列,也可以通过sequence/sequence_name参数赋值

技术图片

hibernate控制:increment(数字,无需赋值)/uuid(32位的字符串,无需赋值,)

技术图片

 自定义主键生成器

建一个主键生成器类(实现org.hibernate.id.IdentifierGenerator接口):

package com.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 session, Object object) throws HibernateException {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		
		return "book_order_"+sdf.format(new Date());
	}

}

  测试:

技术图片

hibernate主键生成策略

标签:build   prope   配置文件   mat   utf-8   package   nbsp   over   comm   

原文地址:https://www.cnblogs.com/omji0030/p/11293764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!