Analysis of Hibernate mappings--Seven mapping relationships

Source: Internet
Author: User
Tags map class

First we understand a noun orm, the full name is ( Object Relational Mapping), which is the object-relational mapping. The idea of ORM is to map the data of tables in a relational database into objects as objects, so that developers can translate the operations of the database into operations on those objects. Hibernate is the realization of this idea, to facilitate the development of people with object-oriented thinking to achieve the operation of the database.

The main files that Hibernate uses when implementing ORM functionality are: Mapping classes (*.java), mapping Files (*.hbm.xml), and database configuration files (*.properties/*.cfg.xml), which each function as follows.

Mapping Class (*.java): It is the structure that describes a database table, and the fields in the table are described as attributes in the class, which in the future can be implemented to map the records in the table into objects of that class.

mapping File (*.hbm.xml): It is the relationship between a specified database table and a mapping class, including mapping classes and database tables, corresponding relationships between table fields and class property types, and the correspondence between table fields and class property names.

Database configuration file (*.properties/*.cfg.xml): It is the connection information that is required to specify the connection to the database, such as which database to connect to, the user name to log in to the database, the login password, and the connection string. Of course, you can also put the Map class address mapping information here.

Next, let's go into Hibernate's seven mapping relationships:

1. One-way, single-link 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: Foreign Key Association, originally used for many-to-one configuration, but with a unique limit (using the <many-to-one> tag to map, specify that many of the end of the unique is true, so that the multiple end of the multiplicity of one), can also be used to indicate a one-to-many relationship, 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.

2, 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:

[Java]View Plaincopy
    1. <many-to-one name="group" column="GroupID"/>

3, 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

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

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

[Java]View Plaincopy
    1. <set name="Students" >
    2. <key column="Classesid"/>
    3. <one-to-many class="com.hibernate.Student"/>
    4. </set>

Flaw: Because many end student do not know the existence of classes (that is, student does not maintain the relationship with classes), so when saving student the relationship field classesid is null, if the relationship field is set to non-empty, You will not be able to save the data, a common workaround is to use bidirectional affinity mappings, see 6.

4, 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:

[Java]View Plaincopy
    1. <set name="Roles" table="T_user_role" >
    2. <key column="user_id"/>
    3. <many-to-many class="com.hibernate.Role" column="role_id"/>
    4. </set>

5. 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:

[Java]View Plaincopy
    1. <one-to-one name="person"/>

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

[Java]View Plaincopy
    1. <one-to-one name="person" property-ref="Idcard"/>

Note: one-to-one unique foreign Key Association bidirectional adoption <one-to-one> TAG mapping, you must specify the Property-ref attribute in the <one-to-one> tag as the name of the relationship field

6, 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:

* Use <key> tags on one end of the set, and add a foreign key at the end of the multiple

* Use <many-to-one> tag at one end of the multi

Note:<key> tags and <many-to-one> tags are added to the field to keep it up, otherwise it will result in data confusion

Key Mapping Code:

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

[Java]View Plaincopy
    1. <set name="Students" inverse="true" >
    2. <key column="Classesid"/>
    3. <one-to-many class="com.hibernate.Student"/>
    4. </set>

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

[Java]View Plaincopy
    1. <many-to-one name="classes" column="Classesid"/>

Comment: Inverse property

* The inverse property can be used on a one-to-many and many-to-many bidirectional association, the inverse property defaults to False, False indicates that the local side can maintain the relationship, if the inverse is true, then the side cannot maintain the relationship, the other end will be left to maintain the relationship, the end of the failure. So a one-to-many association map we usually maintain relationships on many of the other side, which invalidates a single end.

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

7, 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-side Key mapping code:

[Java]View Plaincopy
    1. <set name="users" table="T_user_role" >
    2. <key column="role_id"/>
    3. <many-to-many class="Com.hibernate.User" column="user_id"/>
    4. Lt;/set>

User-side Key mapping code:

[Java]View Plaincopy
    1. <set name="Roles" table="T_user_role" >
    2. <key column="user_id"/>
    3. <many-to-many class="com. Hibernate. Role "column=" role_id "/>
    4. Lt;/set>

Summary: For the above seven association mappings, the most important thing is a one-to-many mapping, because it is closer to our real life, such as: classrooms and students can be a typical one-to-many relationship, and we develop software is to solve some of the problems of repetitive life, We can improve our work efficiency by handing over the repetitive questions to the computer to help us finish. A word: Life away from programming, programming more inseparable from life.

    • Prev One-to-one, unique foreign key Association instance
    • Next Hibernate map parsing---inheritance mappings

Hibernate mapping Resolution-seven mapping relationships

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.