Shared primary key affinity mapping: Let two objects have the same primary key value to indicate a one-to-one correspondence between them; database tables do not have additional fields to maintain their relationships, only through the table's primary key
One, one-way Association mappings:
One (Maintenance side: Person)
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping dtd3.0//en" "Http://hibernate.sourceforge.net/hiber Nate-mapping-3.0.dtd ">To
One (non-maintenance side: Idcard)
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
public void Testonetoone () { session session = NULL; try { session = Hibernateutils.getsession (); Session.begintransaction (); Idcard idcard = new Idcard (); Idcard.setcardno ("111111"); Person person = new person (); Person.setname ("name"); Person.setidcard (Idcard); The attribute of a one-to-one correlation mapping determines that the associated object (Idcard) is saved when the person is saved (by default cascading properties), so the save person can be saved idcard //maintenance end to save Session.save ( person); Session.gettransaction (). commit (); } catch (Exception e) { e.printstacktrace (); Session.gettransaction (). rollback (); } finally { hibernateutils.closesession (session); } }
Two, two-way association mapping
One (person: maintenance side)
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
To
One (Idcard: non-maintenance)
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
Hibernate-----One-to-one shared primary key association mappings (map File mode)