hibernate--a one-to-many/many pair

Source: Internet
Author: User

One-to-many associations (multi-pair):
    One-to-many association mappings:        Add a foreign key to one end of a multi-point, and it maintains a relationship that points    to many-to-one correlation mappings: one end of a        foreign key points to one end, it maintains the relationship is more point to a
Add in config file: use <set><key></key><one-to-many></one-to-many></set> on one end <key> The foreign key field specified must be the same as the <many-to-one> specified foreign key field at the multi-end use <many-to-one>
Relationship between the Customer table and the order table in the database:

To create an entity class:
 public  class   Customer { private   Integer ID;     private   String name;     private   Character gender;     private   Integer age;     private   String level;         //one-to-many private set<order> orders = new hashset<order> ();  public   Customer () { super   (); } 

getters and setters}
 Public class Order {    private  Integer ID;     Private String OrderNo;     Private String productName;         //Many to a    private customer customer    ;  Public Order () {        Super();         // TODO auto-generated Constructor stub     }

  getters and Setters}
To create a mapping file:
 class  name= " Com.roxy.hibernate.pojo.Customer "table=" T_customer "> <id name=" id "column=" c_id "> <gene Rator class  = "native" ></generator> </id> <prope Rty name= "name" column= "C_name" Not-null  = "true" ></property> <prope Rty name= "Gender" column= "C_gender" length= "1" ></property> <property name= "age" column= "C_age" ></  property> <property name= "level" column= "C_level" length= "></property>  <set name=" Orders "> <key column=" customer_id "></key> <one-to- Many class= "Com.roxy.hibernate.pojo.Order"/> </set>  </class>
package = "Com.roxy.hibernate.pojo" >    < Mapping!--classes and tables-    <class name= "Order" table = "T_order" >        <id name= "id" column= "id" >            class= "Native" ></generator>        </ Id>        <!--other properties map--        <property name= "OrderNo" column= "OrderNo" length= "></property>"        <property name= "ProductName" column= "Product_Name" length= "></property>        <!-- Many-to-one and        <many-to-one name= "Customer" class= "Customer" column= "customer_id"        /> </ Class>
To create a configuration file:
        <!--mapping file--        <mapping resource= "Com/roxy/hibernate/pojo/customer.hbm.xml"/>        <mapping Resource= "Com/roxy/hibernate/pojo/order.hbm.xml"/>
To view and parse the SQL statement:
hibernate:alter table t_order Add constraint fkesy3n2gc3fa0s3trrk3tvyv9a foreign key (Customer_i d) References T_customer (c_id)

-----Hibernate first add a foreign key association for T_order and T_customer
Hibernate:insert into T_customer (C_name, C_gender, C_age, C_level) VALUES (?, ?, ?, ?)

----- inserting data into the T_customer (Session.save (Cust);)
Hibernate:insert into T_order (OrderNo, product_name, customer_id) VALUES (?, ?, ?)

----- inserting data into the T_order (Session.save (O1);)
Hibernate:insert into T_order (OrderNo, product_name, customer_id) VALUES (?, ?, ?)
Inserting data into the T_order (Session.save (O2);)
Hibernate: update t_order set customer_id=? where id=?
----- maintains a relationship between two tables, hibernate updates T_customer data
Hibernate: update t_order set customer_id=? where id=?
-----Maintain the relationship between two tables, hibernate updates the T_customer data

hibernate--a one-to-many/many pair

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.