One-way bi-directional analysis of 5-hibernate cascade of experience

Source: Internet
Author: User

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 ">  


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.