One -to-one mapping in the object model is often seen, mainly to transform the object model into a relational model must be configured in the mapping file, focusing on the use of <one-to-one> tags, there are two ways, the first is the primary key association, The second is the only foreign key association, and now the first way to see it first.
A primary key Association in a one-to-one association map, which means that two objects have the same primary key value to indicate a one by one corresponding relationship between them, and the database table does not have additional fields to maintain the relationship between them, simply by the table's primary key.
the person class and the Idcard class is a one by one correspondence, one can only have an identity card, an identity card can only correspond to one person, then the identity of the identity card is also the identity of the person class, the primary key as the identity card Idcard class foreign key. How does this one-off primary key correlation relate to what is implemented in hibernate? like this relationship, there are two ways to achieve this.
The first: The ID in person is not treated as a foreign key, only as the primary key in person, but only for the ID in person and the ID in the idcard corresponding. This is handled on the mapping relationship:
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
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Idcard class:
Package Com.tgb.zhudan;public class Idcard {private int id;private String cardno;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;}}
Person class:
Package Com.tgb.zhudan;public class Idcard {private int id;private String cardno;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;}}
Hibernate.cfg.xml
<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">At this point, when you delete the Idcard table, you can easily delete it.
The disadvantage of this method: since it is man-made control, then in fact, it is not very good control, not very good person ID and idcard in the ID corresponding to, when I delete in the Idcard and add another, then I this piece of data I should be how to correspond with the previous person. Or, after I delete a piece of data in person, I add the data that is exactly the same (note that the ID has changed, but that's not changed), so how to correspond to the ID in Idcard. Therefore, it is not advisable to use this method in practical applications.
Second: The ID in person is treated as the primary key as a foreign key. The mapping relationship is as follows:
The main is to modify the Person.hbm.xml file.
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
At this point, when you delete the Idcard table, a prompt pops up:
Tip We cannot delete, there are foreign key constraints.
Summary: A summary of one sentence the primary Key association in an association map is that you do not need to add extra fields, so that the two entities have the same primary key.
"SSH Tour" step-by-step learning Hibernate Framework (II): PRIMARY Key Association in one-to-two association mappings