Hibernate series (2) Multiple-to-one, one-to-one, one-to-many, and many-to-many configuration methods

Source: Internet
Author: User

Hibernate series (2) Multiple-to-one, one-to-one, one-to-many, and many-to-many configuration methods

This is a few of my experiences in the recent case input and java project: I have a profound understanding of the project. I would like to share with you!


Hihernate one-to-Multiple Association ing (unidirectional Classes -----> Student)

One-to-Multiple Association ing utilizes the multi-to-one association ing principle

Multi-to-one association ing: Add a foreign key to multiple ends to point to one end, and maintain the relationship between multiple links to one
One-to-Multiple Association ing: Add a foreign key at one end to point to one end, and maintain the relationship between one and multiple

That is to say, the one-to-many and multi-to-one ing policies are the same, but they have different site angles.

Disadvantages of One-end relationship maintenance:
* If the classesid field in the t_student table is set to a non-empty value, it cannot be saved.
* Because the relationship is not maintained at the student end, student does not know which class it belongs,
Therefore, redundant update statements are required to update the relationship.




Hihernate one-to-Multiple Association ing (bidirectional Classes <-----> Student)

One-to-multiple bidirectional Association ing:
* Used on a one-end set Add a foreign key to the target table to point to one-end
* Use at multiple ends

Note: The foreign key field specified by the tag must be The specified foreign key fields are the same; otherwise, the referenced field is incorrect.

If one-to-multiple associations are maintained at one end, hibernate will issue redundant udpate statements.
To maintain the Association

About inverse attributes:
Inverse is mainly used for one-to-many and multi-to-many bidirectional Association. inverse can be set to set tags. ,
The default value of inverse is false. Therefore, we can maintain associations between one end and multiple ends,
If the value of inverse is set to true, we can only maintain the association from one end.

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

Inverse and cascade
* Inverse is the control direction of the association.
* Chain reaction in cascade operations




Hibernate one-to-one primary key Association ing (one-way Association Person ----> IdCard)

One-to-one primary key Association ing: ensure that the IDs of the two object objects are the same, so that redundant fields are not created.

Specific ing:




IdCard







Hibernate one-to-one primary key Association ing (two-way Association Person <----> IdCard)

You need to add it to the idcard ing File The tag points to person, indicating how hibernate loads person
Load by primary key by default




Hibernate one-to-one unique foreign key Association ing (one-way Association Person ----> IdCard)

A unique foreign key Association ing is a special case of multi-to-one association ing.

Available Label, specify unique = true for multiple ends, which limits the multiple ends to one
Ing one-to-one unique foreign key association using this method




Hibernate one-to-one unique foreign key Association ing (two-way Association Person <----> IdCard)

One-to-one unique foreign key is associated with two-way, and needs to be added at the other end (idcard) Tag, indicating how to load hibernate
The associated object is loaded by default based on the primary key. In the person ing of the foreign key Association, because the two entities use the foreign key maintained by the person,
Therefore, the primary key cannot be specified to load the person, but must be loaded according to the foreign key of the person, so the following ing method is used:




Hibernate multi-to-one association ing

Nature of association ing:
* Ing an association to a database. The so-called Association is one or more references of the Object Model in the memory.

A foreign key is added to multiple ends to point to one end.
If this attribute is ignored, the default foreign key is consistent with the object attribute.

Tag definition example:
*

What does cascade mean?
* It is a chained operation of objects.



Hibernate multi-to-Multi-Association ing (one-way User ----> Role)

Specific ing method:







Hibernate multi-to-Multi-Association ing (two-way User <----> Role)

Ing method:




The table attribute value must be consistent with the table attribute value in one-way join.
The attribute value of column must be associated The column attribute values in the label are consistent.
In The column attribute value in must be in one-way Association. The column attribute values of the label are consistent.


Summary

If you encounter many problems, check and calm down and think about it. You can analyze the problem from the object-oriented perspective. hibernate requires a lot of experience, and the project continues to work hard ......

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.