1. Overview of Knowledge points
<!--formatted SQL statements, good-looking-->
<property name= "Format_sql" >true</property>
<!--display SQL statements-->
<property name= "Show_sql" >true</property>
The correlation includes the relation of diversity and directionality. Mapping Association relationship is to map the association relationship between classes in object model to the foreign key reference relationship between database tables in the relational model.
2. One-to-one relationship
One-way one-to-one: (Citizen and identity card)
mode one: Based on unique primary key
Citizen.hbm.xml
<many-to-one name= "Idcard" column= "idcard_id" unique= "true" cascade= "all"/>
Like a many-to-many relationship, the most important thing is to unique= the phrase "true" to indicate that the relationship is one-to-one.
mode two: Based on foreign key
Citizen.hbm.xml:
<id name= "id" column= "id" >
<generator class= "foreign" >
<param name= "Property" >idCard</param>
</generator>
</id>
....
<one-to-one name= "Idcard" constrained= "true" cascade= "all"/>
Based on the comparison of the main key is more complicated, the specific is the above, a class of primary key is another class of foreign keys. constrained= "true", which means whether to display a reference statement, usually when creating a table, whether to write a foreign key reference statement, true to write.
Bidirectional one-to-one: (Citizen and identity card) a one-to-one association based on a unique foreign key the foreign key can be placed at either end, the Many-to-one element is used to add the unqiue=true to the end of the external key, and the one-to-one element is required for the end of the foreign key.
At this point you also need to specify that this end is only associated with the other end by using the Property-ref property, and no additional columns or foreign keys are required to manage this relationship by hibernate. Citizen.hbm.xml <many-to-one name= "Idcard" column= "idcard_id" unique= "true" cascade= "all"/> IDCard.hbm.xml < One-to-one name= "Citizen" property-ref= "Idcard"/> based on the primary key at one end of a primary key using the foreign policy to reference the primary key at the other end to build.
This will have the same primary key value at both ends. 3. Case study 1) one-way one-to-one: (Citizenship and identity card) mode one: Based on unique primary key Citizen.hbm.xml <?xml version= "1.0"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourcefo Rge.net/hibernate-mapping-3.0.dtd ">