Hibernater学习笔记(四)

作者: Mr_欢先生 | 来源:发表于2017-12-10 22:16 被阅读49次

    今天学习了hibernate的查询,创建的实体类如下

    Customer.java(客户信息)

    public class Customer {
        private Integer c_id;
        private String c_name;
        private String c_source;
        private String c_phone;
        private Set<Salesperson> salespersonSet = new HashSet<Salesperson>();//一个客户可以有多个销售的联系方式
    .....省略get@set方法......
    }
    
    

    Salesperson.java(销售信息)

    public class Salesperson {
        private Integer s_id;
        private String s_name;
        private String s_sex;
        private String s_phone;
        private Customer customer;//一个销售暂且对应一个客户
    .....省略get@set方法.....
    }
    

    1.一对多映射配置

    • Customer.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.study.entity.Customer" table="h_customer">
            <id name="c_id" column="c_id">
                <generator class="native"/>
            </id>
            <property name="c_name" column="c_name"/>
            <property name="c_phone" column="c_phone"/>
            <property name="c_source" column="c_source"/>
            <!--使用set标签,表示所有销售联系人-->
            <set name="salespersonSet">
                <!--双向维护外键,hibernate机制:在1和多的哪一方都配置外键-->
                <key column="sc_id"></key>
                <one-to-many class="com.study.entity.Salesperson"></one-to-many>
            </set>
        </class>
    </hibernate-mapping>
    
    • Salesperson.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.study.entity.Salesperson" table="s_salesperson">
            <id name="s_id" column="s_id">
                <generator class="native"/>
            </id>
            <property name="s_name" column="s_name"/>
            <property name="s_sex" column="s_sex"/>
            <property name="s_phone" column="s_phone"/>
            <!--表示多个销售联系人对应一个客户-->
            <many-to-one name="customer" class="com.study.entity.Customer" column="sc_id"></many-to-one>
        </class>
    </hibernate-mapping>
    
    • 配置完两个文件后,需要将文件引入hibernate核心文件中
            <mapping resource="mapping/Customer.hbm.xml"></mapping>
            <mapping resource="mapping/Salesperson.hbm.xml"></mapping>
    
    • 测试是否配置成功,在HibernateUtils.java中写一个main方法进行执行。


      创建成功
    • 可以查看数据库信息


      数据库

    2.一对多级联操作

    一对多的级联保存

    场景:添加客户,为这个客户添加多个销售联系人

    • 方法一:复杂写法
     @Test
        public void TestOne_to_Many(){
            Session session = null;
            Transaction transaction=null;
            try{
                session = HibernateUtils.getSessionObject();
                transaction = session.beginTransaction();
                Customer customer = new Customer();
                customer.setC_name("李客户");
                customer.setC_phone("13654521363");
                customer.setC_source("西安");
    
                Salesperson salesperson = new Salesperson();
                salesperson.setS_name("张销售");
                salesperson.setS_phone("15632653623");
                salesperson.setS_sex("女");
                Salesperson salesperson2 = new Salesperson();
                salesperson2.setS_name("赵销售");
                salesperson2.setS_phone("17845632154");
                salesperson2.setS_sex("男");
    
    
                //建立客户和销售联系人的关系
                //把销售联系人放在客户set集合里面
                customer.getSalespersonSet().add(salesperson);
                customer.getSalespersonSet().add(salesperson2);
                //把客户放到销售联系人里面
                salesperson.setCustomer(customer);
                salesperson2.setCustomer(customer);
                session.save(customer);
                session.save(salesperson);
                session.save(salesperson2);
                transaction.commit();
            }catch(Exception e){
                e.printStackTrace();
                transaction.rollback();
            }finally {
                session.close();
            }
        }
    
    • 方法二:简化写法
      第一步:修改Customer.hbm.xml文件
    <set name="salespersonSet" cascade="save-update">
                <!--双向维护外键,hibernate机制:在1和多的哪一方都配置外键-->
                <key column="sc_id"></key>
                <one-to-many class="com.study.entity.Salesperson"></one-to-many>
            </set>
    

    第二步:测试代码

     @Test
        public void TestOne_to_Many2(){
            Session session = null;
            Transaction transaction=null;
            try{
                session = HibernateUtils.getSessionObject();
                transaction = session.beginTransaction();
                Customer customer = new Customer();
                customer.setC_name("孙客户");
                customer.setC_phone("111111111");
                customer.setC_source("广东");
    
                Salesperson salesperson = new Salesperson();
                salesperson.setS_name("马销售");
                salesperson.setS_phone("15632653623");
                salesperson.setS_sex("女");
                Salesperson salesperson2 = new Salesperson();
                salesperson2.setS_name("周销售");
                salesperson2.setS_phone("145263253");
                salesperson2.setS_sex("男");
                //把联系人放到客户里面
                customer.getSalespersonSet().add(salesperson);
                customer.getSalespersonSet().add(salesperson2);
                session.save(customer);
                transaction.commit();
            }catch(Exception e){
                e.printStackTrace();
                transaction.rollback();
            }finally {
                session.close();
            }
        }
    
    存入成功
    一对多级联删除

    场景:删除某个客户,并且删除客户关联的所有销售联系人
    第一步:修改Customer.hbm.xml文件

    <set name="salespersonSet" cascade="save-update,delete">
                <!--双向维护外键,hibernate机制:在1和多的哪一方都配置外键-->
                <key column="sc_id"></key>
                <one-to-many class="com.study.entity.Salesperson"></one-to-many>
            </set>
    

    第二步:测试代码

     @Test
        public void TestOne_to_Many_Delete(){
            Session session = null;
            Transaction transaction=null;
            try{
                session = HibernateUtils.getSessionObject();
                transaction = session.beginTransaction();
                //根据ID查询用户
                Customer customer = session.get(Customer.class,3);
                //调用删除
                session.delete(customer);
                transaction.commit();
            }catch(Exception e){
                e.printStackTrace();
                transaction.rollback();
            }finally {
                session.close();
            }
        }
    
    删除成功
    一对多修改操作
     @Test
        public void TestOne_to_Many_Update(){
            Session session = null;
            Transaction transaction=null;
            try{
                session = HibernateUtils.getSessionObject();
                transaction = session.beginTransaction();
                //根据ID查询用户
                Customer customer = session.get(Customer.class,1);
                Salesperson salesperson = session.get(Salesperson.class,6);
                 //虽然在这个事物中并没有调用session的save()方法来保存user对象,但是usr处于持久太,
                //  所以对user对象所做的任何修改都持久化到数据库中  _ 持久态自动更新
                //把联系人放到客户里
                customer.getSalespersonSet().add(salesperson);
                //把客户放到联系人中
                salesperson.setCustomer(customer);
                transaction.commit();
            }catch(Exception e){
                e.printStackTrace();
                transaction.rollback();
            }finally {
                session.close();
            }
        }
    
    修改id=6的销售关联的客户id为1
    • inverse属性:false:不放弃;true:放弃
      在修改时,因为外键修改有两次,因为在刚开始设置的时候,外键有两次维护,一和多的一方都会去维护,因此执行的效率会比较低下。


      两次sc_id修改

    解决方式:让其中的一方不维护外键,让一的一方放弃外键维护,设置inverse属性

     <set name="salespersonSet" cascade="save-update,delete" inverse="true">
                <!--双向维护外键,hibernate机制:在1和多的哪一方都配置外键-->
                <key column="sc_id"></key>
                <one-to-many class="com.study.entity.Salesperson"></one-to-many>
            </set>
    
    修改一次sc_id
    上一篇:Hibernater学习笔记(三)
    当前文集 :Hibernate框架学习
    本笔记hibernate案例:github地址

    相关文章

      网友评论

        本文标题:Hibernater学习笔记(四)

        本文链接:https://www.haomeiwen.com/subject/nwjeixtx.html