hibernate主键生成策略
Posted omji0030
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate主键生成策略相关的知识,希望对你有一定的参考价值。
创建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主键生成策略的主要内容,如果未能解决你的问题,请参考以下文章