Detailed description of CascadeType usage of Hibernate cascade attribute, cascadecascadetype

Source: Internet
Author: User

Detailed description of CascadeType usage of Hibernate cascade attribute, cascadecascadetype

Detailed explanation of CascadeType usage of Hibernate cascade attributes

Cascade)

Cascade is often used when writing triggers. The trigger is used to ensure that data in the associated tables is synchronously updated when the information of the master table changes. If you use a trigger to modify or delete the associated table records, you must delete the corresponding associated table information. Otherwise, dirty data exists. Therefore, when you delete a primary table, you must also delete the associated table information. In hibernate, you only need to set the cascade attribute value.

Cascade indicates cascade operations. In hibernate, configure the annotation @ OneToOne, @ OneToMany, @ ManyToOne, @ ManyToOne.

For example:

@ManyToOne(cascade = CascadeType.REFRESH, optional = true) @JoinColumn(name = "user_id", unique = false) private UserBaseInfo userBaseInfo; 

Configure multiple cascade operations, for example:

@OneToOne(cascade = {CascadeType.REFRESH,CascadeType.PERSIST,CascadeType.MERGE}, optional = true) @JoinColumn(name = "user_id", unique = false) private UserBaseInfo userBaseInfo; 

CascadeType. PERSIST: cascade addition (also called cascade storage ):The order object is also saved to the objects in items. Corresponding to the presist method of EntityManager.

CascadeType. MERGE: cascade and (cascade update ):If the items attribute is modified, the order object is modified at the same time when it is saved. Corresponds to the merge method of EntityManager.

CascadeType. REMOVE: cascading deletion:Deleting an order object also deletes the objects in items. Corresponding to the remove Method of EntityManager.

CascadeType. REFRESH: cascade REFRESH:You can also obtain the latest items object from the order object. The refresh (object) method corresponding to EntityManager is valid. The latest data in the database is queried again.

CascadeType. ALL:All four of the above are.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.