使用Hibernate Envers手动设置修订日期 [英] Setting the revision date manually with Hibernate Envers

查看:61
本文介绍了使用Hibernate Envers手动设置修订日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知, Hibernate Envers 在创建,更改或删除时存储修订用@Audited注释的对象.

As far as I know, Hibernate Envers stores a revision when you create, change or delete an object annotated with @Audited.

Envers自动将修订日期设置为当前时间.可以手动设置此时间吗?

Envers sets automatically the revision date to the current time. Is it possibile to set this time manually?

我需要使用它来处理时间收集,其中数据具有有效时间,需要手动设置.

I'd need this to handle a temporal collection where data has valid time, which I'd need to set manually.

推荐答案

可以,但是乍一看似乎并不直观.

You can, but it may not seem intuitive at first.

当Envers创建其修订实体实例时,会发生几件事.

When Envers creates its revision-entity instance, several things happen.

  1. @RevisionTimestamp带注释的属性是使用当前时间设置的.
  2. 调用可选的RevisionListener并提供修订实体实例.
  1. The @RevisionTimestamp annotated property is set with the current time.
  2. The optional RevisionListener is called and supplied the revision-entity instance.

您可以通过两种方式指定RevisionListener,这实际上取决于您当前是否提供自定义修订实体实例或使用Envers根据您的设置解析的实例.

You can specify a RevisionListener in two ways and this really depends on whether or not your currently supplying a custom revision-entity instance or using the instance Envers resolves based on your setup.

在这种情况下,可以通过将RevisionListener设置在实体类的@RevisionEntity类注释上来指定它.

In this situation, you can specifying your RevisionListener by setting it on the @RevisionEntity class annotation on the entity class.

@RevisionEntity(YourCustomRevisionListener.class)
public class CustomRevisionEntity {
  ...
}

通过配置提供RevisionListener

在这种情况下,您需要通过hibernate.properties文件或在您显式设置hibernate配置属性的代码中为Hibernate添加附加的引导程序配置属性:

Supplying RevisionListener via configuration

In this situation, you'll want add an additional bootstrap configuration property for Hibernate, either via your hibernate.properties file or in your code where you explicitly set the hibernate configuration properties:

org.hibernate.envers.revision_listener=com.company.envers.YourCustomRevisionListener

无论采用哪种方法,都将实现侦听器的协定,并根据应用程序所需的任何规则显式设置时间戳记值:

Regardless of which approach you take, you'll then implement the listener's contract and explicitly set the timestamp value based on whatever rules your application needs:

public class YourCustomRevisionListener implements RevisionListener {
  @Override
  public void newRevision(Object revisionEntity) {
    // I am going to assume here you're using a custom revision entity.
    // If you are not, you'll need to cast it to the appropriate class implementation.
    final CustomRevisionEntity revisionEntityImpl = (CustomRevisionEntity) revisionEntity;
    revisionEntityImpl.setTimestamp( resolveValidTimestampValue() );
  }

  private long resolveValidTimestampValue() {
    // implement your logic here.
  }
}

这里有几个警告.如果您需要从应用程序空间中的某个bean解析值,则需要确定以下哪一项适用于您:

There are a couple caveats here. If you need to resolve the value from some bean in your application space, you'll need to determine which of the following applies to you:

在这种情况下,您必须使用ThreadLocal变量的旧方法来传递应用程序作用域实例/值以访问侦听器内部的实例/值.

In this case you'll have to use the legacy approach of ThreadLocal variables to pass application scope instances/values to access those inside the listener.

在这种情况下,您可以使用CDI注入简单地注入CDI bean,因为我们添加了在创建侦听器实例时自动解析CDI bean的支持.

In this case you can simply inject the CDI bean using CDI's injection since we added support to automatically resolve CDI beans when we create the listener instance.

您可以使用Spring的注入注释将Spring bean直接注入到侦听器中,就像侦听器是spring-bean一样.

You can inject spring beans directly into the listener using Spring's injection annotations just like the listener were a spring-bean.

在这种情况下,您将需要使用ThreadLocal变量的传统方法,因为Spring Framework直到5.1才添加对将bean注入Hibernate bean的支持.

In this case, you'll need to use the legacy approach of ThreadLocal variables since Spring Framework didn't add support for injecting beans into Hibernate beans until 5.1.

这篇关于使用Hibernate Envers手动设置修订日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆