In the first-to-one mapping of foreign keys described in the previous section, there is a foreign key column idcard_id in the person table, the primary key ID of the corresponding Idcard table, and one-to-one mapping based on the primary key, meaning that the foreign key column of the person table is discarded idcard_id. Instead of a primary key, or a table structure that is the main key, the primary key-based two-way mapping relationship with the Idcard table should be configured like this:
Create a new Idcard entity class:
Public class Idcard { privateint ID; Private String code; Private person person ; // Get/set method omitted }
Create a new person entity class:
Public class Person { privateint ID; Private String name; Private int Age ; Private Idcard Idcard; // Get/set method omitted }
The mapping file for the new Idcard class under the current package IdCard.hbm.xml:
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hib Ernate-mapping-3.0.dtd "><hibernate-mapping Package= "Com.wang.pojo"> <classname= "Idcard" > <IDname= "id"> <Generatorclass= "Native"></Generator> </ID> < Propertyname= "Code"></ Property> <!--just add this tag to - <one-to-onename= "Person"></one-to-one> </class></hibernate-mapping>
The mapping file for the new person class under the current package Person.hbm.xml:
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hib Ernate-mapping-3.0.dtd "><hibernate-mapping Package= "Com.wang.pojo"> <classname= "Person" > <IDname= "id"> <!--foreign represents a foreign key reference - <Generatorclass= "foreign"> <!--referencing the primary key in the Idcard table - <paramname= "Property">Idcard</param> </Generator> </ID> < Propertyname= "Name"></ Property> < Propertyname= "Age"></ Property> <!--constrained= "True" indicates that the PRIMARY KEY constraint is set to True - <one-to-onename= "Idcard"constrained= "true"Cascade= "Save-update"></one-to-one> </class></hibernate-mapping>
Add two mapping files to Hibernate.cfg.xml; New test class, test: 1, automatically generate table 2: Save data 3: Read data:
@Test Public voidTestcreatedb () {Configuration cfg=NewConfiguration (). Configure (); Schemaexport SE=NewSchemaexport (CFG); //whether the first parameter generates a DDL script whether the second parameter executes to the databaseSe.create (true,true); } @Test Public voidTestsave () {Session session=hibernateutil.getsession (); Transaction TX=session.begintransaction (); //score S=new score (2,12,97); //Session.save (s);Idcard id1=NewIdcard (); Id1.setcode ("3713251882923783494"); Idcard Id2=NewIdcard (); Id2.setcode ("3713251882925378612"); Person P1=NewPerson (); P1.setname ("Li Yunlong"); P1.setage (41); P1.setidcard (ID1); Person P2=NewPerson (); P2.setname ("Zhuyunfei"); P2.setage (34); P2.setidcard (ID2); Session.save (p1); Session.save (p2); //Session.save (p3);Tx.commit (); Session.close (); } @Test Public voidTestget () {Session session=hibernateutil.getsession (); Transaction TX=session.begintransaction (); Person P= (person) session.get (person).class, 1); System.out.println ("Name=" +p.getname () + "===idcard=" +P.getidcard (). GetCode ()); Idcard ID= (Idcard) session.get (Idcard.class, 1); System.out.println ("Name=" +id.getperson (). GetName () + "==idcard=" +Id.getcode ()); Tx.commit (); Session.close (); }
The above is a two-way mapping based on the primary key, if only one-way mapping should be set, if only through the person class can get to the Idcard class information can be, do not need to idcard to get the information in person, as long as the Idcard class private perosn person; The corresponding Get/set method is removed, and then the <one-to-one name= "Person" ></one-to-one> label in the IdCard.hbm.xml is removed. There's no need to change anywhere else. .
Hibernate notes-a one-to-two mapping relationship based on the primary key