Analysis of Hibernate mappings--Seven mapping relationships

Source: Internet
Author: User
Tags map class

An ORM, the full name (objectrelational Mapping), 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 Classes ( *.java ): It is the structure that describes the database table, the fields in the table are described as attributes in the class, and in the future it is possible to map the records in the table into objects of that class.

Mapping file (*.hbm.xml): It is the relationship between the specified database table and the mapping class, including the mapping class and database table correspondence, The correspondence 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, login password, and connection string for the login database. Of course, you can also put the Map class address mapping information here.

then let's walk in together Hibernate Seven kinds of 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 a number of the end of a unique is true, this 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.

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>

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.

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-- Idcard Add the following tag mappings to the new port :

[Java]view Plaincopy
    1. <one-to-one name= "Person" />

Two-way one-to-one unique foreign key Mapping key mapping code-- Idcard Add the following tag mappings to the new end :

[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> label mappings, you must specify <one-to-one> in the label Property-ref property is 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> Labels and <many-to-one> label added fields remain constant, otherwise data clutter

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" />

notes: Inverse Properties

* 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 (role) side key mapping code:

[Java] view plaincopy
    1. < Set name= "users"  table= "T_user_ Role " >  
    2.        <key  Column= "role_id" />  
    3. < Span style= "FONT-FAMILY:SIMSUN; Font-size:14px ">        <many-to-many  Class = "Com.hibernate.User"  column= "user_id" />  
    4. lt;/set>  

User (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 =  column= " role_id " />  
    4. lt;/set>  

Summarize

The most important of these seven kinds of correlation mappings 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 repetitive problems in life, the duplication of questions to the computer to help us complete , so as to improve our efficiency. A word: life away from programming, programming more inseparable from life, learning to combine life, understanding more deeply.


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.