SSH-Hibernate (II)-link ing (I)

Source: Internet
Author: User

SSH-Hibernate (II)-link ing (I)
The previous blog mentioned that ORM is a persistent solution. Its idea is to map the records of tables in a relational database to JAVA objects. Simply put, the JAVA object and the record in the relational database have established a fixed correspondence relationship, and the object is a record! The most direct result of this operation is that developers can operate relational databases with an object-oriented approach.
Objects correspond to records in data tables, and there are one-to-one, one-to-many, and many-to-many relationships between records and records in relational databases. ORM ING also needs to store the relationships between these records. It is mainly implemented through the relational relationships in object-oriented systems. Inheritance relationships are a special form of reflecting the relationships between records. Let's start with the one-to-one relationship ing.
One-to-one association ing primary key Association: two tables share the primary key, that is, primary key synchronization.Person class

Public class Person {private String id; private String name; private IdCard idCard; // the set and get methods are omitted}
IdCard class
Public class IdCard {private String id; private String securityBureau; private Person person; // the set and get methods are omitted}
 
       
            
                                
                 
              
             
                     
                     
         
    
 
IdCard. hbm. xml configuration:
       
            
                        
    
     Person
              
             
             
             
         
    
   

Because primary key Association is used, the primary key values of the two tables must be synchronized. That is to say, we only need to set a primary key generation policy for one table, and the primary key of the other table can be shared with it. However, the primary key id in the idCards table does not refer to the foreign key of the persons Table id.

Although there is a mutual reference relationship between the Pserson class and the IdCard class, there is no reference relationship between the database persons and idCards tables, but when we save the User object, if the idCard attribute value in User is not NULL, Hibernate uses. hbm. the primary key value generated by the primary key generator used in xml serves as the primary key value for inserting records into the users and idCards tables, at this time, the primary key value of the associated record is synchronized (the output SQL statement shows that this is handled by Hibernate, and the database does not know)

Unique foreign key Association: it is a limited one-to-many Association. That is, you can set a unique one-to-many Association at one end. The special relationship between the unique foreign key Association and one-to-many Association will be placed in the next blog!
SummaryThe nature of ing is to reflect the relationship between records in the database through the relationship between objects in Java, this relationship ing involves the use of primary keys and Foreign keys in the database to ensure one-to-one relationship. Because the uniqueness of records in the database is related to the primary key.

  
  


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.