记录 Java Web 应用程序中的实体更改

发布于 2024-09-24 10:27:06 字数 346 浏览 0 评论 0原文

我们对项目有一个要求,我们需要维护对应用程序中某些实体所做的更改历史记录。该应用程序是一个基于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 技术交流群。

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

发布评论

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

评论(2

丘比特射中我 2024-10-01 10:27:06

JBoss Envers 支持试听和版本控制 - 看看吧。

JBoss Envers supports audition and versioning - take a look.

水溶 2024-10-01 10:27:06

因为你说

所有编辑都没有相应的不同方法...许多编辑发生在单个 java 方法中

即使您的方法只接收一个参数作为参数并且您可以检索其关系,AOP 仍然可以是一个不错的选择主意。我强烈建议您查看这篇好文章

Hibernate 文档它本身说:

Hibernate 拦截器允许应用程序在保存、更新、删除或加载持久对象之前检查和/或操作持久对象的属性。 此功能的一个可能用途是跟踪审核信息。

但它也说

确保您不存储特定于会话的状态,因为多个会话可能会同时使用此拦截器

。如果您使用 Spring 管理的事务,则可以使用 sessionFactory 获取附加到当前线程的会话。 getCurrentSession();

public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
    if(entity instanceof SomeEntity) {
        Session session = sessionFactory.getCurrentSession();

请参阅getCurrentSession() 文档

如果没有,您应该创建一个本地 Hibernate 拦截器(附加到打开的 Session)而不是全局拦截器(附加到 SessionFactory) ),然后设置其会话

AuditableInterceptor interceptor = new AuditableInterceptor()
Session session = sessionFactory.openSession(interceptor);

/**
  * do it before processing anything if you use local interceptor
  */ 
interceptor.setSession(session);

关于触发器,这取决于其他问题,例如数据库管理(您具有数据库管理访问权限。如果没有,与 DBA 交谈是您最好的选择)。

Because you said

All edits do not have their corresponding different methods... many edits happen in a single java Method

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:

Hibernate Interceptor allows the application to inspect and/or manipulate properties of a persistent object before it is saved, updated, deleted or loaded. One possible use for this is to track auditing information.

But it also says

Ensure that you do not store sessionspecific states, since multiple sessions will use this interceptor potentially concurrently

If you use a Spring managed Transaction, you can get the session atached to the current Thread by using sessionFactory.getCurrentSession();

public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
    if(entity instanceof SomeEntity) {
        Session session = 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

AuditableInterceptor interceptor = new AuditableInterceptor()
Session session = sessionFactory.openSession(interceptor);

/**
  * do it before processing anything if you use local interceptor
  */ 
interceptor.setSession(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).

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