"SSH Advanced path" hibernate mapping--multiple-to-one one-way correlation mapping (iv)

Source: Internet
Author: User

"SSH Advanced Path" Hibernate fundamentals (i) , the small part introduces the basic principle of hibernate and its core, using the object-oriented thinking operation relational database.

"SSH Advanced path" hibernate build development environment + Simple example (ii) , Xiao-bian set up the basic hibernate development environment, and made a simple example, the basic principle of it has a rational understanding.

"SSH Advanced Path" hibernate basic mapping (c), we introduced the basic mapping of hibernate (that is, the mapping of an entity) related concepts, and give you the implementation of relevant examples, relatively simple.

In this blog, we begin to introduce an association relationship mapping based on a basic mapping.

Concept

A basic mapping is the mapping of an entity, which is the process of dealing with relationships between multiple entities, mapping association relationships to databases, and so-called association relationships that have one or more references in the object model.

Classification



Correlation is divided into seven of the above, but because of the various relationships between each other, can be simplified, for example: many-to-one and a-to-many mappings, just focus on the wrong angle.

Mapping tips

Mapping skills are small to write the process of mapping files, summary of the classic content, a total of four steps, I see not particularly understandable, but the effect is very good. Let's look at the techniques in the example below.


(1) Write notes

The format is:? attribute, which is the expression of this object with? Of Relationship.

Explanation: Write a comment before writing the map file, and place the question mark in the appropriate content. For example, the:<!--Group property, which expresses the many-to-one relationship between this object (User) and group--

(2) write the frame of the map (copy template)

 

Many-to-one

<many-to-one name= "" class= "" column= ""/>

one-to-many

< Span style= "font-size:18px" > ( set )

<set name=" "

        <key column=" "> </key>

        <one-to-many class= ""/>

</SET>

Many-to-many

< Span style= "font-size:18px" > ( set )

<set name= "" table= "" >

<key column= "" ></key>

<many-to-many class= "" column= ""/>

</set>

(3) Fill in the blanks

Fill in the blanks, is to map the framework information, complete the completion of mapping files.

? Name property: Property name (the 1th question mark in the note)
? Class Property: The associated entity type (the 2nd question mark in the note)
? Column property:
0<many-to-one column= "..." Generally written as a property name plus an ID suffix, such as the attribute group, the column value is written as GroupID.
0 one-to-many <key column= "..." : Copy the column value from the associated opponent (the other party is a many-to-one) mapping.
0-to-many <key column= "..." It is generally possible to write the name of this object plus the ID suffix, if this object is named user, it is written as UserID.
0-to-many <many-to-many column= "..." Generally, it can be written as the name of the associated object plus the ID suffix.

(4) Complete

Add the mapping file to Hibernate.hbm.xml, which I believe everyone knows why.

All of our post-relational mapping posts use this mapping technique to write the mapping file, which is very fast after understanding.

Many-to-one association mappings
 Mapping principle

one end maintains an association, and a foreign key is added at one end of the "many", pointing to the end of "a". The multiple end holds a reference at one end, i.e. a foreign key at one end of the "many", pointing to the end of "a".

Instance

For example, multiple users belong to the same group, we analyze This example from two angles of object model and relational model, as follows:

as you can see, the object model has directionality, which can be seen by the user (group), but not vice versa. Each user and group corresponds to a database table, and the aggregation relationship requires a foreign key (GroupID) to represent the resulting table as follows:

Role:

When I get the user, I can get the user's group directly,hibernate can automatically load the associated object when it accesses the many end . For the user, its associated object is group.

Above are the basic principles of many-to-one correlation mappings, and the corresponding examples, let's look at the code:

Code

User class

public class User {private int id;private String name;private Group group;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Group Getgroup () {return group;} public void Setgroup (group group) {this.group = group;}}

Group class

public class Group {private int id;private String name;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}

User.hbm.xml

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

Group.hbm.xml

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

The resulting table structure is the same as the relational model of the analysis above:

In the project, many-to-one correlation mappings are the most common mappings, but they are the simplest of the relational mappings for hibernate. In the next blog post, we introduce one-to-one correlation mappings, thanks for attention.

"SSH Advanced path" hibernate mapping--multiple-to-one one-way correlation mapping (iv)

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.