Summary of Hibernate one to many association mapping learning

One to many association mapping

Mapping principle

The mapping principles of one to many association mapping and many to one association mapping are the same. Both of them add a foreign key at one end of many to point to one end. The association relationship is maintained by multiple ends, but it changes when writing the mapping.

The difference between many to one and one to many

The difference between many to one and one to many is that they maintain different relationships:

(1) Many to one: multiple terminals maintain the relationship between one end. When loading multiple terminals, one end can be loaded.

(2) One to many: one end maintains the relationship between multiple ends. When one end is loaded, multiple ends can be loaded.

classification

One to many unidirectional association mapping

object model

From the object model, we can see that group holds a reference to user. Because it is a one-way Association, the user will be loaded when the data is loaded into the group, but the user does not know the existence of the group.

Let's first look at the entities of group and user, as well as the mapping file.

Group

User

User. hbm. xml

Group. hbm. xml

Generated table structure and test data

shortcoming

1) Because multi terminal users do not know the existence of groups (that is, users do not maintain the relationship with groups), when saving users, the relationship field groupid is null. If this field is set to non empty, data cannot be saved.

2) Because the user does not maintain the relationship, but the group maintains the relationship, the group will issue redundant update statements to ensure that there is a relationship between the group and the user, so that the user corresponding to the users can be loaded when the group is loaded.

One to many bidirectional association mapping

object model

Compared with unidirectional association mapping, the loading direction of objects changes from unidirectional to bidirectional.

Let's look at the group and user entities, mapping files

Group

User

Group. hbm. xml

User. hbm. xml

Generated tables and test data

Mapping method of one to many bidirectional association:

1) The < key > tag is used on the set at one end of the group, and a foreign key is added at the multi end

2) Use the < many to one > tag at the multiple end

Note: the fields added to the < key > tag and the < many to one > tag remain the same, otherwise data confusion will occur.

Inverse attribute:

The inverse attribute can be used for one to many and many to many bidirectional associations. The inverse attribute is false by default. If it is false, it means that the relationship is maintained at the local end. If the inverse attribute is true, the relationship cannot be maintained at the local end, it will be handed over to the other end to maintain the relationship, and the local end will become invalid. Therefore, for one to many association mapping, we usually maintain the relationship at the many end and invalidate the one end, so set reverse to true.

Note: the inverse attribute only affects data storage, that is, persistence.

objective

The purpose of one to many bidirectional association mapping is mainly to solve the defects of one to many unidirectional Association, rather than demand driven.

summary

The above is a summary of Hibernate one to many association mapping learning introduced by Xiaobian. I hope it will be helpful to 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
分享
二维码
< <上一篇
下一篇>>