Envers 与 Hibernate 拦截器,onDelete
我正在尝试实现一个简单的基于 Envers + 拦截器的方法来审计我的 Hibernate 实体。为此,按照典型方法,有一个包含所有审核属性的 BaseEntity
:
BaseEntity{
createdOn;
createdBy;
updatedOn;
updatedBy;
}
在 AuditInterceptor
中,我重写了 onFlushDirty
和 >onSave
方法来操作上述属性,具体取决于它是实体保存还是更新。保存和更新一切都按预期进行。
我面临的问题是:我还需要一种方法来操作删除操作的上述属性。覆盖 onDelete
确实为我提供了 state[]
数组的句柄,但修改它不会反映在 _AUD(审核)表中。简而言之:我也希望能够审核审计表中的删除操作。
有没有解决方法,或者我错过了什么?
I'm trying to implement a simple Envers + interceptor based approach to audit my Hibernate entities. For this, as per the typical approach, there's a BaseEntity
with all the audit properties:
BaseEntity{
createdOn;
createdBy;
updatedOn;
updatedBy;
}
In the AuditInterceptor
, I have overridden onFlushDirty
and onSave
methods to manipulate the above properties depending upon whether it's an entity Save or Update. Everything works as expected for Save and Update.
The issue that I'm facing is: I need a way to be able to manipulate the above properties for Delete operations as well. Overriding onDelete
does give me a handle to the state[]
array, but modifying it doesn't reflect in the _AUD (audit) tables. In a nutshell: I want to be able to audit delete operations in the audit tables as well.
Is there a workaround for this, or am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不需要实现 onSave、onFlushDirty 方法。
您只需要实现 RevisionListener 接口即可。如果您遵循此处的模式,您也不需要将这些属性(createdOn 等)添加到您的实体中(除非您需要它们进行正常操作)。
请参阅http://docs.jboss.org/hibernate /envers/3.6/reference/en-US/html_single/#revisionlog 了解更多详细信息。在此示例中,他们使用 Seam 来获取登录用户。
Envers 允许您将审核与实体分开,从而保持正常模型的整洁并最大限度地减少审核所需的代码。
I don't think you need to implement the onSave, onFlushDirty methods.
You just need to implement the RevisionListener interface. If you follow the pattern here you don't need to add those properties (createdOn etc) to your entities either (unless you need them for normal operations).
See http://docs.jboss.org/hibernate/envers/3.6/reference/en-US/html_single/#revisionlog for more details. In the example here they are using Seam to get the logged in user.
Envers allow you to separate your auditing from your entities which keeps your normal model clean and minimises the code required to do the auditing.