Hibernate one-to-many and two-way association mapping method and its advantages and disadvantages (to be continued)

Source: Internet
Author: User

The basic principle of a one-to-many correlation mapping and a multi-pair association map implementation is the same, with one end of a foreign key pointing at one end of the foreign key, and the main difference being the difference between the maintenance side.
The difference between them is that they maintain different relationships:

A one-to-many association mapping refers to the data at the end of the load at the same time

Many-to-one association mappings are data that loads one end of the data at the same time


A one-way-to-multi-association mapping maintains relationships at one end, with the following drawbacks:

You must save one end before you can save a side, so when you save a side of the many one side does not know whether there is the corresponding data at the end, so you can only set the maintenance of the relationship field to NULL, if non-empty will not be saved. Because the relationship is maintained at one end, an extra UPDATE statement is issued to maintain the foreign key relationship at one end of the save.


The following example illustrates the mapping method for configuring one-to-many unidirectional and bidirectional associations:

Refer to one end and multi-terminal class structure reference this article: Hibernate configuration of a one-to-many association map and its cascade deletion issues


Configuration of a one-to-many bidirectional association map (refer to the article provided above):

Mapping with <key> tags at one end of the collection:

<set name= "Quesion" inverse= "true" cascade= "delete" ><key column= "Question_type_name"/><one-to-many class= "Question"/></set>

Add a foreign key to the private Questiontype questiontype at one end; Mapping with <many-to-one> Tags:

<many-to-one name= "Questiontype" column= "Question_type_name"/>

A one-to-many bi-directional association mapping generally lets many one end maintain relationships, so it is necessary to set the inverse property to true, which means that this segment does not maintain relationships and is handed over to the other side for maintenance. You can also use the SET CASCADE to cascade operations, such as the Cascade Delete setting for the previous article: cascade= "Delete", which represents deleting all data at one end of the associated multi-side.


One-to-many unidirectional association mappings:

A one-to-many-way association mapping of many end question does not need to join the foreign key private Questiontype questiontype; You only need to map the <key> tag at one end of the collection, and the mapping code is as follows:

<set name= "Quesion" inverse= "true" cascade= "delete" ><key column= "Question_type_name"/><one-to-many class= "Question"/></set>


In addition to the article has special instructions, are it house original articles, reproduced please link the form to indicate the source.
This article link: http://www.itzhai.com/ Hibernate-one-to-many-unidirectional-and-bidirectional-association-mapping-method-and-its-advantages-and-disadvantages.ht ml
Keywords: Hibernate, one-to-many

Further information reference: http://blog.csdn.net/liangoo7/article/details/8070166

Hibernate one-to-many and two-way association mapping method and its advantages and disadvantages (to be continued)

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.