Hibernate implements a one-to-one mapping with a foreign key-based approach and a primary key-based approach. Due to the problem of the primary key mode mapping and the lack of flexibility in implementing operations such as deletion, it is generally recommended to use a foreign key-based approach.
Take an example of the relationship between an individual and an identity card (mainly looking at the configuration and entity classes of the mapping file):
Foreign key-based approach:
Package Test.hibernate.hbmonetoone;public class Person {private Integer id;private String name;private idcard idcard; Public Integer GetId () {return 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;} @Overridepublic String toString () {//TODO auto-generated method Stubreturn "[person:id=" + ID + ", name=" + name + "]";}}
Package Test.hibernate.hbmonetoone;public class Idcard {private Integer id;private String idnumber;private person person ;p ublic Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String Getidnumber () {return IDNumber;} public void Setidnumber (String iDNumber) {iDNumber = IDNumber;} Public Person Getperson () {return person;} public void Setperson (person person) {This.person = person;} @Overridepublic String toString () {//TODO auto-generated method Stubreturn "[idcard:id=" +id+ ", idnumber=" +idnumber+ "]" ;}}
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 "><?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
Test classPackage Test.hibernate.hbmonetoone;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import Org.junit.test;public class App {private static sessionfactory Sessionfactory = New Configuration ()//.configure ()//.addclass (Person.class)//Add Hibernate entity class (load corresponding mapping file). addclass (Idcard.class)//. Buildsessionfactory (); @Testpublic void Testsave () throws Exception {Session session = Sessionfactory.opensession (); Session.begintransaction ();//--------------------------------------------//Build object person person = new person (); Person.setname ("Zhang San"), Idcard Idcard = new Idcard (), Idcard.setidnumber ("377489392002121X");//Associated Person.setidcard ( Idcard); Idcard.setperson (person);//save Session.save (person); Session.save (idcard);//----------------------------- ---------------session.gettransaction (). commit (); Session.close ();} @Testpublic void Testget () throws Exception {Session session = Sessionfactory.opensession (); Session.begintransaction () ;//Get data person person = (PerSon) Session.get (Person.class, 2); SYSTEM.OUT.PRINTLN (person); System.out.println (Person.getidcard ()); Idcard Idcard = (idcard) session.get (Idcard.class, 2); System.out.println (Idcard); System.out.println (Idcard.getperson ()); Session.gettransaction (). commit (); Session.close ();} Disassociate relationship, from having foreign key square @testpublic void Testremoverelation () throws Exception {Session session = Sessionfactory.opensession (); Session.begintransaction ();//--------------------------------------------Idcard Idcard = (idcard) session.get ( Idcard.class, 1); Idcard.setperson (null);//--------------------------------------------session.gettransaction (). Commit (); Session.close ();} Delete, can only do have foreign key side to no foreign key side of the one-way Association @testpublic Void Testdelete () throws Exception {Session session = Sessionfactory.opensession (); Session.begintransaction ();//--------------------------------------------//person person= (person) session.get ( Person.class, 2);//Session.delete (person);//delete failed with PersonID foreign key, throw exception idcard Idcard = (idcard) session.get (Idcard.class, 3); Session.deleTe (Idcard);//The card is deleted, the person is still in//--------------------------------------------session.gettransaction (). commit (); Session.close ();}}
Primary key-based approach (map file configuration is different):
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.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 ">
Package Test.hibernate.hbmonetoone2;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import Org.junit.test;public class App {private static sessionfactory Sessionfactory = New Configuration ()//.configure ()//.addclass (Person.class)//Add Hibernate entity class (load corresponding mapping file). addclass (Idcard.class)//. Buildsessionfactory (); @Testpublic void Testsave () throws Exception {Session session = Sessionfactory.opensession (); Session.begintransaction ();//--------------------------------------------//Build object person person = new person (); Person.setname ("Zhang San"), Idcard Idcard = new Idcard (), Idcard.setidnumber ("377489392002121X");//Associated Person.setidcard ( Idcard); Idcard.setperson (person);//save//session.save (person); Session.save (idcard);//--------------------------- -----------------session.gettransaction (). commit (); Session.close ();} Disassociate relationship @testpublic void Testremoverelation () throws Exception {Session session = Sessionfactory.opensession (); Session.begintransaction ();//--------------------------------------------//idcard Idcard = (idcard) session.get (Idcard.class, 1);// Idcard.setperson (null);//Throw exception person person= (person) session.get (Person.class, 3);p erson.setidcard (null);// The data in the database has not changed//--------------------------------------------session.gettransaction (). commit (); Session.close ();} Delete @testpublic void Testdelete () throws Exception {Session session = Sessionfactory.opensession (); Session.begintransaction ();//--------------------------------------------//idcard Idcard = (idcard) session.get ( Idcard.class, 2);//session.delete (idcard);//Normal person person= (person) session.get (Person.class, 3); Session.delete ( person);//throw exception, ID as foreign key//--------------------------------------------session.gettransaction (). commit (); Session.close ();}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If you want to reprint, please specify the source: Http://blog.csdn.net/lindonglian
A one-to-one association mapping relationship for Hibernate