Link ing of Hibernate relationship ing

Source: Internet
Author: User

Hibernate relationship ing
1. Association ing
Concept: ing is to map an association to a database. The so-called Association is one or more references in the object model.

Category:

(1) Multi-to-one association ing
Allow-to-one

Principle of Multi-to-one association ing: Add a foreign key to multiple ends to point to one end

Use the following tag ing on multiple ends:

<Role-to-one name = "group" column = "groupId"/>
(2) one-to-one association ing
One-to-one

A primary key Association: let two objects have the same primary key value to indicate the one-to-one correspondence between them. The database table does not have any additional fields to maintain the relationship between them, associate only with the table's primary key

Example 1:

Hibernate one-to-one primary key Association ing (one-way Association Person ---> IdCard)

One-to-one association ing principle: make the two entities have the same primary key, so that no extra fields need to be added (poor scalability)

<Class name = "com. ys. hibernate. Person" table = "t_person">
<Id name = "id">
<! -- Use the foreign generation policy to obtain the ID of the associated object -->
<Generator class = "foreign">
<! -- Property indicates the associated object -->
<Param name = "property"> idCard </param>
</Generator>
</Id>
<Property name = "name"/>
<! --
One-to-one indicates how hibernate loads its associated objects. By default
That is, get the relational field value and load the correlated object based on the primary key of the Peer end.

Constrained = "true" indicates that the current primary key (the primary key of Person) is a foreign key
Refer to the primary key of the Peer (the primary key of the IdCard), that is, a foreign key constraint statement is generated.
-->
<One-to-one name = "idCard" constrained = "true"/>
</Class>
Example 2:

Hibernate one-to-one primary key Association ing (two-way Association Person <---> IdCard)

You need to add the <one-to-one> label to the IdCard,

Instructs hibernate to load the correlated object Person according to the primary key.

<One-to-one> storage is not affected, but loading is affected.

<One-to-one> the default label fetch attribute is join ".

 

Unique foreign key Association of B: foreign key Association, which is originally used for multiple-to-one configuration. However, if a unique restriction is added, it can also be used to represent a one-to-one association.

Example 1:

Hibernate one-to-one unique foreign key Association ing (one-way Association Person ---> IdCard)

One-to-one unique foreign key Association ing is actually a special case of many-to-one

Use the <relative-to-one> label to map, and set unique to true for multiple ends,

In this way, the multiple ends of multiple ends are limited to one, which is mapped in this way.

<Subtitle-to-one name = "idCard" unique = "true"/>
Example 2:

Hibernate one-to-one unique foreign key Association ing (two-way Association Person <---> IdCard)

One-to-one unique foreign key is associated with two-way, using <one-to-one> tag ing,

The property-ref attribute in the <one-to-one> label must be specified as the name of the link field.

<One-to-one name = "person" property-ref = "idCard"/>
(3) One-to-Multiple Association ing
One-to-least

Example 1:

Hibernate one-to-Multiple Association ing (one-way Classes ---> Student)

The principle of one-to-many and one-to-one ing is the same. A foreign key is added to one end to point to one end.

 

The difference between them is that the maintained relationship is different:

* Multi-to-one maintenance relationship: multiple links direct to one. If multiple links direct to one are maintained, one is loaded when multiple links are loaded.

* One-to-multiple maintenance relationships: One-to-multiple relationships. If one-to-multiple relationships are maintained, the one-to-many relationships are loaded.

There are defects in maintaining the relationship at one end:

* Because Student does not know the existence of Classes (that is, Student does not maintain the relationship with Classes)

Therefore, when the Student is saved, the classesId of The Link field is null. If this field is set to non-empty, data cannot be saved.

* In addition, because Student does not maintain the relationship, but Classes maintains the relationship, Classes will issue redundant update statements to ensure that Classes is related to Student, in this way, the corresponding student set of the Classes can be loaded.

<Set name = "students">
<! --
<Key column = "classesId" not-null = "true"/> -- the storage fails.
-->
<Key column = "classesId"/> -- redundant update statements are issued to maintain the relationship.
<One-to-learn class = "com. ys. hibernate. Student"/>
</Set>
Example 2:

Hibernate one-to-Multiple Association ing (bidirectional Classes <---> Student)

The purpose of one-to-multiple bidirectional Association ing is to solve the defects of one-to-multiple unidirectional Association.

Not demand-driven

 

One-to-multiple bidirectional Association ing:

* Use the <key> label on the set at one end, and add a foreign key to multiple ends.

* Use the <allow-to-one> label on multiple ends

<Subtitle-to-one name = "classes" column = "classesId"/>
!!! Note:

The <key> label and the <relative-to-one> label must be the same, otherwise data confusion occurs.

(4) Multi-to-Multi-Association ing
Copying-to-Sequence

Example 1:

Hibernate multi-to-Multi-Association ing (one-way User ---> Role)

Specific ing:

<Set name = "roles" table = "t_user_role">
<Key column = "user_id"/>
<Role-to-operate class = "com. ys. hibernate. Role" column = "role_id"/>
</Set>
Extracts a class from the third table and uses two classes-to-one to complete the operation.

Example 2:

Hibernate multi-to-Multi-Association ing (two-way User <---> Role)

Specific ing:

<Set name = "roles" table = "t_user_role">
<Key column = "user_id"/>
<Role-to-operate class = "com. ys. hibernate. Role" column = "role_id"/>
</Set>
Note:

* The Name Of The generated intermediate table must be the same

* The fields in the generated intermediate table must be the same.

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.