• Hibernate5.2关联关系之双向一对多(三)


                                                           Hibernate之双向一对多(三)

    一.简介

      本篇博文接着上一章的内容接着开展,代码也是在上篇博文的基础上修改。

    二.hbm文件的方式

    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.demo.hibernate.one2many.Customer" table="customer">
            <id name="id" type="int">
                <generator class="sequence">
                    <param name="sequence_name">cus_order_seq</param>
                </generator>
            </id>
            <property name="name" type="string" column="name"></property>
            <property name="phoneNum" type="string" column="phone_number"></property>
            <!-- inverse表示由对方维护这种关联关系 -->
            <set name="orderSet" inverse="true">
                <key column="customer_id"></key>
                <one-to-many class="com.demo.hibernate.one2many.Order"/>
            </set>
        </class>
    </hibernate-mapping>

    Order.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.demo.hibernate.one2many.Order" table="orders">
            <id name="id" type="int">
                <generator class="sequence">
                    <param name="sequence_name">cus_order_seq</param>
                </generator>
            </id>
            <property name="orderId" column="order_id" type="string"></property>
            <property name="createTime" column="create_time" type="timestamp"></property>
            <many-to-one name="customer" class="com.demo.hibernate.one2many.Customer" column="customer_id"></many-to-one>
        </class>
    </hibernate-mapping>

    三.注解的方式

    Customer.java

    @Entity
    @Table(name="customer")
    public class Customer {
        @Id
        @Column(name="id")
        @SequenceGenerator(name="sequenceGenerator", sequenceName="cus_order_seq", allocationSize=1)
        @GeneratedValue(generator="sequenceGenerator", strategy=GenerationType.SEQUENCE)
        private int id;
        
        @Column(name="name")
        private String name;
        
        @Column(name="phone_number")
        private String phoneNum;
        
        @OneToMany(fetch=FetchType.LAZY, mappedBy="customer")
        private Set<Order> orderSet;
        
        //setter and getter
    }

    Order.java

    @Entity
    @Table(name="orders")
    public class Order {
        @Id
        @Column(name="id")
        @SequenceGenerator(name="sequenceGenerator", sequenceName="cus_order_seq", allocationSize=1)
        @GeneratedValue(generator="sequenceGenerator", strategy=GenerationType.SEQUENCE)
        private int id;
        
        @Column(name="order_id")
        private String orderId;
        
        @Column(name="create_time")
        @Type(type="timestamp")
        private Date createTime;
        
        @ManyToOne(fetch=FetchType.LAZY)
        private Customer customer;
        
        //setter and getter
    }

    四.测试代码

    A.保存

    @Test
    public void save(){
        Transaction tx = session.beginTransaction();
        Customer customer = new Customer();
        customer.setName("AAAAA");
        customer.setPhoneNum("334411");
            
        Order order = new Order();
        order.setCreateTime(new Date());
        order.setOrderId("A");
            
        Order order1 = new Order();
        order1.setCreateTime(new Date());
        order1.setOrderId("B");
            
        Set<Order> orderSet = new HashSet<Order>();
        orderSet.add(order);
        orderSet.add(order1);
            
        session.save(customer);
        session.save(order);
        session.save(order1);
        tx.commit();
    }
  • 相关阅读:
    461.mysql数据的备份和恢复
    thinkcmf安装遇到的问题【服务器rewrite】未开启
    navicat for mysql 中文破解版(无需激活码)
    centos7--docker安装gitlab时权限异常
    Cenos7 ++Zabbix监控(同个节点)
    Centos-第一次机子遭受黑客入侵??很鸡冻
    Linux运维--企业sudo权限规划详解 (实测一个堆命令搞定)
    Centos7--sudo的使用和配置
    网络技术--IPv4子网掩码的理解和计算。
    CentOS7--部署JSP网站项目(环境架构jsp+mysql+tomcat)
  • 原文地址:https://www.cnblogs.com/miller-zou/p/5727684.html
Copyright © 2020-2023  润新知