hibernate主键生成策略
Posted xmf3628
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate主键生成策略相关的知识,希望对你有一定的参考价值。
hibernate的主键生成器:
generator元素:表示了一个主键生成器,它用来为持久化类实例生成唯一的标识
新建项目进行测试
1.新建实体
Student.java
public class Student implements Serializable /** * */ private static final long serialVersionUID = 562990602844912346L; 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.java
public class Worker implements Serializable /** * */ private static final long serialVersionUID = -5800181301333923208L; 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 + "]"; public Worker() super();
配置对应实体的映射文件
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.hmc.hibernate.entity.Student" table="t_student_hb"> <id name="sid" type="java.lang.Integer" column="sid" > <generator class="increment"> <!-- 参数名 --> <!-- <param name="sql">hello</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.hmc.hibernate.entity.Worker" table="t_worker_hb"> <id name="wid" type="java.lang.String" column="wid" > <generator class="com.zking.hibernate.id.MyUUID"></generator> </id> <property name="wname" type="java.lang.String" column="wname"></property> </class> </hibernate-mapping>
hibernate核心配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--1.数据库相关的 --> <!--(connection.username|connection.password|connection.url|connection.driver_class|dialect) --> <property name="connection.username">root</property> <property name="connection.password">123</property> <property name="connection.url"> jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8 </property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!--2.调试相关 --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!--3.实体映射配置 --> <mapping resource="com/hmc/hibernate/entity/Student.hbm.xml"/> <mapping resource="com/hmc/hibernate/entity/Worker.hbm.xml"/> </session-factory> </hibernate-configuration>
编写dao层
//新增学生 public void addStudent(Student stu) Session session = SessionFactoryUtils.openSession(); //开启事务 Transaction tran=session.beginTransaction(); //CRUD //保存 session.save(stu); //事务回滚 // tran.rollback(); //提交事务 tran.commit(); SessionFactoryUtils.closeSession(); //新增工人 public void addWorker(Worker worker) Session session = SessionFactoryUtils.openSession(); //开启事务 Transaction tran=session.beginTransaction(); //CRUD //保存 session.save(worker); //事务回滚 // tran.rollback(); //提交事务 tran.commit(); SessionFactoryUtils.closeSession();
编写测试类
AllDaoTest
MyUUID.java
public class MyUUID implements IdentifierGenerator @Override public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException // TODO Auto-generated method stub long datetime=System.currentTimeMillis(); return "W_"+datetime;
以上是关于hibernate主键生成策略的主要内容,如果未能解决你的问题,请参考以下文章