Knowing the mapping principle of many-to-one correlation mappings, let's look at the case of single-to-one correlation, with two implementations of a pair of mappings:
A pair of primary key association mappings
We are divided into one-way and two-way in the case of the related cases, and Hibernate uses the one-to-one label to identify each other.
Principle Analysis
We have an example of a one-to-one association object (person) and ID (Idcard), whose entity diagram is:
With the first scenario, the Person Correspondence database table corresponds to the primary key in the Idcard corresponding database table, which does not need to add extra fields to represent the foreign key. The configuration in the Person association mapping file is:
<?xml version= "1.0"?><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourceforge.net/hib Ernate-mapping-3.0.dtd "><hibernate-mapping> <class name="Com.tgb.hibernate.Person" table="T_person"> <ID name="id"> <!--PRIMARY KEY policy foreign -- <generator class="foreign"> <!--property refers to the associated object -- <param name="Property">Idcard</param> </Generator> </ID> < property name="name" /> <one-to-one name="Idcard" constrained="true" /> </class></hibernate-mapping>
Note: constrained= "true" in the one-to-one tag indicates that the current primary key is also a foreign key, referencing the primary key in Idcard.
Operation Example
1. Call the session's Load method to derive the Cascade class object for the person object Idcard
public void TestLoad () {Session session = NULL;Transaction tx = NULL;try{session = Hibernateutils. GetSession();tx = Session. BeginTransaction();Person person = (person) session. Load(Person. Class,1);System. out. println("Person.name ="+ Person. GetName());System. out. println("Person.idCard.cardNo ="+ Person. Getidcard(). Getcardno());Session. Save(person);Tx. Commit();}catch (Exception e) {E. Printstacktrace();if (tx! = null) {TX. Rollback();}}finally{Hibernateutils. CloseSession(session);}}
Similar to the one described in the previous article, by one-to-one the role of tags, we query the person object while cascading queries in addition to the Idcard object
2. Default Cascade save Idcard mechanism
Public void Testsave() {Session session =NULL; Transaction tx =NULL;Try{session = Hibernateutils.getsession (); tx = Session.begintransaction (); Idcard Idcard =NewIdcard (); Idcard.setcardno ("11111111111"); Person person =NewPerson (); Person.setname ("Zhang San"); Person.setidcard (Idcard); Session.save (person); Tx.commit (); }Catch(Exception e) {E.printstacktrace ();if(tx! =NULL) {tx.rollback (); } }finally{hibernateutils.closesession (session); }}
As we mentioned in the previous article, an exception occurs when an object that references the persistent state of a transient object is saved, but this does not occur on a one-to-one primary key affinity mapping. That is, one-to-one defaults with the Cascade property. For a-to-one primary key association mapping, the Bidirectional association configuration is to add the one-to-one tag on the Idcard side, which only affects loading and does not affect storage.
In the future, if you change to a more than a non-modifiable, less flexibility, poor scalability.
One-to-one foreign key association mappings
We can consider a one-to-one situation as an extreme manifestation of many pairs of situations, and we can use the Many-to-one tag with the unique attribute to add foreign key fields to their corresponding tables, which resolves the drawbacks of using primary key affinity mappings. The relevant mapping files are as follows:
<?xml version= "1.0"?><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourceforge.net/hib Ernate-mapping-3.0.dtd "><hibernate-mapping> <class name="Com.tgb.hibernate.Person" table="T_person"> <ID name="id"> <generator class="native" /> </ID> < property name="name" /> <many-to-one name="Idcard" unique="true"></many-to-one> </class></hibernate-mapping>
For the appropriate action, refer to the previous article for a multi-click example.
Summarize
We can see that a pair of primary keys associated to one-to-two foreign key associations can be viewed as a different perspective, with the idea that some of the previously demanding situations can be handled in another way, to achieve the same effect, and to achieve a flexible response to future changes.
Hibernate (v)-Associative mapping (one-to-one)