سؤال

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top