Hibernate5.2關聯關係之雙向一對多(三)

來源:互聯網
上載者:User

標籤:

                                                       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.儲存

@Testpublic 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關聯關係之雙向一對多(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.