Inverse and cascade attributes in hibernate

Source: Internet
Author: User

Assume that the customer and orders establish a one-to-multiple bidirectional Association.

 

Inverse attributes:

// Load the Persistent Object

Customer customer = (customer) Session. Load (customer. Class, new INTEGER (2 ));

Orders order = (orders) Session. Load (orders. Class, new INTEGER (2 ));

// Establish Association

Order. setcustomer (customer );

Customer. getorders (). Add (order );

Because hiberante automatically clears persistent objects in the cache and synchronously updates the database based on changes in the Persistence State, although the above Code only modifies

One record in the orders table will still execute two update statements because the state of the Persistent object in the memory has changed twice.

This redundant SQL statement affects the performance. To solve this problem, set the value of the inverse attribute to true.

Summary:

When a one-to-multiple bidirectional Association is specified, the one-party inverse attribute value is set to true.

When establishing two-way associations between two objects, you must modify the attributes at both ends of the Association at the same time.

 

Cascade attributes:

When the property value is

Save-Update: indicates that when the current object is saved or updated, it is saved or updated in cascade mode.

Delete: (cascade delete) cascade Delete the associated objects.

All-delete-orphan: automatically deletes sub-objects that are no longer associated with the parent object. In the preceding two cases, the above two functions are executed.

It can be said that it is a fully automated property value.

In general, when there is a parent-child relationship between the two parties, you can set the cascade attribute value of the parent to all-delete-orphan.

Parent-child relationship: the parent controls the lifecycle of the child. The child object must be associated with a parent object. The child object cannot exist in isolation.

 

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.