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!