SSH Deep Adventures (i) Hibernate architecture (i)-------mapping analysis--seven mapping relationships

Source: Internet
Author: User
Tags map class

ORM. The full name is (objectrelational Mapping), which is the objects relational mapping. The idea of ORM is to map the data of tables in a relational database into objects. Presented in the form of objects. This enables developers to translate the operations of the database into operations on those objects. Hibernate is the realization of this idea, to facilitate the developers to the 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). Their respective functions are for example the following.

Mapping Classes ( *.java ): It is a description of the structure of the database table, the table field in the class is described as a property, in the future can be implemented to map the table records into the object of the class.

Mapping file (*.hbm.xml): It is the relationship between the specified database table and the mapping class. Contains the corresponding relationship between the mapping class and the database table, the corresponding relationship of the table field and Class property type, and the corresponding relationship between the table field and the Class property name.

Database configuration file (*.properties/*.cfg.xml): It is the connection information that is required to specify the connection to the database. For example, which database to connect to, username to log in to the database, login password, and connection string.

Of course, you can also put the mapping information of the map class 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-idcard (ID)

There are two strategies to implement one-to-one correlation mappings:

* PRIMARY KEY Association: that is, two objects have the same primary key value to indicate a one by one corresponding relationship between them; The database table does not have additional fields to maintain the relationship between them. is only associated with the primary key of the table. For example, with:

Example: one-way primary Key Association sample 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 end unique is true , which limits the multiplicity of a single end to one ), and can also be used to indicate a to-do relationship. In fact, it is a special case of many-to-one. For example, with:


Example: one-way, single-to-all Foreign Key Association sample connection

Note: Due to one-to-one primary key association mapping extensibility is not good. When our needs change and we want to make them one-to-many, we are unable to manipulate them, so we often use unique foreign key associations to solve this problem when we encounter a single connection, and very little of a primary key association.

2, one-way many-to-one correlation mapping (many-to-one):

Many-to-one correlation mapping principle: Add a foreign key to one end of a multi-side. For example, with:


Key Mapping Code --Add the following tag mappings at one end of the multi-instance:

[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 the addition of a foreign key at one end to the other. For example with (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, loaded in more time can be loaded into the

* One-to-many maintenance relationships are: a point-to-many relationship with this relationship. You can load it up when you load one.

Key Mapping Code --Add the following tag mappings at one end, for example:

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

defect: Due to the multiple endStudentDon't knowClassesthe existence (i.e.StudentNo maintenance andClassesthe relationship) so in the SaveStudentWhen the relationship fieldClassesidis forNULL, assuming that the relationship field is set to non-null. You will not be able to save the data, often the workaround is to use bidirectional correlation mapping, see6.

4, One-way many-to-many mappings (Many-to-many):

Many-to-many association mappings Add a table to complete the basic mapping. For example, with:


Key Mapping Code --can be added at one end of the user such as the following tag mappings:

[Java]view Plaincopy
    1. < Set name= "roles"  table= "T_user_ Role " >  
    2.      <key column=< Span class= "string" "user_id" />  
    3.      <many-to-many class = "Com.hibernate.Role"  column= "role_id" />  
    4. </set>  

5. Bidirectional one-to-one correlation mapping :

For one-way mapping, you need to add <one-to-one> tags to the idcard. It does not affect. only affects loading.

For example, with:

Bidirectional one-to-one primary key Mapping key mapping code-- Idcard end new additions such as the following label mappings :

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

Two-way one-to-one unique foreign key Mapping key mapping code-- Idcard new additions such as the following label mappings :

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

Note: one-to-one unique Foreign key association bidirectional use <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 bi-directional correlation mapping is primarily to solve the defect rather than the demand-driven problem of a pair of multi-directional associations.

A one-to-many bidirectional association mapping method:

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

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

Note: <key> Labels and <many-to-one> the label added field remains constant. Otherwise, it would create data chaos .

Key Mapping Code:

At one end of the classes is added such as the following label mappings:

[Java]view Plaincopy
    1. < Set name= "students" inverse= "true" >  
    2.        <key column= "Classesid" />  
    3.        <one-to-many class = />  
    4. </set>  

At one end of the student is added such as the following label mappings:

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

Gaze: Inverse Properties

* The inverse property can be used on a one-to-many and many-to-many bidirectional association, and the inverse property defaults to False, indicating that the local side can maintain the relationship, assuming inverse is true. Then this side can not maintain the relationship, will be handed over to one end to maintain the relationship, the end of 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 :

The purpose of the two-way is to both sides can be loaded into the other side. And one-way many-to-many differences are two-way need to add tag mapping at both ends, it is important to note 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.         <many-to-many class =  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. < Span style= "FONT-FAMILY:SIMSUN; Font-size:14px ">       <many-to-many class =  column= " role_id " />  
    4. lt;/set>  

Summarize

For the above seven kinds of correlation mappings, the most important is a one-to-many mapping, because it is closer to our real life. For example: classrooms and students can be a typical one-to-many relationship. And one of the purposes of our software development is to solve some of the recurring problems in life, to put those repeated questions to the computer to help us finish. So as to improve our efficiency. A word: life away from programming, programming more inseparable from life, learning to combine life, understanding more deeply.


SSH Deep Adventures (i) Hibernate architecture (i)-------mapping analysis--seven mapping relationships

Related Article

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.