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.