Hibernate from Getting started to mastering (vi) One-to-one Bidirectional Association mapping

Source: Internet
Author: User

In the last blog post hibernate from Getting started to mastering (v.) one to one one-way association mapping we explained one to one one-way association mapping, this time we continue to explain the corresponding one-to-one two-way association mapping.

One-to-one Two-way Association

Unlike a one-to-one one-way association mapping, in a one-to-one two-way association, not only is there a reference to another object (Idcard) in one object (person), but also a reference to that object (person) in another object (Idcard), so that the object can be loaded ( person), or you can find the object (person) when another object (Idcard) is loaded (Idcard).

The class structure diagram looks like this:

The specific code looks like this:

public class Person {private int id;              
    private String name;  
          
    Private Idcard Idcard;  
    public int getId () {return id;  
    The public void setId (int id) {this.id = ID;  
    Public String GetName () {return name;  
    public void SetName (String name) {this.name = name;  
    Public Idcard Getidcard () {return idcard;  
    public void Setidcard (Idcard idcard) {this.idcard = Idcard;       
    } public class Idcard {private int id;        
    Private String Cardno;  
          
    private person person;  
    public int getId () {return id;  
    The public void setId (int id) {this.id = ID;  
    Public String Getcardno () {return cardno;  
    } public void Setcardno (String cardno) {this.cardno = Cardno;  Public Person Getperson () {
        return person;  
    public void Setperson {This.person = person; }  
}

As with one-to-one one-way association mappings, a one-to-one, two-way association mapping is also associated with different policies that generate different table structures and can be divided into primary key associations and unique foreign key associations. But the same as a one-to-one one-way association mapping is the resulting table structure, that is, a one-to-one bidirectional association map, compared to a one-to-one one-way association, only changes the loading of one-to-one correlation mappings and does not change storage.

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.