Hibernate affinity Configuration (one-to-one, one-to-many, and many-to-many)

Source: Internet
Author: User

A one-to-many correlation mapping using a multi-pair correlation mapping principle

Many-to-one association mappings: one end of a foreign key is added to one side of the many, and it maintains a relationship that points to a
One-to-many association mappings: at one end of a multi-side join a foreign key to the end, it maintains the relationship is a point to many

That is, a one-to-many and a-to-many mapping strategy is the same, but the angle of the station is different

Disadvantages of maintaining relationships at one end:
* If you set the Classesid field in the T_student table to non-empty, you cannot save
* Because the relationship is not maintained at the end of the student, so student does not know which class it is,
So you need to issue an extra UPDATE statement to update the relationship

Hihernate one-to-many association mappings (bidirectional classes<----->student)

One-to-many bidirectional association mappings:
* Use <key> on a one by one-terminal set, and add a foreign key to one end of the table
* Use <many-to-one> at multiple end

Note The foreign key field specified by the:<key> label must be the same as the foreign key field specified by <many-to-one>, otherwise the error of the reference field

If you maintain a one-to-many relationship at the "A" end, hibernate emits extra udpate statements, so we generally
One end of the link to maintain the relationship

About the Inverse property:
Inverse is mainly used in a one-to-many and many-to-many bidirectional association, inverse can be set to the set label <set>,
The default inverse is false, so we can maintain the correlation from the "one" End and the "many" end.
If set to inverse to true, then we can only maintain correlation from multiple end

Note: The inverse property only affects the storage of data, which is persistent

Inverse and Cascade
* Inverse is the control direction of correlation relation
* The chain reaction of cascade operation




Hibernate pair of primary key association mappings (one-way Association person---->idcard)

Two-to-one primary key affinity mapping: Keep the IDs of two entity objects the same, which avoids unnecessary fields being created

Specific mappings:

<id name= "id" >
<!--person's primary key source Idcard, which is the primary key of the shared Idcard--
<generator class= "foreign" >
<param name= "Property" >idCard</param>
</generator>
</id>
<property name= "Name"/>
<!--the meaning of the one-to-one tag, indicating how hibernate loads its associated object, which is loaded by default based on the primary key,
Constrained= "True", indicating that a constraint exists on the current primary key, and the person's primary key is referenced as a foreign key idcard
-
<one-to-one name= "Idcard" constrained= "true"/>




Hibernate pair of primary key Association Mappings (Bidirectional Association person<---->idcard)

You need to include the <one-to-one> tag in the Idcard mapping file to point to person, indicating how hibernate loads the person
Default load based on primary key




Hibernate pair one unique Foreign Key association mapping (one-way Association person---->idcard)

A pair of unique foreign key association mappings is a special case of many-to-one association mappings

You can use the <many-to-one> tag to specify the unique=true at one end, which limits the multiplicity of the multiple end to a
Mapping one-to-one unique foreign key associations by this means




Hibernate pair one unique Foreign Key Association mapping (bidirectional Association person<---->idcard)

One-to-one unique foreign key association bidirectional, need at the other end (Idcard), add <one-to-one> tag, indicate hibernate how to load
Its associated object, which is loaded by default according to the primary key in the person, foreign Key association mapping, because two entities are using the relationship of the foreign key maintenance of the person,
Therefore, you cannot specify a primary key to load a person, but to load according to the foreign key of person, the following mapping method is used:
<one-to-one name= "Person" property-ref= "Idcard"/>




Hibernate many-to-one association mappings

The nature of the associated mappings:
* Mapping association relationships to databases, so-called association relationships are one or more references to the object model in memory

<many-to-one> will add a foreign key to one end of the multi-point, and the foreign key is the <many-to-one>
If this property is omitted, the default foreign key is consistent with the attribute of the entity

Examples of definitions for <many-to-one> tags:
* <many-to-one name= "group" column= "GroupID"/>

Understand the meaning of cascading?
* is the object's chain operation



Hibernate many-to-many association mappings (one-way user---->role)

Specific mapping Method:
<set name= "Roles" table= "T_user_role" >
<key column= "userid"/>
<many-to-many class= "Com.bjsxt.hibernate.Role" column= "Roleid"/>
</set>



Hibernate many-to-many association mappings (bidirectional user<---->role)

Mapping method:
<set name= "Roles" table= "T_user_role" >
<key column= "userid"/>
<many-to-many class= "Com.bjsxt.hibernate.Role" column= "Roleid"/>
</set>
The Table property value must be the same as the table property value in a one-way association
The value of the column property in the <key> is consistent with the value of the column attribute in the <many-to-many> tag in one-way association
The value of the column property in <many-to-many> is consistent with the value of the column property of the <key> tag in one-way association

Hibernate affinity Configuration (one-to-one, one-to-many, and many-to-many)

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.