Cascade and inverse attributes of Hibernate

Source: Internet
Author: User

Cascade: set the level of cascade operations.

Inverse: whether to discard the maintenance of cascade links.

The cascade attribute has the following values:

1. None: ignore other associated objects. Default Value.

2. Save-Update: when the session uses the SAVE (), update (), saveorupdate () method to save or update an object, cascade to save all newly created temporary objects associated with the object, and updates all associated free objects in cascade mode.

3. persist: when the session uses the persist () method to save the current object, it will cascade all newly created temporary objects associated with the object.

4. Merge: when the session's merge () method is used to save the current object, it will cascade all associated free objects.

5. Delete: when the current object is deleted through Delete (), all associated objects are deleted cascade.

6. Lock: when the current free object is added to the session cache through lock (), all free objects are also added to the session cache.

7. Replicate: when the current object is copied through replicate (), all associated objects are copied in a cascade.

8. evict: when an object in the session cache is cleared through evict (), all associated objects are cleared cascade.

9. Refresh: when the current object is refreshed through refresh (), all associated objects are refreshed cascade. (Refresh refers to synchronously updating data in the session cache)

10. ALL: Save-Update (), persist (), merge (), delete (), lock (), replicate (), evict (), and refresh.

11. Delete-orphan: delete all objects that are associated with the current object.

12. All-delete-orphan; when the current object is deleted through Delete (), all associated objects are deleted cascade.

The inverse attribute has the following values:

If inverse is set to false, it is the active party, and if inverse is set to true, the active party is responsible for maintaining the association.

Inverse value: If inverse = true is not set, the parent-child relationship is maintained on both sides.

Inverse conclusion:

1. When ing One-to-multiple bidirectional associations, set the inverse attribute to true on the one side to improve performance.

2. When establishing an association between two objects, you should modify the corresponding attributes at both ends of the Association at the same time:

Customer. getorders (). Add (order );

Order. setcustomer (customer );

This will makeProgramMore robust, improve the independence of the business logic layer, make the business logic layer ProgramCode

It is not affected by the hibernate implementation class. Similarly, when deleting a two-way Association, you should also modify

Attributes of objects at both ends of the association:

Customer. getorders (). Remove (order );

Order. setcustomer (null );

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.