The relationship between a person and an identity card is a typical one-to-one association. There are two ways to implement a one-to-one correlation mapping, which is based on the foreign key, and one is based on the primary key, and we look at the foreign key-based correlation method.
First look at their entity classes.
Person class
Package Entity;public class Person {private Integer id;private String name;private idcard idcard;public integer getId () {R Eturn ID;} public void SetId (Integer 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;}}
Then look at the Idcard class
Package Entity;public class Idcard {private integer id;private String number;private person person;public Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetNumber () {return number;} public void Setnumber (String number) {this.number = number;} Public Person Getperson () {return person;} public void Setperson (person person) {This.person = person;}}
There is a property of each other type in the person class and the Idcard class.
Then look at the relational mappings between them, first look at Person.hbm.xml
<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
Then take a look at the IdCard.hbm.xml document
<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
So the one-to-one relationship between person and Idcard is set up, and then look at the test class
Package Test;import static Org.junit.assert.*;import Org.hibernate.session;import Org.hibernate.transaction;import Entity. Idcard;import entity. Person;import Factory. Hibernatesessionfactory;public class Test {private session session = Null;private Transaction tran = null; @org. junit.test public void Save () {session = Hibernatesessionfactory.getsession (); tran = Session.begintransaction (); try{//New Object person p = new Person ();p. SetId (1);p. SetName ("Zhang San"), Idcard Idcard = new Idcard (); Idcard.setid (1); Idcard.setnumber ("10001"); P.setidcard (Idcard); Idcard.setperson (P); Session.save (P); Session.save (Idcard); Tran.commit (); catch (Exception e) {tran.rollback ();}} @org. junit.testpublic void Get () {session = Hibernatesessionfactory.getsession (); tran = Session.begintransaction (); Try{person person = (person) session.get (Person.class, 1); Idcard Idcard = Person.getidcard (); System.out.println (Person.getname () + "Social Security Number:" +idcard.getnumber ()); Tran.commit (); catch (Exception e) {tran.rollback ();}} Release Relationship @org.junit.testpublicvoid Removerelation () {session = Hibernatesessionfactory.getsession (); tran = Session.begintransaction (); try{// From the foreign key side contact relationship can not be removed from the non-foreign key side idcard Idcard = (idcard) session.get (Idcard.class, 1); Idcard.setperson (null); Tran.commit ();} catch (Exception e) {tran.rollback ();}} Delete @org.junit.testpublic void Deleterelation () {session = Hibernatesessionfactory.getsession (); tran = Session.begintransaction (); try{//if not associated with the other, you can delete it directly. If the association is associated and can be maintained, he deletes the association and deletes himself. If the relationship is associated, and the association relationship cannot be maintained, the delete itself is performed and an exception is thrown. Idcard Idcard = (idcard) session.get (Idcard.class, 1); Session.delete (Idcard); Tran.commit ();} catch (Exception e) {tran.rollback ();}}}
This is based on the foreign key approach, based on the primary key method only need to modify the map file on the above basis, the following to see the modified mapping file
First Look at Person.hbm.xml
<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
and watch IdCard.hbm.xml.
<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
One-to-one correlation relation of hibernate correlation relation mapping