Hibernate Envers:审核表中没有用于级联删除的删除条目
我正在使用 Hibernate envers 来跟踪对数据库对象所做的所有更改。这些对象有时通过(单向)父子关系相关。因为我需要应该列出所有已删除对象的查询,所以我依靠 envers 上的审核表来标记已删除对象(*_aud 表中的 revtype 列)。但是,当父对象被删除时,这些条目似乎不会为我的任何子对象创建。
我的对象类如下所示:
@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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否使用@OnDelete并通过Hibernate生成DDL?如果是这样,Hibernate 将在关系上添加“级联删除”,这意味着子级的删除将在 Hibernate 外部进行。因此,Envers(或 Hibernate)将无法访问此事件,无法对此采取行动。
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.