如何从我的审核表中删除条目?
我目前正在与 Hibernate Envers 合作。
如何删除审计表中与我要删除的实体相关的条目?我的实体与其他实体没有关系。
我发现我必须在自定义侦听器的 onPostDelete
方法中执行此操作:
import org.hibernate.envers.event.AuditEventListener;
import org.hibernate.event.PostCollectionRecreateEvent;
import org.hibernate.event.PostDeleteEvent;
import org.hibernate.event.PostInsertEvent;
import org.hibernate.event.PostUpdateEvent;
import org.hibernate.event.PreCollectionRemoveEvent;
import org.hibernate.event.PreCollectionUpdateEvent;
public class MyListener extends AuditEventListener {
...
@Override
public void onPostDelete(PostDeleteEvent arg0) {
// TODO Auto-generated method stub
super.onPostDelete(arg0);
}
...
}
我已经阅读了文档、论坛等很多内容,但我无法弄清楚。也许这是不可能的,我不知道。
以前有人这样做过吗?
I am currently working with Hibernate Envers.
How to delete entries in the audit table related to the entity I want to delete? My entity has no relation with other entities.
I figured out that I have to do that in onPostDelete
method of my custom listener:
import org.hibernate.envers.event.AuditEventListener;
import org.hibernate.event.PostCollectionRecreateEvent;
import org.hibernate.event.PostDeleteEvent;
import org.hibernate.event.PostInsertEvent;
import org.hibernate.event.PostUpdateEvent;
import org.hibernate.event.PreCollectionRemoveEvent;
import org.hibernate.event.PreCollectionUpdateEvent;
public class MyListener extends AuditEventListener {
...
@Override
public void onPostDelete(PostDeleteEvent arg0) {
// TODO Auto-generated method stub
super.onPostDelete(arg0);
}
...
}
I've read the documentation, forums, many things but I can't figure it out. Maybe it's not possible, I don't know.
Has someone ever done this before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,对于那些想知道的人来说,我已经完成了 50%。
感谢 Hibernate Envers 的创建者 Adam Warski,我引用:
但是现在,我还想删除 revinfo 表中与审计表相关的条目。
如果有人有线索,请告诉我。
Ok I'm 50% done with this for those who want to know.
Thanks to the creator of Hibernate Envers, Adam Warski, I quote:
But now, I also would like to delete entries related to audit table in my revinfo table.
If someone has a clue, let me know.
这对我来说完全有效,并且不需要本机查询
This is fully working to me, and no native query required
如果您想按 ID 删除修订版本,可以使用本机查询直接访问 envers 表。有 2 个表包含对修订版本的引用。假设您的审计表使用传统的 _AUD 后缀,您可以通过编程方式找到实体表名称。
以下是用 Kotlin 编写的一些片段:
现在我们有了表名,我们可以删除表中的行。 (将其放入您的 JPA 事务块中,根据需要替换内容,并为您的提供商调整 SQL)。因此,给定 MyEntityClass 和 myRevisionId,我们可以这样做:
如果要通过 revisionID 以外的参数进行删除,只需在entity_AUD 表中查询 revisionIds,然后按照上述方式删除找到的行即可。
请记住,一个 revisionId 可能与多个实体关联,并且所有条目都将在上一个方法中删除。要删除单个实体的修订,您将需要实体的 ID 和实体的关键字段名称。
下面是动态获取字段名称的代码:
If you want to wipe out a revision by ID, you can access the envers table directly using a native query. There are 2 tables that contain references to the revision. Assuming your audit table uses the conventional _AUD suffix, you can find the entity table name programmatically.
Here are some snippets written in Kotlin:
Now that we have the table name, we can remove the rows in the tables. (Put this in your JPA transaction block, replace the content as needed, and adjust the SQL for your provider). So given MyEntityClass and myRevisionId, we can do something like this:
If you want to delete by a parameter other than the revisionID, simply query for the the revisionIds in the entity_AUD table, and then delete the found rows in the mentioned way.
Keep in mind that a revisionId may be associated with more than 1 entity, and all of the entries will be removed in the previous method. To delete the revision for a single entity, you will need the entity's ID and entity's key field name(s).
Here is code for dynamically getting the field name:
审核条目通常只会添加,不会删除,即使相关实体已删除,因此我不认为 Envers API 对此提供支持。
现在,如果您确实想删除已删除实体的条目(这有点违背了审核的目的),您可以稍微延迟一下,而不是在删除时删除条目,而是运行每日本机查询,例如每天晚上。
Audit entries are typically only added, not deleted, even when the related entity is deleted so I don't think that the Envers API provide support for that.
Now, if really you want to remove entries for deleted entities (this kinda defeats the purpose of auditing), you can maybe delay this a bit and instead of removing entries at deletion time, run a daily native query, for example every night.