简体   繁体   中英

Hibernate Envers: No delete entry in audit table for cascading deletes

I'm using Hibernate envers to track all changes made to my database objects. These objects are sometimes related by a (uni-directional) parent-child relationship. Because I need queries that are supposed to list all deleted objects, I'm relying on the audit table on envers to mark deleted objects (revtype column in *_aud table). However, these entries don't seem to be created for any of my child objects when their parent object gets deleted.

My object class looks like this:

@Entity
@Audited
public class MyClass {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(nullable = false, unique = false, length = 1024)
private String name;

    // An object can have exactly one parent, but multiple children
@ManyToOne
@OnDelete(action = OnDeleteAction.CASCADE)
private MyClass parent;

}

I'm suspecting it has something to do with the cascade delete action that somehow bypasses hibernate envers. How can I achieve that the entries in the audit table for the children objects are created while still making sure that all children get automatically deleted by the database when the referenced parent is deleted?

Are you using @OnDelete and generating the DDL via Hibernate? If so, Hibernate will add a "on cascade delete" on the relationship, meaning that the deletion of the children will take place outside Hibernate. So, Envers (or Hibernate) won't have access to this event, failing to act on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM