zoukankan      html  css  js  c++  java
  • hibernate框架学习笔记4:主键生成策略、对象状态

    创建一个实体类:

    package domain;
    
    public class Customer {
        
        private Long cust_id;
        private String cust_name;
        private String cust_source;
        private String cust_industry;
        private String cust_level;
        private String cust_linkman;
        private String cust_phone;
        private String cust_mobile;
        public Long getCust_id() {
            return cust_id;
        }
        public void setCust_id(Long cust_id) {
            this.cust_id = cust_id;
        }
        public String getCust_name() {
            return cust_name;
        }
        public void setCust_name(String cust_name) {
            this.cust_name = cust_name;
        }
        public String getCust_source() {
            return cust_source;
        }
        public void setCust_source(String cust_source) {
            this.cust_source = cust_source;
        }
        public String getCust_industry() {
            return cust_industry;
        }
        public void setCust_industry(String cust_industry) {
            this.cust_industry = cust_industry;
        }
        public String getCust_level() {
            return cust_level;
        }
        public void setCust_level(String cust_level) {
            this.cust_level = cust_level;
        }
        public String getCust_linkman() {
            return cust_linkman;
        }
        public void setCust_linkman(String cust_linkman) {
            this.cust_linkman = cust_linkman;
        }
        public String getCust_phone() {
            return cust_phone;
        }
        public void setCust_phone(String cust_phone) {
            this.cust_phone = cust_phone;
        }
        public String getCust_mobile() {
            return cust_mobile;
        }
        public void setCust_mobile(String cust_mobile) {
            this.cust_mobile = cust_mobile;
        }
        @Override
        public String toString() {
            return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
        }
    }
    View Code

    ORM元数据配置文件:

    <?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="domain.Customer" table="cst_customer" >
            <id name="cust_id"  >
                <!-- generator:主键生成策略 -->
                <generator class="native"></generator>
            </id>
            <property name="cust_name" column="cust_name" ></property>
            <property name="cust_source" column="cust_source" ></property>
            <property name="cust_industry" column="cust_industry" ></property>
            <property name="cust_level" column="cust_level" ></property>
            <property name="cust_linkman" column="cust_linkman" ></property>
            <property name="cust_phone" column="cust_phone" ></property>
            <property name="cust_mobile" column="cust_mobile" ></property>
        </class>
    </hibernate-mapping>

    这里的主键生成策略是native

    详细:

    主键生成策略.就是每条记录录入时,主键的生成规则.(7个)


    identity : 主键自增。由数据库来维护主键值。录入时不需要指定主键。


    sequence: Oracle中的主键生成策略,这里使用的是MySQL,暂时不介绍


    increment(不使用,存在效率低和线程安全问题): 主键自增.由hibernate来维护.每次插入前会先查询表中id最大值.+1作为新主键值.


    hilo(不使用,了解即可):高低位算法,主键自增。由hibernate来维护,开发时不使用。


    native:hilo+sequence+identity 自动三选一策略(检测当前数据库,使用相应的策略)


    uuid: 产生理论上永远不会重复的随机字符串作为主键. 主键类型必须为string 类型.


    assigned:自然主键生成策略。hibernate不会管理主键值,由开发人员自己录入。

    hibernate对象分为三种状态:

    瞬时状态:没有id,没有session关联

    持久化状态:有id,与session关联

    游离状态托管状态):有id,没有与session关联

    package state;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.junit.Test;
    
    import domain.Customer;
    import utils.HibernateUtils;
    
    //测试对象的三种状态
    public class Demo {
    
        @Test
        //查看三种状态
        public void fun1(){
            //1 获得session
            Session session = HibernateUtils.openSession();
            //2 控制事务
            Transaction tx = session.beginTransaction();
            //3执行操作
            Customer c = new Customer(); // 没有id, 没有与session关联 => 瞬时状态
            
            c.setCust_name("联想"); // 瞬时状态
            
            session.save(c); // 持久化状态, 有id,有关联
            
            //4提交事务.关闭资源
            tx.commit();
            session.close();// 游离|托管 状态, 有id , 没有关联
            
            
        }
        
        @Test
        //三种状态特点
        //save方法: 其实不能理解成保存.理解成将瞬时状态转换成持久状态的方法
        //主键自增 : 执行save方法时,为了将对象转换为持久化状态.必须生成id值.所以需要执行insert语句生成.
        //如果主键生成策略是increment: 执行save方法,为了生成id.会执行查询id最大值的sql语句(select max(id) from 表).
        public void fun2(){
            //1 获得session
            Session session = HibernateUtils.openSession();
            //2 控制事务
            Transaction tx = session.beginTransaction();
            //3执行操作
            Customer c = new Customer(); // 没有id, 没有与session关联 => 瞬时状态
            
            c.setCust_name("联想"); // 瞬时状态
            
            session.save(c); // 持久化状态, 有id,有关联
            
            //4提交事务.关闭资源
            tx.commit();
            session.close();// 游离|托管 状态, 有id , 没有关联
            
            
        }
        
        @Test
        //三种状态特点
        // 持久化状态特点: 持久化状态对象的任何变化都会自动同步到数据库中.
        public void fun3(){
            //1 获得session
            Session session = HibernateUtils.openSession();
            //2 控制事务
            Transaction tx = session.beginTransaction();
            //3执行操作
            
            Customer c = session.get(Customer.class, 1l);//持久化状态对象
            
            c.setCust_name("微软公司");
            
            //4提交事务.关闭资源
            tx.commit();
            session.close();// 游离|托管 状态, 有id , 没有关联
            
            
        }
    }

    接下来从状态角度分析hibernate的增删改查:

  • 相关阅读:
    一文读懂高性能网络编程中的I/O模型
    QQ的成功,远没有你想象的那么顺利和轻松
    解密“达达-京东到家”的订单即时派发技术原理和实践
    以网游服务端的网络接入层设计为例,理解实时通信的技术挑战
    老罗最新发布了“子弹短信”这款IM,主打熟人社交能否对标微信?
    最火移动端跨平台方案盘点:React Native、weex、Flutter
    ogre3D学习基础11 -- 交换两个场景管理器
    STL学习笔记2--list
    STL学习笔记1--vector
    设计模式 --- 学习总结
  • 原文地址:https://www.cnblogs.com/xuyiqing/p/8449059.html
Copyright © 2011-2022 走看看