Object relational mapping in Hibernate

The essence of Hibernate is object relational mapping (ObjectRelational mapping). ORM saves object data to the database. In the past, we used to operate on relational tables and perform tasks such as adding, deleting, modifying and querying. Now we no longer operate on relational tables, but directly operate on objects. ORM mapping files in Hibernate usually use. Hbm.xml as the suffix. Using this mapping file is not only easy to read, but also manual You can also use some tools to generate mapping documents. The mapping in Hibernate will be introduced below.

Hibernate mapping classification, as shown in the following figure.

1 basic class mapping

Create corresponding tables according to entity classes. This simple relationship is the basic mapping of hibernate.

User1 entity class code is as follows:

User1. hbm. The XML Mapping file is as follows:

Through user1 hbm. The XML Mapping File converts the user1 object into a table t in a relational database_ user1。 The converted results are as follows:

2 object relational mapping

2.1 many to one association mapping (unidirectional)

For example, the relationship between users and groups is many to one, and multiple users correspond to a group.

Mapping entities to tables and corresponding entities to tables. The corresponding attributes are mapped to table fields.

Many to one association mapping is to maintain Association fields on the many side. In our example, it is to maintain relationship fields on the user side.

User. hbm. XML file.

Group. hbm. XML file.

The code we look at here is * hbm. MLX code, because for the association between classes, when implementing, one class acts as a private member of another class. We all understand this when learning UML modeling. Here we mainly look at the m of ORM, that is * hbm. XML file.

2.2 one to one association mapping

One-to-one association mapping is common in real life, such as the relationship between a person and his home address. Through the object of person, we can find the content related to his home address.

2.2. 1 one to one mapping (one-way primary key Association)

One way one-to-one primary key association depends on their equal primary keys. IDcard can be seen from person, that is, t_ Take the primary key in IDcard as t_ Pseron's primary key.

XML file:

A one-to-one relationship is defined through the one-to-one element.

2.2. 2 one to one mapping (bidirectional primary key Association)

The difference between one-to-one bidirectional primary key Association and one-to-one unidirectional primary key association is that one-to-one unidirectional primary key association can see IDcard on the person side, but IDcard cannot see the person side. The two-way association is that you can also see person on the IDcard side, that is, not only in person hbm. The < one-to-one > tag is added to the XML, and at the same time, the IDcard hbm. Add the < one-to-one > tag to the XML file. The code is shown below.

2.2. 3 one to one mapping (one-way unique foreign key Association)

One to one one one-way unique foreign key association is a special case of many to one association. Limiting one end of many to one is one to one unique foreign key Association. Like many to one, add the tag of the other end at one end and use the < many to one > tag. Through unique = "true", this restricts the multi end to one. Code first.

IdCard. hbm. xml

Person. hbm. xml

The figure is as follows:

In t_ A foreign key field IDcard is added to the pserson end, which limits the uniqueness of IDcard to one-to-one unique foreign key Association.

2.2. 4 one to one mapping (bidirectional unique foreign key Association)

One-to-one unique foreign key one-way Association, we have learned that two-way Association, in turn, is to add it at the end where there is no foreign key.

Our IDcard hbm. The < one-to-one > tag is used in XML.

And person hbm. XML is the same as one-way Association of one-to-one unique foreign key.

It can be concluded from the above that for one-to-one association mapping, the one-way and two-way table structures generated by primary key Association and unique foreign key association are the same, except that they are different when loaded. That is, compared with one-to-one bidirectional association, one-to-one unidirectional association only changes the loading of one-to-one association mapping, but does not change the storage.

2.3 one to many association mapping

2.3. 1 one to many association mapping (unidirectional)

We introduced many to one above. Let's look at one to many in turn. Isn't it many to one? Do you still need to do different mapping? What's the difference? The principle of one to many and many to one mapping is the same. The storage is the same, that is, the generated database tables are the same. The difference between them is that they maintain different relationships.

The difference between them is that they maintain different relationships

*The relationship of many to one maintenance is the relationship of many to one. With this relationship, one can be loaded when there are many loads.

*The relationship of one to many maintenance is one to many. With this relationship, you can load many when loading one.

The code is shown below.

Class. hbm. xml

Students. hbm. xml

Students can be seen from the class. It is the class that maintains the relationship, not the students. Students do not know which class they are. Therefore, when storing students, the class code does not know. In order to update which class the students belong to, many update statements should be issued to tell the students which class they belong to. When we set classesid not null = "true", the data cannot be saved. The solution is to change to two-way association mapping.

2.3. 2 one to many association mapping (bidirectional)

In order to solve the possible problems of one to many and one-way, we adopt two-way one to many, and each party can maintain the other party.

One to many bidirectional association mapping method:

*The < key > tag is used on the set at one end of the group, and a foreign key is added at the other end of the group.

*Use the < many to one > label at the multi end

!~ Note that the fields added to the < key > tag and the < many to one > tag are consistent, otherwise data confusion will occur.

The code is shown below.

Note: the inverse attribute

1. Reverse Chinese means opposite, reverse. In Hibernate, reverse can be used for one to many and many to many bidirectional associations. Reverse is false by default. When it is false, it means that the relationship can be maintained at the local end. If reverse is true, the relationship cannot be maintained at the local end and will be handed over to the other end to maintain the relationship. The local end will fail. Therefore, in one to many association mapping, we usually maintain the relationship at the many end and make the one end fail.

2. Inverse is the reversal of the control direction, which only affects the storage.

Comparing one-to-many unidirectional and bidirectional mapping, there is no difference in storage structure, but from the perspective of configuration file, one-to-many bidirectional is better than one-to-many unidirectional. In the configuration file of one-to-many bidirectional association, there are < many to one > related configurations on the configuration file at the multiple end, that is, many to one mapping is guaranteed.

2.4 many to many association mapping

2.4. 1 many to many association mapping (unidirectional)

For many to many object relational mapping, you need to add a new table to complete the basic mapping. As shown in the figure below.

code.

Role. hbm. xml

User. hbm. xml

2.4. 2 many to many association mapping (bidirectional)

Bidirectional many to many object relationship mapping is that both ends can load each other, and both directions need to be labeled. Note:

*The generated intermediate table name must be the same

*The fields in the generated intermediate table must be the same

The code is shown below.

Role. hbm. xml

. User. hbm. xml

Difference: there is no difference between unidirectional many to many and bidirectional many to many storage structures, but their mapping files are different, and the loading process is different.

3 Relationship Mapping summary

To sum up, it can be seen that the same type of mapping, whether one-way or two-way, has the same storage structure. The reason why the mapping files are different is that they are loaded differently (during addition, deletion and modification).

Whether it is many to one, one to many, one to one or many to one, a to B, a is the active party. A actively wants to know the situation of B, so set B to end a. Two way, that is, a to B, a wants to know B's information, and B also wants to know a's information, so it is necessary to set a to B at the same time

The above is the object relationship mapping in Hibernate 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
分享
二维码
< <上一篇
下一篇>>