Hibernate basic Mapping

Source: Internet
Author: User

with the step-by-step practice of hibernate , it is necessary to make a summary, so that you can make your ideas clearer!

I. Knowledge generalization

The whole idea of mind mapping is:


second, the corresponding structure diagram and implementation     Span lang= "ZH-CN" style= "Font-family:calibri" >   (i) One-way, single-connection mapping ( one-to-one ):

A pair of relationships between two objects, for example: person (people)-idcard (ID)

There are two strategies that can be implemented for a-to-one correlation mapping:

         * PRIMARY Key Association: that is, let two objects have the same primary key value to indicate a one by one corresponding relationship between them; database tables do not have additional fields to maintain relationships between them, and are only associated with the primary key of the table. such as:


Example: one-way primary Key Association example connection

         * Unique Foreign Key association: A Foreign Key association, originally used for many-to-one configurations, but with a unique limit ( <many-to-one> label to map, specify a multiple end Unique to be true , which limits the multiplicity of one end to the other, and can also be used to denote a single-to-one correlation, in fact it is a multi-pair special case. such as:


Example: one-way, single-click, unique Foreign Key Association example connection

Note: Because a one-to-one primary key correlation mapping is not good enough, when our needs change to become one-to-many, it becomes impossible to operate, so when we encounter a one-to-one correlation, we often use unique foreign key associations to solve the problem, rarely using a one-to-one primary key association.

(b) One-way many-to-one correlation mapping (many-to-one):

Many-to-one correlation mapping principle: Add a foreign key at one end of the many, point to the end of a, such as:


Key Mapping code--Add the following tag mappings at one end of the number:

<span style= "FONT-SIZE:18PX;" >[java] viewplaincopy01.<many-to-onename= "group" column= "GroupID"/>  </span>

(iii) unidirectional one-to-many association mappings (one-to-many):

One-to-many correlation mappings and many-to-one correlation mappings are consistent, with a foreign key at one end, pointing to the end of the other . such as (Students and classes):


Note: The difference between it and many-to-one is that the maintenance relationship is different

        * Many-to-one maintenance relationship is: a multi-point relationship, with this relationship, when loading can be loaded up

        * A one -to-many maintenance relationship is: a point-to-many relationship, with this relationship, when loading one can be more loaded up

Key Mapping code--Add the following tag mappings at one end:

<span style= "FONT-SIZE:18PX;" >[java] viewplaincopy01.<setname= "Students" >.      <keycolumn= "Classesid"/>.      <one-to-manyclass= "Com.hibernate.Student"/> 04.</set> </span>

      defect: Because one end of the manyStudentDon't knowClassesthe existence (i.e.StudentNo maintenance andClassesthe relationship) so in the SaveStudentWhen the relationship fieldClassesidis forNULL, If the relationship field is set to non-empty, the data cannot be saved, and the common workaround is to use bidirectional associative mappings instead, see6.

(d) One-way many-to-many mappings (Many-to-many):

Many-to-many association mappings add a new table to complete the basic mapping, such as:


Key Mapping code--You can Add the following tag mappings at one end of the User:

<span style= "FONT-SIZE:18PX;" >[java] viewplaincopy01.<setname= "Roles" table= "T_user_role" >.     <keycolumn= "user_id"/>.     <many-to-manyclass= "Com.hibernate.Role" column= "role_id"/> 04.</set> </span>
(v ) bidirectional one-to-one correlation mapping:

In contrast to one-way mapping, it is necessary to add <one-to-one> tags to the idcard , it does not affect, only affects loading. such as:


bidirectional one-to-one primary key Mapping key mapping code- Add the following tag mappings on the Idcard side:

<span style= "FONT-SIZE:18PX;" >[java] viewplaincopy01.<one-to-onename= "person"/> </span>

bidirectional one-to-one one-to-one unique foreign key mapping code--Add the following tag mappings on the Idcard side:

<span style= "FONT-SIZE:18PX;" >[java] viewplaincopy01.<one-to-onename= "person" property-ref= "Idcard"/> </span>

Note: one-to-one unique Foreign Key Association bidirectional adoption <one-to-one> tag mapping, must be specified <one-to-one> The Property-ref property in the label is the name of the relationship field

(vi ) bidirectional one-to-many association mappings (very Important):

The purpose of using a one-to-many bidirectional association mapping is primarily to solve the problem of a pair of multi-directional associations rather than demand-driven.

A one-to-many bidirectional association mapping method:

              * at one end of the set, using the <key> tags, adding a foreign key to the multiple end

              * at one end of the multi-use <many-to-one> label

Note:the<key> tag and <many-to-one> tag fields remain constant, otherwise data clutter

Key Mapping code:

Add the following tag mappings at one end of the Classes :

<span style= "FONT-SIZE:18PX;" >[java] viewplaincopy01.<setname= "Students" inverse= "true" >.       <keycolumn= "Classesid"/>.      <one-to-manyclass= "Com.hibernate.Student"/> 04.</set>      Add the following tag mappings at one end of the Student: [Java] Viewplaincopy01 . <many-to-onename= "Classes" column= "Classesid"/> </span>

Comment:inverse Property

                * Inverse property can be used on a one-to-many and many-to-many bidirectional association, inverse . Span lang= "en-US" style= "Font-family:simsun" the property defaults to false inverse true

* Inverse is the reversal in the control direction, affecting only the storage

(vii) bidirectional Many-to-many association mappings:

Two-way purpose is to both sides can be loaded up, and one-way many-to-many difference is two-way need to add tag mapping at both ends, it should be noted that:

              * The resulting intermediate table name must be the same

              * The fields in the resulting intermediate table must be the same

Role (role) side key mapping code:

<span style= "FONT-SIZE:18PX;" >[java] viewplaincopy01.<setname= "users" table= "T_user_role" >.       <keycolumn= "role_id"/>.       <many-to-manyclass= "Com.hibernate.User" column= "user_id"/> 04.lt;/set>        user-side key mapping code:        [Java] Viewplaincopy01.<setname= "Roles" table= "T_user_role" >.      <keycolumn= "user_id"/>.      <many-to-many class= "Com.hibernate.Role" column= "role_id"/> 04.lt;/set> </span>
Iii. Summary

for the above basic mapping relationship summary and application in the project, let me feel in real life often have such demand, so we must master and learn skillfully!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hibernate basic Mapping

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.