Hibernate Envers:审核表中没有用于级联删除的删除条目

发布于 2024-10-10 09:20:46 字数 636 浏览 7 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2024-10-17 09:20:46

您是否使用@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文