Hibing relationship of Hibernate4.x -- one-to-many, hibernate4.x ing

Source: Internet
Author: User

Hibing relationship of Hibernate4.x -- one-to-many, hibernate4.x ing

In the domain model, the most common relationship between classes is the association relationship.
In UML, associations are oriented.
Take Customer and Order as an example: a user can issue multiple orders, and one Order can only belong to one Customer. The Association from Order to Customer is a many-to-one association, while from Customer to Order is a one-to-many Association.

Unidirectional n-1
One-way n-1 association can be accessed from one end of n to one end of 1.
Domain Model: For multi-to-one association between Order and Customer, a Customer attribute needs to be defined in the Order class. In the Customer class, the set attribute of the Order object does not need to be defined.
Relational Data Model: The CUSTOMER_ID in the ORDER table references the primary key of the CUSTOMER table.

The Code is as follows:

 1 package com.yl.hibernate.entities.n21; 2  3 public class Customer { 4      5     private Integer customerId; 6     private String customerName; 7     public Integer getCustomerId() { 8         return customerId; 9     }10     public void setCustomerId(Integer customerId) {11         this.customerId = customerId;12     }13     public String getCustomerName() {14         return customerName;15     }16     public void setCustomerName(String customerName) {17         this.customerName = customerName;18     }19     20     21 }
 1 package com.yl.hibernate.entities.n21; 2  3 public class Order { 4      5     private Integer orderId; 6     private String orderName; 7      8     private Customer customer; 9 10     public Integer getOrderId() {11         return orderId;12     }13 14     public void setOrderId(Integer orderId) {15         this.orderId = orderId;16     }17 18     public String getOrderName() {19         return orderName;20     }21 22     public void setOrderName(String orderName) {23         this.orderName = orderName;24     }25 26     public Customer getCustomer() {27         return customer;28     }29 30     public void setCustomer(Customer customer) {31         this.customer = customer;32     }33     34     35 }

Customer. hbm. xml

 1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2014-11-26 19:19:40 by Hibernate Tools 3.4.0.CR1 --> 5 

Order. hbm. xml

1 <? Xml version = "1.0"?> 2 <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <! -- Generated 2014-11-26 19:19:40 by Hibernate Tools 3.4.0.CR1 --> 5 

Test class:

1 package com. yl. hibernate. entities. n21; 2 3 4 import org. hibernate. session; 5 import org. hibernate. sessionFactory; 6 import org. hibernate. transaction; 7 import org. hibernate. cfg. configuration; 8 import org. hibernate. service. serviceRegistry; 9 import org. hibernate. service. serviceRegistryBuilder; 10 import org. junit. after; 11 import org. junit. before; 12 import org. junit. test; 13 14 public class Hibe RnateTest {15 16 private SessionFactory sessionFactory; 17 private Session session; 18 private Transaction transaction; 19 20 @ Before 21 public void init () {22 Configuration configuration Configuration = new Configuration (). configure (); 23 ServiceRegistry serviceRegistry = 24 new ServiceRegistryBuilder (). applySettings (configuration. getProperties () 25. buildServiceRegistry (); 26 27 sessionFactory = running Ion. buildSessionFactory (serviceRegistry); 28 29 session = sessionFactory. openSession (); 30 31 transaction = session. beginTransaction (); 32} 33 @ After 34 public void destory () {35 transaction. commit (); 36 37 session. close (); 38 39 sessionFactory. close (); 40} 41 42 @ Test 43 public void testMany2OneSave () {44 Customer customer Customer = new customer (); 45 Customer. setCustomerName ("BB"); 46 47 Order order 1 = new Order (); 48 order1.setOrderName ("ORDER-3"); 49 50 Order order2 = new Order (); 51 order2.setOrderName ("ORDER-4 "); 52 53 // set the association relationship 54 order1.setCustomer (customer); 55 order2.setCustomer (customer); 56 57 // execute the save operation: insert Customer first, then insert Order, 3 INSERT 58 // INSERT the end of 1 first, then INSERT the end of n, only INSERT statement 59/* session. save (customer); 60 61 session. save (order1); 62 session. save (order2); */63 64 // insert Order first and insert Customer. 3 INS ERT, two updates 65 // insert n at the first end, and then insert 1 at the end, there will be more UPDATE statement 66 // when multiple ends are inserted, the foreign key value at one end of 1 cannot be determined. Therefore, after one end of 1 is inserted, it is recommended that one end be inserted first when an additional UPDATE statement 67 is sent, insert 68 sessions at the end of n. save (order1); 69 session. save (order2); 70 71 session. save (customer); 72} 73 74 @ Test 75 public void testMany2OneGet () {76 // 1. if you query an object at multiple ends, only objects at multiple ends are queried by default, but objects at the same end of associated 1 are not queried! 77 Order order = (Order) session. get (Order. class, 2); 78 System. out. println (order. getOrderName (); 79 // 2. send the corresponding SQL statement 80 Customer customer = order. getCustomer (); 81 System. out. println (customer. getCustomerName (); 82 83 // 3. when querying a Customer object and navigating from multiple ends to one end, 84 // if the session is closed, then, 85 // by default, a lazy loading exception will occur. 86 87 // 4. when obtaining the Order object, by default, the associated Customer object is a proxy object! 88} 89 90 @ Test 91 public void testUpdate () {92 Order order = (Order) session. get (Order. class, 2); 93 order. getCustomer (). setCustomerName ("AAA"); 94} 95 96 @ Test 97 public void testDelete () {98 // if there is no cascading relationship, if the object at the end of 1 has n objects being referenced, the 99 Customer customer = (Customer) session object at the end of 1 cannot be deleted directly. get (Customer. class, 1); 100 session. delete (customer); 101} 102 103 104 105}

 

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.