On Hibernate relational Mapping (2)

Source: Internet
Author: User

Following the previous blog

One-to-one relationship mapping: A to-do correlation mapping in real life is more common, such as the relationship between people and identity cards, through the object can find his relevant content.


One-way unidirectional (primary key):



One-way primary key association, by their primary key is equal, from the person can see Idcard, that is, the t_idcard in the primary key is taken as the primary key T_pseron.

Line represents an association, in the Person can be seen in Idcard . That is , the reference that holds idcard in person

Person mapping relationships for classes


Idcard The mapping Relationship



One-to-one bidirectional (primary key):


A one-to-one, two-way primary Key association is the same as the single-way primary key association, which is a single-way primary key association, where Idcard can be seen on the person side, and Idcard cannot see the person side. The two-way association is also able to see the person at the Idcard end, that is, not only add <one-to-one> tags in the Person.hbm.xml, but also add <one-to-one> in the IdCard.hbm.xml file. Label. The code is shown below.

Idcard The mapping Relationship



One-way mapping (unique foreign Key Association)


One-way unique foreign Key association, which is a special case of many-to-one association, restricts the one end to the other, which is a unique foreign key association. Same as many to one, at one end of the other side and adopt the <many-to-one> tag, by unique= "true", so as to limit the number of the end of a.

Person The mapping Relationship:



Idcard The mapping Relationship:



That is, with a foreign key field idcard on the T_person side, limiting the uniqueness of Idcard is one-to-single foreign Key association.

One-to-one bidirectional (unique Foreign Key Association)


Single-to-one foreign key unidirectional association we have learned that the two-way reverse is in the absence of the end of the add on it.

Idcard The mapping Relationship:



From the above, it can be concluded that for one-to-two correlation mappings, the primary Key association and the unique Foreign Key association both unidirectional and bidirectional generated table structure is the same, different when loading different. That is, one-to-two bi-directional correlation is compared to single-direction correlation, which only changes the load of a single-link mapping without changing the storage.

Here's a comparison of the primary key associations with unique foreign key associations:

Primary Key Association: that is, to have two objects have the same primary key value to indicate a one by one correspondence between them, the database table will not have additional fields to maintain the relationship between them, only through the table's primary key to associate.

Unique FOREIGN Key Association: A Foreign Key association is intended to be used for many-to-one configurations, but it can also be used to represent an affinity relationship if a unique limit is added

One2One without destroying the table structure, no more fields will be added at any end, just looking up together. The primary key association does not alter the storage, but the unique Foreign key association affects the storage. For how to choose, it is recommended to use a unique foreign key association! The primary key association is not easy to extend, and the dependencies between the two classes are too strong.

Continue to the next article!



Related Article

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.