休眠-恩弗斯->审计/属性的版本控制,但仅当值发生更改时

发布于 2024-10-10 13:48:51 字数 1233 浏览 2 评论 0原文

我对 Hibernate - Envers 有疑问。我有一个域对象,只有一个审核属性 status,可以是数字 0、1、2、3、4、5 之一。

@Entity
public class Item {
    ...
    @Audited
    private int status;
    ... other variables, setter/getter, ...
}

现在,Envers 和 Hibernate 中的一切都正常运行。创建一个新的 Item 对象并将其添加到数据库中,并将一行插入 Item_AUD 数据库表中。

但现在我在更新它时遇到了问题。我在 Hibernate Dao 实现中的更新如下所示:

public void updateItem(Item i) {
    SessionFactory sessionFac = HibernateUtility.getSessionFactory();
    Session s = sessionFac.getCurrentSession();
    Transaction trans = s.beginTransaction();
    s.update(i);
    s.flush();
    trans.commit();
}

每次更新时,都会在我的控制台上打印:

Hibernate: update Item set amount=?, description=?, status=? where id=?
Hibernate: insert into REVINFO (REVTSTMP) values (?)
Hibernate: insert into Item_AUD (REVTYPE, status, id, REV) values (?, ?, ?, ?)

但问题是,我只想在 REVINFO中插入一行>Item_AUD 如果状态编号已更改!

例如:我更改了项目的 description,通过调用 updateItem 执行更新,然后恩弗斯对审计表进行了新的修订。但我不想要这种行为。

我想要的:仅当 status 的值发生更改时,Envers 才应将数据库条目写入审核表中。

但我该怎么做呢?

最好的问候,蒂姆。

I have a problem with Hibernate - Envers. I have a domain object with only one audited attribute status which can be one of the numbers 0,1,2,3,4,5.

@Entity
public class Item {
    ...
    @Audited
    private int status;
    ... other variables, setter/getter, ...
}

Now, everything in Envers and Hibernate is working. Creating a new Item object and add it to the database a row is inserted into theItem_AUD database table.

But now I have a problem with updating it. My update in the Hibernate Dao implementation looks like:

public void updateItem(Item i) {
    SessionFactory sessionFac = HibernateUtility.getSessionFactory();
    Session s = sessionFac.getCurrentSession();
    Transaction trans = s.beginTransaction();
    s.update(i);
    s.flush();
    trans.commit();
}

On every update, this is printed on my console:

Hibernate: update Item set amount=?, description=?, status=? where id=?
Hibernate: insert into REVINFO (REVTSTMP) values (?)
Hibernate: insert into Item_AUD (REVTYPE, status, id, REV) values (?, ?, ?, ?)

But the problem is, that I only want to insert a row into the REVINFO and Item_AUD if the status number has changed!

For example: I change the description of the item, perform the update with calling updateItem and then Envers write a new revision to the auditing tables. But I do not want this behavior.

What I want: only if the value of status is changed, Envers should write database entries into the auditing tables.

But how can I do this?

Best Regards, Tim.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

短叹 2024-10-17 13:48:51

为此,您必须扩展 AuditEventListener< /a> &重写它的方法。

    public class EnversListener extends AuditEventListener {

      @Override
      public void onPostInsert(PostInsertEvent event) {

        Object o = event.getEntity();

        if (o instanceof Item) {

          Item currentItem = (Item) o;
          Item previousItem = findItemById(currentItem.getId());

          if(previousItem != null)
              if (currentItem.getStatus() != previousItem.getStatus()) 
                 super.onPostInsert(event);

        } else {
          super.onPostInsert(event);
        }
      }

      @Override
      public void onPostDelete(PostDeleteEvent event) {
        super.onPostDelete(event);
      }

      @Override
      public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
        super.onPostRecreateCollection(event);
      }

      @Override
      public void onPostUpdate(PostUpdateEvent event) {
          super.onPostUpdate(event);
      }

      @Override
      public void onPreRemoveCollection(PreCollectionRemoveEvent event) {
        super.onPreRemoveCollection(event);
      }

      @Override
      public void onPreUpdateCollection(PreCollectionUpdateEvent event) {
        super.onPreUpdateCollection(event);
      }
}

您可以根据需要在其他重写方法中添加自定义约束。
hibernate.cfg.xml 中监听器类的路径应进行相应配置。

For this, you have to extend AuditEventListener & override its methods.

    public class EnversListener extends AuditEventListener {

      @Override
      public void onPostInsert(PostInsertEvent event) {

        Object o = event.getEntity();

        if (o instanceof Item) {

          Item currentItem = (Item) o;
          Item previousItem = findItemById(currentItem.getId());

          if(previousItem != null)
              if (currentItem.getStatus() != previousItem.getStatus()) 
                 super.onPostInsert(event);

        } else {
          super.onPostInsert(event);
        }
      }

      @Override
      public void onPostDelete(PostDeleteEvent event) {
        super.onPostDelete(event);
      }

      @Override
      public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
        super.onPostRecreateCollection(event);
      }

      @Override
      public void onPostUpdate(PostUpdateEvent event) {
          super.onPostUpdate(event);
      }

      @Override
      public void onPreRemoveCollection(PreCollectionRemoveEvent event) {
        super.onPreRemoveCollection(event);
      }

      @Override
      public void onPreUpdateCollection(PreCollectionUpdateEvent event) {
        super.onPreUpdateCollection(event);
      }
}

You can add custom constraints in the other overridden methods as required.
The path to the listener class inside hibernate.cfg.xml should be configured accordingly.

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