Hibernate ing (4)-one-to-many

Source: Internet
Author: User

Hibernate ing (4)-one-to-many

 

One-to-multiple and multiple-to-one differences:

The ing principle is consistent. A foreign key is added to multiple ends, pointing to one end.

The difference is that the maintained relationship is different:

The one-to-one relationship is maintained by pointing multiple points to one. With this relationship, you can load multiple links.

The one-to-multiple maintenance relationship is: one-to-many relationship. With this relationship, load one

You can load more

 

Unidirectional

Applicable situations: A class has multiple students (the class depends on the students, and the class must have students first to maintain the relationship)

Relationship diagram:

 

Note: A foreign key is added to multiple ends, pointing to one end.

 

Entity: Student: public class Student {private int id; private String name; public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name ;}} Class: public class Classes {private int id; private String name; private Set students; public int getId () {return id ;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public Set getStudents () {return students;} public void setStudents (Set students) {this. students = students;} ing file: Student:
     
  
        
    
   
       
   
  
 Class:
      
  
   
    
   
   
            
         
      
     
         
   
            
 Client: public void testSave2 () {Session session = null; try {session = HibernateUtils. getSession (); session. beginTransaction (); Student student1 = new Student (); student1.setName ("James"); session. save (student1); Student student2 = new Student (); student2.setName (""); session. save (student2); Classes classes = new Classes (); classes. setName ("power node"); Set students = new HashSet (); students. add (student1); students. add (student2); classes. setStudents (students); // The data can be saved successfully. // additional update statements are issued to maintain the link session. save (classes); session. getTransaction (). commit ();} catch (Exception e) {e. printStackTrace (); session. getTransaction (). rollback ();} finally {HibernateUtils. closeSession (session );}}

 


 

Result:

Bidirectional

Applicability:

Both ENDS know the relationship and solve the one-to-many one-way Association defect.

Relationship diagram:

 

Entity: student: public class Student {private int id; private String name; private Classes classes; public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public Classes getClasses () {return classes;} public void setClasses (Classes classes) {this. classes = classes ;}} class: public class Classes {private int id; private String name; private Set students; public int getId () {return id ;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public Set getStudents () {return students;} public void setStudents (Set students) {this. students = students;} ing file: student:
       
  
   
    
   
   
           
    
   
      
 
class:
        
 
       
    
  
      
   
     
     
   
            
        
     
    
   
  
 

 

 

Client:

 

Scenario 1: public void testSave2 () {Session session = null; try {session = HibernateUtils. getSession (); session. beginTransaction (); Student student1 = new Student (); student1.setName ("James"); session. save (student1); Student student2 = new Student (); student2.setName (""); session. save (student2); Classes classes = new Classes (); classes. setName ("power node"); Set students = new HashSet (); students. add (student1); students. add (student2); classes. setStudents (students); // The data can be saved successfully. // additional update statements are issued to maintain the link session. save (classes); session. getTransaction (). commit ();} catch (Exception e) {e. printStackTrace (); session. getTransaction (). rollback (); finally {HibernateUtils. closeSession (session );}}
Note: Data can be saved, but redundant update statements are issued to maintain the relationship.

 

 

Case 2:

 

Public void testSave3 () {Session session = null; try {session = HibernateUtils. getSession (); session. beginTransaction (); Classes classes = new Classes (); classes. setName ("power node"); session. save (classes); Student student1 = new Student (); student1.setName ("James"); student1.setClasses (classes); session. save (student1); Student student2 = new Student (); student2.setName (""); student2.setClasses (classes); session. save (student2); session. getTransaction (). commit (); catch (Exception e) {e. printStackTrace (); session. getTransaction (). rollback ();} finally {HibernateUtils. closeSession (session );}}

 

Result:

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.