One-way multi-pair One (ii) of Hibernate5.2 relation

Source: Internet
Author: User

Hibernate5.2 One-way pair-More (ii)

I. INTRODUCTION

  In this blog post I will be in the previous blog code based on the changes, this article will introduce one-way to many.

Two. How HBM files are

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>    <classname= "Com.demo.hibernate.one2many.Customer"Table= "Customer">        <IDname= "id"type= "int">            <Generatorclass= "sequence">                <paramname= "Sequence_name">Cus_order_seq</param>            </Generator>        </ID>        < Propertyname= "Name"type= "string"column= "Name"></ Property>        < Propertyname= "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>    <classname= "Com.demo.hibernate.one2many.Order"Table= "Orders">        <IDname= "id"type= "int">            <Generatorclass= "sequence">                <paramname= "Sequence_name">Cus_order_seq</param>            </Generator>        </ID>        < Propertyname= "OrderId"column= "order_id"type= "string"></ Property>        < Propertyname= "Createtime"column= "Create_time"type= "Timestamp"></ Property>        <Many-to-onename= "Customer"class= "Com.demo.hibernate.one2many.Customer"column= "customer_id"></Many-to-one>    </class></hibernate-mapping>

Three. How to Annotate

Customer.java

@Entity @table (name= "Customer") Public classCustomer {@Id @Column (name= "id") @SequenceGenerator (name= "Sequencegenerator", sequencename= "Cus_order_seq", allocationsize=1)@GeneratedValue (Generator= "Sequencegenerator")    Private intID; @Column (Name= "Name")    PrivateString name; @Column (Name= "Phone_number")    PrivateString Phonenum; //Setter and Getter}

Order.java

@Entity @table (name= "Orders") Public classOrder {@Id @Column (name= "id") @SequenceGenerator (name= "Sequencegenerator", sequencename= "Cus_order_seq", allocationsize=1)@GeneratedValue (Generator= "Sequencegenerator")    Private intID; @Column (Name= "order_id")    PrivateString orderId; @Column (Name= "Create_time") @Type (Type= "Timestamp")    PrivateDate Createtime; @ManyToOne (Fetch=fetchtype.lazy)//annotations need to specify a deferred load policy @JoinColumn (name= "customer_id")    Privatecustomer customer; //Setter and Getter}

Four. Test code

A. Save

@Test Public voidSave () {Transaction tx=session.begintransaction (); Customer Customer=NewCustomer (); Customer.setname ("AAAAA"); Customer.setphonenum ("334411"); Order Order=NewOrder (); Order.setcreatetime (NewDate ()); Order.setorderid (A);            Order.setcustomer (customer); Order Order1=NewOrder (); Order1.setcreatetime (NewDate ()); Order1.setorderid (B);            Order1.setcustomer (customer);    Session.save (customer);    Session.save (order);    Session.save (Order1); Tx.commit ();
}

B.get Way

@Test  Public void get () {    = Session.get (Order.  Class, +);    SYSTEM.OUT.PRINTLN ("Query customer's SQL has been sent")    ; + "::" + order.getcreatetime ());     = Order.getcustomer ();    SYSTEM.OUT.PRINTLN ("Query for customer's SQL has not been sent, because lazy loading, only when the use of the customer when the SQL will be sent");     + "::" + Customer.getphonenum ());}

C.load Way

@Test  Public void load () {    = Session.load (Order.  Class, +);    SYSTEM.OUT.PRINTLN ("Query order SQL has not been sent, only when the use of the time will be relaxed SQL");     + "::" + order.getcreatetime ());     = Order.getcustomer ();    System.out.println ("The customer of the query order has not been sent, because the delay is loaded, only the SQL will be sent when the customer is used");     + "::" + Customer.getphonenum ());}

D.delete (delete)

@Test  Public void Delete () {    new  Order ();    Order.setid (+);     = session.begintransaction ();    Session.delete (order);    Tx.commit ();}

E.update (update)

@Test  Public void Update () {    new  Order ();    Order.setid ();      Order.setcreatetime (new  Date ());    Order.setorderid ("N");             = session.begintransaction ();    Session.update (order);    Tx.commit ();}

One-way multi-pair One (ii) of Hibernate5.2 relation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.