Hibernate,保存对象之前的状态
是否有任何普遍接受的、经过验证的使用休眠的方法可以在数据库中保留实体更改的历史记录?
我们需要跟踪相当多的对象,并且希望能够撤消对对象的更改。
我尝试使用 hibernate 的拦截器,但对象的旧状态仅在执行 merge()
时可用。大多数情况下,我们都会执行 update()
或 saveOrUpdate()
...
我目前让 warp-persist 与 Guice 一起管理会话。
Is there any generally accepted, proven-to-work way using hibernate, that keeps a history of an entitys changes in the database?
We need to keep track of quite some objects and we want to be able to undo changes to objects.
I tried using the interceptors of hibernate, but the old state of the object is only available when doing merge()
. Mostly we do update()
or saveOrUpdate()
though...
I'm currently letting warp-persist together with Guice manage the session.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我已经使用拦截器完成了此操作(基于 创建自己的拦截器空拦截器)。我能够在 onFlushDirty() 中获取所有更改(合并、保存、saveOrUpdate 和删除)。
我使用的 EmptyInterceptor 的方法:
在 onFlushDirty( )我必须查询实体之前的状态:
I've done this with an Interceptor (creating my own based on EmptyInterceptor). I was able to get all changes (merge, save, saveOrUpdate and delete) in onFlushDirty().
The methods of EmptyInterceptor I used:
In onFlushDirty() I had to query the previous state of the entity:
JBoss envers 也许就是您正在寻找的。
JBoss envers is perhaps what you are looking for.
JBoss 开发了一个自动版本控制和审计项目:envers。
我可以推荐使用它,我之前已经手动实现了审核,当您开始处理更复杂的用例时,这可能会变得非常混乱。
There is an automatic versioning and auditing project developed by JBoss: envers.
I can recommend using this, I've implemented auditing by hand before and that can get really messy when you start approaching more complex usecases.
JBoss 的 Hibernate Envers 可能就是您正在寻找的:
Envers 项目旨在启用持久类的轻松审计/版本控制。您所要做的就是使用 @Audited 注释您想要审核的持久类或其某些属性。对于每个审计实体,将创建一个表,该表将保存对该实体所做的更改的历史记录。然后您就可以轻松检索和查询历史数据。
Hibernate Envers from JBoss is probably what you are looking for:
The Envers project aims to enable easy auditing/versioning of persistent classes. All that you have to do is annotate your persistent class or some of its properties, that you want to audit, with @Audited. For each audited entity, a table will be created, which will hold the history of changes made to the entity. You can then retrieve and query historical data without much effort.
我也遇到过同样的问题。一种解决方法是使用先前的状态进行lock(),然后使用新的状态进行merge(),而不是执行update()只是新的状态。
然后 Hibernate 知道之前/之后,并且可以在拦截器/事件侦听器中使用。
HTH。
I've had exactly the same problem. One way round it is to
lock()
with the previous state and thenmerge()
with the new state, rather than doingupdate()
with just the new state.Then hibernate is aware of the before/after and it is available in the interceptors/event listeners.
HTH.