Explain the use of CascadeType for Hibernate cascade cascading attributes in detail

  • 2021-12-12 05:18:10
  • OfStack

Explain the use of CascadeType for Hibernate cascade cascading attributes in detail

cascade (cascade)

Cascade is often used when writing triggers. The function of triggers is to ensure that the data in the associated tables are updated synchronously when the information of the main control table changes. If the trigger to modify or delete associated table records, must delete the corresponding associated table information, otherwise, there will be dirty data. Therefore, it is appropriate to delete the information of the associated table as well as the main table. In hibernate, you only need to set the cascade attribute value.

cascade represents cascading operations, and the properties in the hibernate configuration annotations @ OneToOne, @ OneToMany, @ ManyToMany, @ ManyToOne.

For example:


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

Configure multiple cascades, such as:


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

CascadeType. PERSIST: Cascade Add (also known as Cascade Save): Save order objects as well as items objects. presist method corresponding to EntityManager.

CascadeType. MERGE: Cascade Union (Cascade Update): If the items property is modified, then the order object is saved while modifying the object in items. merge method corresponding to EntityManager.

CascadeType. REMOVE: Cascade deletion: Deletion of order objects also deletes objects in items. remove method corresponding to EntityManager.

CascadeType. REFRESH: Cascade refresh: Retrieves the order object and also retrieves the latest items object. The refresh (object) method corresponding to EntityManager is effective. It will re-query the latest data in the database.

CascadeType. ALL: All of the above four.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: