Hibernate之主键生成策略
ibernate的主键生成器:
generator元素:表示了一个主键生成器,它用来为持久化类实例生成唯一的标识 。
1.1 程序员自己控制:assigned 意味不管你表是怎么建的你都得按照程序员的规矩来;不管你的表是不是自增列
1.2 数据库控制:1. identity(标识列/自动增长)
sequence(在mysql编程不建议使用,因为在hibernate内置的序列号默认的情况下所有关于hibernate的表都会使用,从而出现跳号的现象)
2. 指定用哪个序列:
<generator class="sequence" > <param name="序列表">xxx</param> </generator>
3.跨平台
1.3 hibernate控制:increment identity(标识列/自动增长)如果把数据库的自增列去掉就会报错,然而increment可以(它会去找你这个表里面的最大id)
uuid/uuid.hex
是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的
xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12)
其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。
SessionFactoryUtils工具类:可以来检测所写的映射文件是否正确
package com.chenjiahao.two.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * 这个类是在学习hibernate的过程中所有(整合ssh框架之前用) * * 作用: * 可以用来检测所写的映射文件是否正确 * @author dell * */ public class SessionFactoryUtils { private static SessionFactory sessionFactory; // 存放当前会话 private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); static { Configuration cfg = new Configuration(); Configuration configure = cfg.configure("/hibernate.cfg.xml"); sessionFactory = configure.buildSessionFactory(); } public static Session openSession() { Session session = threadLocal.get(); if (null == session) { session = sessionFactory.openSession(); threadLocal.set(session); } return session; } public static void closeSession() { Session session = threadLocal.get(); if (null != session) { if (session.isOpen()) { session.close(); } threadLocal.set(null); } } public static void main(String[] args) { Session session = openSession(); System.out.println(session.isConnected()); closeSession(); } }
配置文件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.chenjiahao.two.entity.Worker" table="t_hibernate_worker"> <id name="wid" type="java.lang.String" column="wid"> <!-- <generator class="assigned" /> --> <!-- <generator class="uuid" /> --> <!-- 指定用哪个序列 --> <!-- <generator class="sequence" > <param name="sequence_name">aaa</param> </generator> --> <!-- 自定义id --> <generator class="com.chenjiahao.two.id.Myts" /> </id> <property name="wname" type="java.lang.String" column="wname"> </property> </class> </hibernate-mapping>
配置文件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.chenjiahao.two.entity.Student" table="t_hibernate_student"> <id name="sid" type="java.lang.Integer" column="sid"> <!-- 程序员自己控制 --> <!-- <generator class="assigned" /> --> <!-- 数据库控制 --> <generator class="increment" /> <!-- hibernate控制 --> <!-- <generator class="identity" /> --> <!-- 指定用哪个序列 --> <!-- <generator class="sequence" > <param name="sequence_name">aaa</param> </generator> --> <!-- 自定义id --> <!-- <generator class="com.javaxl.two.id.Myts" /> --> </id> <property name="sname" type="java.lang.String" column="sname"> </property> </class> </hibernate-mapping>
Student实体类:
package com.chenjiahao.two.entity; import java.io.Serializable; public class Student implements Serializable{ 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 + "]"; } }
Worker实体类
package com.chenjiahao.two.entity; import java.io.Serializable; public class Worker implements Serializable{ 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 + "]"; } }
数据库表:
测试类:
package com.chenjiahao.two.dao; import java.io.Serializable; import org.hibernate.Session; import org.hibernate.Transaction; import com.chenjiahao.two.entity.Student; import com.chenjiahao.two.entity.Worker; import com.chenjiahao.two.util.SessionFactoryUtils; public class DemoDao { /** * 添加学生 * @param stu * @return */ public Serializable addStudent(Student stu) { Session session = SessionFactoryUtils.openSession(); Transaction transaction = session.beginTransaction(); Serializable saveId = session.save(stu); transaction.commit(); session.close(); return saveId; } /** * 添加工人 * @param worker * @return */ public Serializable addWorker(Worker worker) { Session session = SessionFactoryUtils.openSession(); Transaction transaction = session.beginTransaction(); Serializable saveId = session.save(worker); transaction.commit(); session.close(); return saveId; } public static void testStudent(String[] args) { DemoDao dao=new DemoDao(); Student stu=new Student(); // stu.setSid(1); stu.setSname("旺财"); System.out.println(dao.addStudent(stu)); } public static void main(String[] args) { DemoDao dao=new DemoDao(); Worker worker=new Worker(); worker.setWname("钢弹"); System.out.println(dao.addWorker(worker)); } }
自定义主键生成器:
package com.chenjiahao.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 session, Object object) throws HibernateException { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); return "shop_book_"+sdf.format(new Date()); } }