Hibernate5.2關聯關係之單向多對一(二)

來源:互聯網
上載者:User

標籤:

                                                     Hibernate5.2之單向一對多(二)

一. 簡介

  在本篇博文中筆者會在上一篇部落格的代碼基礎上進行修改,本篇文章將介紹單向的一對多。

二. 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>            </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")    private int id;        @Column(name="name")    private String name;        @Column(name="phone_number")    private String phoneNum;        //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")    private int id;        @Column(name="order_id")    private String orderId;        @Column(name="create_time")    @Type(type="timestamp")    private Date createTime;        @ManyToOne(fetch=FetchType.LAZY)  //註解的方式需要指定消極式載入策略    @JoinColumn(name="customer_id")    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.setCustomer(customer);            Order order1 = new Order();    order1.setCreateTime(new Date());    order1.setOrderId("B");    order1.setCustomer(customer);            session.save(customer);    session.save(order);    session.save(order1);    tx.commit();
}

 B.get方式

@Testpublic void get(){    Order order = session.get(Order.class, 17);    System.out.println("查詢Customer的SQL已經發送");    System.out.println(order.getOrderId() + "::" + order.getCreateTime());    Customer customer = order.getCustomer();    System.out.println("查詢Customer的SQL還未發送,因為消極式載入,只有當在使用Customer的時候才會發送SQL");    System.out.println(customer.getName() + "::" + customer.getPhoneNum());}

C.load方式

@Testpublic void load(){    Order order = session.load(Order.class, 17);    System.out.println("查詢Order的SQL還未發送,只有當使用的時候才會放鬆SQL");    System.out.println(order.getOrderId() + "::" + order.getCreateTime());    Customer customer = order.getCustomer();    System.out.println("查詢Order的Customer還未發送,因為消極式載入,只有當在使用Customer的時候才會發送SQL");    System.out.println(customer.getName() + "::" + customer.getPhoneNum());}

D.delete(刪除)

@Testpublic void delete(){    Order order = new Order();    order.setId(17);    Transaction tx = session.beginTransaction();    session.delete(order);    tx.commit();}

E.update(更新)

@Testpublic void update(){    Order order = new Order();    order.setId(18);      order.setCreateTime(new Date());    order.setOrderId("N");            Transaction tx = session.beginTransaction();    session.update(order);    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.