Hibernate establishes one-to-multiple bidirectional associations and hibernate associations

Source: Internet
Author: User

Hibernate establishes one-to-multiple bidirectional associations and hibernate associations

The following content is organized from the second edition of Hibernate

Note: Since it is a bidirectional Association, "One-to-multiple bidirectional Association" and "Multiple-to-one bidirectional Association" are the same thing.


The object is in memory. It is much faster to navigate from one object to another in memory than to query data in the database. However, complex association also causes programming difficulties. Therefore, whether one-way association is established between classes or two-way Association is determined by business needs.

If software applications have a large number of such requirements:

1. query all orders of a given customer.

2. query the customers who issue the order based on the given order.

Based on the above requirements, you may wish to establish one-to-multiple bidirectional associations for the Customer class and Order class. In the previous article, we introduced how Hibernate establishes one-to-one Association (Multiple-to-one association between Order class and Customer class.


To add a one-to-multiple association between the Customer class and the Order class, you must first add an orders attribute of the set type to the Customer class:

<Span style = "font-size: 18px;">/** initialize orders as an instance of the set implementation class when defining the orders set attribute, * This improves program robustness and prevents NullPointerException from being thrown when the application accesses the orders Set whose value is null */private Set orders = new HashSet (); public Set getOrders () {return orders;} public void setOrders (Set orders) {this. orders = orders ;}</span>

Then, add the following content to the Hibernate ing file of the Customer class:

<span style="font-size:18px;"><set    name="orders"    cascade="save-update"    >    <key column="CUSTOMER_ID" />    <one-to-many class="mypack.Order"/></set></span>

The <set> element includes the following attributes.

Name: Set the name of the property of the persistence class to be mapped. Here it is the orders property of the Customer class.

Cascade: When the value is "save-update", it indicates the level of joint guarantee and update.

<Key> child element: Set the foreign key of the parent in the child. Here, it is "mermer_id", indicating that the ORDERS table uses the foreign key CUSTOMER_ID to refer to the CUSTOMER table.

<One-to-sequence> sub-element: indicates that the orders set contains a group of Order objects.

If you want Hibernate to automatically delete the Order object associated with the Customer when deleting the Customer object, you can set the cascade attribute to "delete ".

If you want Hibernate to automatically delete the Order object that is no longer associated with the Customer object, you can set the cascade attribute to "all-delete-orphan ".

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.