Hibernate之主键生成策略
1.1 程序员自己控制:assigned
1.2 数据库控制: identity(标识列/自动增长) sequence
1.3 hibernate控制:increment uuid/uuid.hex
1.4 其它:native
hibernate.cfg.xml核心配置文件
Student.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="com.MavenHibernate.two.entity.Student" table="t_hibernate_student"> 7 <id name="sid" type="java.lang.Integer" column="sid"> 8 <generator class="assigned" /> 9 <!-- <generator class="sequence" /> --> 10 <!-- <generator class="increment" /> --> 11 <!-- <generator class="identity" /> --> 12 <!-- <generator class="sequence" /> --> 13 <!-- <generator class="sequence" > <param name="sequence_name">aaa</param> 14 </generator> --> 15 <!-- <generator class="com.javaxl.two.id.Myts" /> --> 16 </id> 17 <property name="sname" type="java.lang.String" column="sname"> 18 </property> 19 </class> 20 </hibernate-mapping>
Student实体类与Student.hbm.xml中的name值一一对应
1 package com.MavenHibernate.two.entity; 2 3 public class Student { 4 5 private Integer sid; 6 private String sname; 7 public Integer getSid() { 8 return sid; 9 } 10 public void setSid(Integer sid) { 11 this.sid = sid; 12 } 13 public String getSname() { 14 return sname; 15 } 16 public void setSname(String sname) { 17 this.sname = sname; 18 } 19 @Override 20 public String toString() { 21 return "Student [sid=" + sid + ", sname=" + sname + "]"; 22 } 23 public Student(Integer sid, String sname) { 24 super(); 25 this.sid = sid; 26 this.sname = sname; 27 } 28 public Student() { 29 super(); 30 } 31 32 33 }
Worker.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="com.MavenHibernate.two.entity.Worker" table="t_hibernate_worker"> 7 <id name="wid" type="java.lang.String" column="wid"> 8 <!-- <generator class="assigned" /> --> 9 <!-- <generator class="uuid" /> --> 10 <generator class="com.MavenHibernate.id.MyTsGenerator" /> 11 <!-- <generator class="sequence" > <param name="sequence_name">aaa</param> 12 </generator> --> 13 <!-- <generator class="com.javaxl.two.id.Myts" /> --> 14 </id> 15 16 <property name="wname" type="java.lang.String" column="wname"> 17 </property> 18 </class> 19 </hibernate-mapping>
Worker.hbm.xml对应的Worker实体类
1 package com.MavenHibernate.two.entity; 2 3 public class Worker { 4 5 private String wid; 6 private String wname; 7 public String getWid() { 8 return wid; 9 } 10 public void setWid(String wid) { 11 this.wid = wid; 12 } 13 public String getWname() { 14 return wname; 15 } 16 public void setWname(String wname) { 17 this.wname = wname; 18 } 19 @Override 20 public String toString() { 21 return "Worker [wid=" + wid + ", wname=" + wname + "]"; 22 } 23 public Worker(String wid, String wname) { 24 super(); 25 this.wid = wid; 26 this.wname = wname; 27 } 28 public Worker() { 29 super(); 30 } 31 32 }
程序员自己控制:assigned
assigned
数据类型不限、保存前必须赋值
1 package com.MavenHibernate.dao; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 6 import com.MavenHibernate.two.entity.Student; 7 import com.MavenHibernate.two.entity.Worker; 8 import com.MavenHibernate.two.util.SessionFactoryUtils; 9 /** 10 * hibernate中的主键生成策略 11 * 1.人工控制 12 * 2.数据库控制 13 * 3.hibernate控制 14 * 4.自定义主键生成策略 15 * @author Administrator 16 * 17 */ 18 public class DemoDao { 19 20 /** 21 * 新增学生 22 * @param stu 23 */ 24 public void addStudent(Student stu) { 25 Session session = SessionFactoryUtils.openSession(); 26 Transaction transaction = session.beginTransaction(); 27 session.save(stu); 28 transaction.commit(); 29 SessionFactoryUtils.closeSession(); 30 } 31 /** 32 * 新增功能 33 * @param worker 34 */ 35 public void addWorker(Worker worker) { 36 Session session = SessionFactoryUtils.openSession(); 37 Transaction transaction = session.beginTransaction(); 38 session.save(worker); 39 transaction.commit(); 40 SessionFactoryUtils.closeSession(); 41 } 42 43 public static void testStudent() { 44 DemoDao dao = new DemoDao(); 45 Student stu = new Student(); 46 stu.setSname("XFQ"); 47 stu.setSid(65); 48 dao.addStudent(stu); 49 } 50 51 52 }
数据库控制: identity(标识列/自动增长) sequence
Student.hbm.xml
DemoDao
1 package com.MavenHibernate.dao; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 6 import com.MavenHibernate.two.entity.Student; 7 import com.MavenHibernate.two.entity.Worker; 8 import com.MavenHibernate.two.util.SessionFactoryUtils; 9 /** 10 * hibernate中的主键生成策略 11 * 1.人工控制 12 * 2.数据库控制 13 * 3.hibernate控制 14 * 4.自定义主键生成策略 15 * @author Administrator 16 * 17 */ 18 public class DemoDao { 19 20 /** 21 * 新增学生 22 * @param stu 23 */ 24 public void addStudent(Student stu) { 25 Session session = SessionFactoryUtils.openSession(); 26 Transaction transaction = session.beginTransaction(); 27 session.save(stu); 28 transaction.commit(); 29 SessionFactoryUtils.closeSession(); 30 } 31 /** 32 * 新增功能 33 * @param worker 34 */ 35 public void addWorker(Worker worker) { 36 Session session = SessionFactoryUtils.openSession(); 37 Transaction transaction = session.beginTransaction(); 38 session.save(worker); 39 transaction.commit(); 40 SessionFactoryUtils.closeSession(); 41 } 42 43 public static void testStudent() { 44 DemoDao dao = new DemoDao(); 45 Student stu = new Student(); 46 stu.setSname("XFQ"); 47 stu.setSid(70); 48 dao.addStudent(stu); 49 } 50 51 58 }
hibernate控制:increment uuid/uuid.hex
是由容器自动生成的一个32位的字符串,.hex代表的是十六进制
32位的字符串,无需赋值,
Worker.hbm.xml
1 package com.MavenHibernate.dao; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 6 import com.MavenHibernate.two.entity.Student; 7 import com.MavenHibernate.two.entity.Worker; 8 import com.MavenHibernate.two.util.SessionFactoryUtils; 9 /** 10 * hibernate中的主键生成策略 11 * 1.人工控制 12 * 2.数据库控制 13 * 3.hibernate控制 14 * 4.自定义主键生成策略 15 * @author Administrator 16 * 17 */ 18 public class DemoDao { 19 20 /** 21 * 新增学生 22 * @param stu 23 */ 24 public void addStudent(Student stu) { 25 Session session = SessionFactoryUtils.openSession(); 26 Transaction transaction = session.beginTransaction(); 27 session.save(stu); 28 transaction.commit(); 29 SessionFactoryUtils.closeSession(); 30 } 31 /** 32 * 新增功能 33 * @param worker 34 */ 35 public void addWorker(Worker worker) { 36 Session session = SessionFactoryUtils.openSession(); 37 Transaction transaction = session.beginTransaction(); 38 session.save(worker); 39 transaction.commit(); 40 SessionFactoryUtils.closeSession(); 41 } 42 43 public static void testStudent() { 44 DemoDao dao = new DemoDao(); 45 Student stu = new Student(); 46 stu.setSname("XFQ"); 47 stu.setSid(70); 48 dao.addStudent(stu); 49 } 50 51 public static void main(String[] args) { 52 DemoDao dao = new DemoDao(); 53 Worker worker = new Worker(); 54 worker.setWname("肖龙飞"); 55 dao.addWorker(worker); 56 } 57 58 }
创建主键生成器类
实现org.hibernate.id.IdentifierGenerator接口即可,并还可以实现org.hibernate.id.Configurable接口来读取一些配置信息
MyTsGenerator
1 package com.MavenHibernate.id; 2 3 import java.io.Serializable; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 import org.hibernate.HibernateException; 8 import org.hibernate.engine.spi.SharedSessionContractImplementor; 9 import org.hibernate.id.IdentifierGenerator; 10 11 public class MyTsGenerator implements IdentifierGenerator { 12 13 @Override 14 public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException { 15 // TODO Auto-generated method stub 16 SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-DD HH:mm:ss"); 17 return "book_order_"+sdf.format(new Date()); 18 } 19 20 }
Worker.hbm.xml
原文:https://www.cnblogs.com/xcn123/p/11296340.html