Previous blog post "SSH Advanced path" hibernate mapping-one-to-one unidirectional correlation mapping (v), we introduced one-to-two unidirectional association mappings, one-way refers to the only people (person) on this side to load the identity card (Idcard), but in turn, can not be added from the identity card side information. :
The key reason is that the object model has directionality:
unidirectional: One end can only load the other end and cannot be reversed.
bidirectional : Both ends can be loaded at the other end.
The question is: how do we want to add a manned (person) from the ID card (idcard)?
Let's start with a one-to-one bidirectional correlation mapping.
Mapping principle
bidirectional correlation Mapping and one-way associative mappings the principle is the same, the bidirectional correlation mapping does not affect the storage, only affects the loading . Therefore, the relational model of bidirectional correlation mapping and unidirectional association mapping is the same, that is, the table structure of the database is the same, but the Idcard entity classes and Configuration files (IdCard.hbm.xml) have changed a little.
Object model
It can be seen from:
1, a person only one ID card, the only one identity card number, the object is a one-on relationship;
2. Both ends hold a reference to the object, however, the relationship maintenance of two objects is determined by the person side ( because the relationship can only be maintained by one side of the primary key, otherwise the relationship is chaotic).
Based on the object model above, we can see that the person side does not change, but to add a reference to the person on the Idcard side, such as the person and Idcard entity classes, as follows.
Person
Package Com.liang.hibernate;public class Person {private int id;private String name;private idcard idcard;public idcard ge Tidcard () {return idcard;} public void Setidcard (Idcard idcard) {this.idcard = Idcard;} 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;}}
Idcard
Package Com.liang.hibernate;public class Idcard {private int id;private String cardno;private person person;public person Getperson () {return person;} public void Setperson (person person) {This.person = person;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String Getcardno () {return cardno;} public void Setcardno (String cardno) {this.cardno = Cardno;}}
Whether they are one-way or two-way associative mappings, they belong to each other, but their primary Key's generation strategy is different, which is the primary key association map and the unique Foreign Key association mapping.
Since they all belong to a one-to-one correlation mapping, hibernate encapsulates a two-way association map, with the primary key-key mapping and the load strategy of the unique foreign-key association map, all using a one-to-<one-to-one name= "" ></one-to-one Only the property settings are inconsistent, so let's look at the Idcard configuration file separately.
Classification:
Primary Key Association Mappings
Similar to a one-way associative mapping, a primary key association is associated with a primary key, and the value of the associated primary key is the same. Let's take a look at the mapping file:
IdCard.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Person.hbm.xml, same as one-way primary Key Association mappings
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
The resulting table structure
Unique Foreign Key Association mappingsA one-to-one, foreign-key association mapping of two-way association mappings is similar to the Foreign Key Association mapping of a single-direction association map, where a unique foreign key exists on the point-to-side (person), which is associated with the pointed end (Idcard) and the value of the associated primary key is the same. Let's take a look at the mapping file:
IdCard.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Person.hbm.xml, same as one-way unique foreign Key association mappings
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
The resulting table structure
ContrastThe difference between one-way and two-way association mappings is the difference between an object model and a relational model.
Object model: Directional. Whether it is unidirectional or bidirectional is determined by the object model, which is the configuration file.
Relational model: No directionality or bidirectional. The other end can be loaded from either end.
DownloadThe above content, only proves that one-to-two bidirectional association mapping does not affect the storage is not change the table structure, but can not prove that the association is bidirectional, we need to write the corresponding test cases, we are in the form of source code to everyone. SOURCE Download
SummarizeOne-to-two bidirectional correlation mapping is not required and is determined by demand. If there is no such demand, the user does not require, the system does not need, there is no need to establish a two-way correlation mapping.
Next blog post, we introduce many-to-many one-way association mappings, thank you for your attention.
"SSH Advanced path" hibernate mapping--one-to-one bidirectional correlation mapping (vi)