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

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

时间:2016-08-02 01:22:49      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

                                                       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();
}

 

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

标签:

原文地址:http://www.cnblogs.com/miller-zou/p/5727684.html

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