记录 Java Web 应用程序中的实体更改
我们对项目有一个要求,我们需要维护对应用程序中某些实体所做的更改历史记录。该应用程序是一个基于Struts、Spring 和Hibernate 的Java Web 应用程序。在这个案例中使用了什么样的方法?
- 各个表上的触发器是一种想法,但它们不容易维护?也许它们也不应该成为事务的一部分(如果触发器失败也没关系,但实体更新事务不应该失败)。
- 为此使用 AoP,因为它是一个横切关注点,但必须非常细粒度,就像在实体更改时仅捕获值一样。 (所有编辑都没有相应的不同方法...许多编辑发生在单个 java 方法中)。
- 使用 Hibernate 事件监听器。
还有其他方法可以进行此类活动吗?
We have a requirement on our project, where we need to maintain a sort of history of changes that are made to certain Entities in the Application. The the Application is a Java Web App based on Struts, Spring and Hibernate. What sort of approaches have been used in this case ?
- Triggers on the respective tables is one idea but they are not easily maintainable ? and perhaps also they should not be part of transactions (its ok if the triggers fail, but the entity update transactions should not fail ).
- Use AoP for this since its a cross-cutting concern, but has to be really granular, as in capturing only the values when the entity changes. (All edits don't have their corresponding different methods... many edits happen in a single java Method).
- Use Hibernate Event Listeners.
Are there any other approaches for doing this sort of activity ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JBoss Envers 支持试听和版本控制 - 看看吧。
JBoss Envers supports audition and versioning - take a look.
因为你说
即使您的方法只接收一个参数作为参数并且您可以检索其关系,AOP 仍然可以是一个不错的选择主意。我强烈建议您查看这篇好文章
Hibernate 文档它本身说:
但它也说
。如果您使用 Spring 管理的事务,则可以使用 sessionFactory 获取附加到当前线程的会话。 getCurrentSession();
请参阅getCurrentSession() 文档
如果没有,您应该创建一个本地 Hibernate 拦截器(附加到打开的 Session)而不是全局拦截器(附加到 SessionFactory) ),然后设置其会话
关于触发器,这取决于其他问题,例如数据库管理(您具有数据库管理访问权限。如果没有,与 DBA 交谈是您最好的选择)。
Because you said
Even if your method just receive one argument as parameter and you can retrieve its relationships, AOP still can be a nice idea. I strongly advice you to see this nice article
The Hibernate documentation itself says:
But it also says
If you use a Spring managed Transaction, you can get the session atached to the current Thread by using sessionFactory.getCurrentSession();
See getCurrentSession() documentation
If not, you should create a local Hibernate interceptor (attached to the opened Session) instead of a global interceptor(attached to the SessionFactory), and then, set up its session
About Triggers, it depends on other issues like DB administration (you have DB administration access. If not, Talk to DBA is your best bet).