Hibernate many to one single Association

Concept @ h_ 301_ 5@

Basic mapping is to map an entity. Association mapping is to deal with the relationship between multiple entities and map the association relationship to the database. The so-called association relationship has one or more references in the object model@ H_ 301_ 5@

classification

Association relationships are divided into the above seven types, but they can be simplified due to various relationships. For example, many to one and one to many mapping, but the focus is not right@ H_ 301_ 5@

Mapping skills @ h_ 301_ 5@

Mapping skills are the process of writing mapping files. The classic content summarized is divided into four steps. At first glance, it is not particularly easy to understand, but the effect is very good. Let's look at the skills with examples.

(1) Write notes @ h_301_5@

Explanation: before writing the mapping file, write notes and fill in the corresponding content in the place of the question mark. For example: <-- The group attribute represents the many to one relationship between the user and the group -- > @ h_301_5@

(2) Write mapped framework (copy template)

(3) Fill in the blanks

Filling in the blank means filling in the mapped frame information and completing the mapping file.

• name attribute: attribute name (question mark 1 in the comment)

• class attribute: associated entity type (the second question mark in the note) @ h_301_5@

• column attribute:


: Generally, it can be written as attribute name plus ID suffix. If the attribute is group, the column value is written as groupid@ H_ 301_ 5@

○ one to many < key column = ".." >: Copy the column value from the associated counterpart (the counterpart is many to one). @ h_301_5@

○ in many to many, < key column = ".." >: Generally, you can write the name of the cost object plus the ID suffix. If the object name is user, it is written as userid@ H_ 301_ 5@

○ in many to many, < many to many column = ".." >: Generally, it can be written as the name of the associated object plus the ID suffix@ H_ 301_ 5@

(4) Complete @ h_301_5@

Add the mapping file to hibernate hbm. XML, I believe everyone knows why@ H_ 301_ 5@

We use this mapping technique to write mapping files in all subsequent blog posts related to mapping. After understanding it, the speed will be very fast@ H_ 301_ 5@

Many to one association mapping @ h_ 301_ 5@

Mapping principle @ h_ 301_ 5@

Maintain the association relationship at the multi end. Add a foreign key at the multi end to point to the one end. The multi end holds a reference to the one end, that is, a foreign key is added to the "multi" end to point to the "one" end@ H_ 301_ 5@

example

For example, multiple users belong to the same group. Let's analyze this example from the perspective of object model and relationship model, as follows:

As can be seen from the above figure, the object model has directionality. Users can see groups, but not vice versa. Users and groups correspond to a database table, and the aggregation relationship needs a foreign key (groupid). The finally generated table is as follows:

Function: @ h_ 301_ 5@

When I get the user, I can get the user's group directly. Hibernate can automatically load the associated object when accessing the multiple end. For a user, its associated object is a group. @ h_301_5@

The above are the basic principles of many to one association mapping and corresponding examples. Let's take a look at the code: @ h_ 301_ 5@

code

User class

Group class

User. hbm. xml

Group. hbm. xml

The generated table structure is the same as the relationship model analyzed above:

In projects, many to one association mapping is the most common mapping, but it is the simplest mapping relationship in Hibernate association mapping

summary

The above is the hibernate many to one single Association introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>