hibernate主键生成策略
Posted hyfl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate主键生成策略相关的知识,希望对你有一定的参考价值。
hibernate的主键生成器:
generator元素:表示了一个主键生成器,它用来为持久化类实例生成唯一的标识 。
首先我们引入一个工具类SessionFactoryUtils
1 package com.liuwenwu.two.util; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6 /** 7 * 这个类是在学习hibernate 的过程中所用(整合SSH框架之前用) 8 * 作用: 9 * 可以用来检测所写的映射文件是否正确 90% 10 * @author ASUS 11 * 12 */ 13 public class SessionFactoryUtils 14 private static SessionFactory sessionFactory; 15 // 存放当前会话 16 private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); 17 static 18 Configuration cfg = new Configuration(); 19 Configuration configure = cfg.configure("/hibernate.cfg.xml"); 20 sessionFactory = configure.buildSessionFactory(); 21 22 23 public static Session openSession() 24 //获取本地线程中的会话 25 Session session = threadLocal.get(); 26 if (null == session) 27 session = sessionFactory.openSession(); 28 threadLocal.set(session); 29 30 return session; 31 32 33 public static void closeSession() 34 Session session = threadLocal.get(); 35 if (null != session) 36 if (session.isOpen()) 37 session.close(); 38 39 threadLocal.set(null); 40 41 42 43 public static void main(String[] args) 44 Session session = openSession(); 45 System.out.println(session.isConnected()); 46 closeSession(); 47 48
这个类可以用来检测所写的映射文件是否出错 如果有错则如下图:
要实现hibernate的主键生成 我们先创两个实体类
Student:
1 package com.liuwenwu.two.entity; 2 3 import java.io.Serializable; 4 5 public class Student implements Serializable 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 374274097567290896L; 11 12 private int sid; 13 private String sname; 14 public int getSid() 15 return sid; 16 17 public void setSid(int sid) 18 this.sid = sid; 19 20 public String getSname() 21 return sname; 22 23 public void setSname(String sname) 24 this.sname = sname; 25 26 @Override 27 public String toString() 28 return "Student [sid=" + sid + ", sname=" + sname + "]"; 29 30
Worker:
1 package com.liuwenwu.two.entity; 2 3 public class Worker 4 private String wid; 5 private String wname; 6 public String getWid() 7 return wid; 8 9 public void setWid(String wid) 10 this.wid = wid; 11 12 public String getWname() 13 return wname; 14 15 public void setWname(String wname) 16 this.wname = wname; 17 18 @Override 19 public String toString() 20 return "Worker [wid=" + wid + ", wname=" + wname + "]"; 21 22
然后对实体类进行配置:
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.liuwenwu.two.entity.Student" table="t_hibernate_student"> 7 <id name="sid" type="java.lang.Integer" column="sid"> 8 <!-- 程序员自己控制 --> 9 <!-- <generator class="assigned" /> --> 10 <!-- hibernate控制 --> 11 <generator class="increment" /> 12 <!-- 数据库控制 --> 13 <!-- <generator class="identity" /> --> 14 <!-- 数据库控制 --> 15 <!-- <generator class="sequence" /> --> 16 <!-- <generator class="sequence" > <param name="sequence_name">aaa</param> 17 </generator> --> 18 <!-- <generator class="com.javaxl.two.id.Myts" /> --> 19 </id> 20 <property name="sname" type="java.lang.String" column="sname"> 21 </property> 22 </class> 23 </hibernate-mapping>
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.liuwenwu.two.entity.Worker" table="t_hibernate_worker"> 7 <id name="wid" type="java.lang.String" column="wid"> 8 <!-- 程序员自己控制 --> 9 <!-- <generator class="assigned" /> --> 10 <!-- 由容器自动生成的一个32位的字符串 --> 11 <!-- <generator class="uuid" /> --> 12 <!-- <generator class="sequence" > <param name="sequence_name">aaa</param> 13 </generator> --> 14 <!-- 自定义主键生成器 --> 15 <generator class="com.liuwenwu.two.id.Myts" /> 16 </id> 17 18 <property name="wname" type="java.lang.String" column="wname"> 19 </property> 20 </class> 21 </hibernate-mapping>
主配置文件 hibernate.cfg.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 <!-- 1. 数据库相关 --> 9 <property name="connection.username">root</property> 10 <property name="connection.password">123</property> 11 <property name="connection.url">jdbc:mysql://localhost:3306/t224?useUnicode=true&characterEncoding=UTF-8 12 </property> 13 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 14 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 15 <!-- <property name="dialect">org.hibernate.dialect.OracleDialect</property> --> 16 17 <!-- 配置本地事务(No CurrentSessionContext configured!) --> 18 <property name="hibernate.current_session_context_class">thread</property> 19 20 <!-- 2. 调试相关 --> 21 <property name="show_sql">true</property> 22 <property name="format_sql">true</property> 23 24 <!-- 3. 添加实体映射文件 --> 25 <mapping resource="com/liuwenwu/one/entity/User.hbm.xml" /> 26 <!--主键生成策略 --> 27 <mapping resource="com/liuwenwu/two/entity/Student.hbm.xml" /> 28 <mapping resource="com/liuwenwu/two/entity/Worker.hbm.xml" /> 29 </session-factory> 30 </hibernate-configuration>
编写dao方法来进行测试
DemoDao:
1 package com.liuwenwu.two.dao; 2 3 import java.io.Serializable; 4 5 import org.hibernate.Session; 6 import org.hibernate.Transaction; 7 8 import com.liuwenwu.two.entity.Student; 9 import com.liuwenwu.two.entity.Worker; 10 import com.liuwenwu.two.util.SessionFactoryUtils; 11 12 public class DemoDao 13 14 /** 15 * 添加学生 16 * @param stu 17 * @return 18 */ 19 public Serializable addStudent(Student stu) 20 Session session = SessionFactoryUtils.openSession(); 21 Transaction transaction = session.beginTransaction(); 22 Serializable save = session.save(stu); 23 transaction.commit(); 24 session.close(); 25 return save; 26 27 28 /** 29 * 添加工人 30 * @param wor 31 * @return 32 */ 33 public Serializable addWorker(Worker wor) 34 Session session = SessionFactoryUtils.openSession(); 35 Transaction transaction = session.beginTransaction(); 36 Serializable save = session.save(wor); 37 transaction.commit(); 38 session.close(); 39 return save; 40 41 42 public static void testStudent(String[] args) 43 DemoDao demoDao=new DemoDao(); 44 Student stu=new Student(); 45 // stu.setSid(8); 46 stu.setSname("毛三"); 47 System.out.println(demoDao.addStudent(stu));; 48 49 50 public static void main(String[] args) 51 DemoDao demoDao=new DemoDao(); 52 Worker worker=new Worker(); 53 worker.setWname("毛三"); 54 System.out.println(demoDao.addWorker(worker));; 55 56
几种不同的主键生成方式:
1.程序员自己控制:assigned 完全由程序员自己控制,不管数据库表怎么建,必须由程序员自己提供ID
2.数据库控制: identity (根据数据库设置的标识列来实现自定增长自动增长)
2.1sequence : 使用hibernate中的默认序列 所有表共用一个序列 会出现跳号现象 不建议使用
使用时可以指定序列 <param name="sequence_name">aaa</param>
无缝跨平台:在主配置文件中修改数据库方言 可实现跨平台生成主键
Mysql语句:
Oracle语句:
3 .hibernate控制:increment 跟数据库没关系 ID必须是数字的
4 .uuid/uuid.hex (是由容器自动生成的一个32位的字符串,.hex代表的是十六进制)
32位的字符串,无需赋值
5.自定义主键生成器:创建主键生成器类
实现org.hibernate.id.IdentifierGenerator接口即可,并还可以实现org.hibernate.id.Configurable接口来读取一些配置信息
自己定义主键规则 Myts:
1 package com.liuwenwu.two.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 Myts implements IdentifierGenerator 12 13 @Override 14 public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException 15 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 16 return "zking_shop_bood"+sdf.format(new Date()); 17 18
效果:自己定义前缀+主键生成时间
以上是关于hibernate主键生成策略的主要内容,如果未能解决你的问题,请参考以下文章