码迷,mamicode.com
首页 > Web开发 > 详细

hibernate主键生成策略

时间:2019-08-03 10:36:30      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:type   shared   mat   enc   ring   private   tee   string   code   

 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&amp;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主键生成策略

标签:type   shared   mat   enc   ring   private   tee   string   code   

原文地址:https://www.cnblogs.com/xmf3628/p/11293755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!