如何在应用程序中实现HibernateEnvers

Hibernate Envers是一个基于Hibernate的审计框架,提供对JPA规范定义的所有映射的审计,支持数据库独立审核。实现包括在实体上添加@Audited注解,配置审计表名,使用RevisionListener创建修订实体。通过AuditReader读取审计实体修订,实现条件审计,并可跟踪修订期间修改的实体名称。
摘要由CSDN通过智能技术生成

Hibernate Envers

技术 (Technology)

Hibernate Envers is the frameworks for auditing entities. As the name suggests Hibernate Envers is developed on top of Hibernate, it will on Hibernate and Hibernate implemented JPA. Hibernate Envers provides easy auditing, versioning solution for entity classes.

Hibernate Envers是用于审核实体的框架。 顾名思义,Hibernate Envers是在Hibernate之上开发的,它将在Hibernate和Hibernate上实现JPA。 Hibernate Envers为实体类提供了简单的审核,版本控制解决方案。

Advantages of Hibernate Envers:

Hibernate Envers的优点:

  • Auditing of all mappings defined by JPA specification.

    审核由JPA规范定义的所有映射。
  • Auditing Hibernate specific mappings which extends the JPA specification.

    审核特定于Hibernate的映射,该映射扩展了JPA规范。
  • Logging data for each revision using revision entity.

    使用修订实体记录每个修订的数据。
  • Querying audited data of an entity and its association.

    查询实体及其关联的审计数据。
  • Database independent auditing.

    数据库独立审核。
  • Querying entity revisions similar to entities.

    查询与实体相似的实体修订。

在应用程序中实现Hibernate Envers (Implementing Hibernate Envers in an application)

Add Hibernate-envers dependency in your build tool configuration file.

在构建工具配置文件中添加Hibernate-envers依赖项。

Annotate entity or entity properties using @Audited annotation, please make sure that entity primary keys will be of immutable class.

使用@Audited批注注释实体或实体属性,请确保实体主键属于不可变类。

修订版 (Revisioning)

Hibernate Envers uses the same concept used source code versioning tools called revision. A revision identifies a collection of changes to entities and their associations for all audited attributes that occurred within the boundary of a transaction. These revisions are global and numeric.

Hibernate Envers使用与称为修订版的源代码版本控制工具相同的概念。 修订版标识了在事务范围内发生的所有已审核属性的实体及其关联更改的集合。 这些修订是全局的和数字的。

Some of the important annotations used in Hibernate Envers

Hibernate Envers中使用的一些重要注释

  • @Audited: we can apply this annotation to either field or a class, when applied to a class, indicates that all of its properties should be audited and when applied to a field, indicates that this field should be audited.

    @Audited:我们可以将此注释应用于字段或类,当应用于类时,指示应审核其所有属性,而应用于字段时,指示应审核此字段。
  • @NotAudited: When applied to a field, indicates that this field should not be audited.

    @NotAudited:应用于字段时,指示不应审核此字段。
  • @AuditingOverride: The annotation is used to override the auditing behavior of a superclass or single property inherited from super class type, or attribute inside an embedded component.

    @AuditingOverride:注释用于覆盖从超类类型继承的超类或单个属性或嵌入式组件内部的属性的审核行为。
  • @AuditingOverrides: The annotation is used to override the auditing behavior for one or more fields (or properties) inside an embedded component.

    @AuditingOverrides:注释用于覆盖嵌入式组件中一个或多个字段(或属性)的审核行为。
  • @AuditTable: By default, Hibernate adds the “_AUD” suffix to the table name of the audited entity. We can define a different table name with the @AuditTable annotation or by configuring a different prefix or suffix in the configuration.

    @AuditTable:默认情况下,Hibernate将“ _AUD”后缀添加到被审计实体的表名中。 我们可以使用@AuditTable批注或通过在配置中配置其他前缀或后缀来定义其他表名。
  • @RevisionNumber: Marks a property which will hold the number of the revision in a revision entity,Values of this property should form a strictly-increasing sequence of numbers. The value of this property won’t be set by Envers. Most of the cases, this should be an auto-generated database-assigned primary id.

    @RevisionNumber:标记将在修订实体中保存修订编号的属性,此属性的值应形成一个严格递增的数字序列。 Envers不会设置此属性的值。 在大多数情况下,这应该是由数据库自动分配的主ID。
  • @RevisionTimestamp: Marks a property which will hold the timestamp of the revision in a revision entity, the value of this property will be automatically set by Envers.

    @RevisionTimestamp:标记一个属性,该属性将在修订实体中保存修订的时间戳,该属性的值将由Envers自动设置。
  • @AuditJoinTable: This annotation is used to audit the entity relations, name of the audit join will be by default to a concatenation of the names of the primary table of the entity owning the association and of the primary table of the entity referenced by the association.

    @AuditJoinTable:此批注用于审核实体关系,默认情况下,审核联接的名称将是拥有关联的实体的主表名称和关联所引用的实体的主表名称的串联。
  • If we are using Secondary Table(s) in entity, then audit tables for them will be generated in the same way (by adding the prefix and suffix). If you wish to overwrite this behavior, you can use the @SecondaryAuditTable and @SecondaryAuditTables annotations.

    如果我们在实体中使用辅助表,则将以相同方式(通过添加前缀和后缀)为它们生成审核表。 如果要覆盖此行为,可以使用@SecondaryAuditTable和@SecondaryAuditTables批注。
  • We can override auditing behavior of some fields/properties inherited from @MappedSuperclass or in an embedded component, you can apply the @AuditOverride annotation on the subtype or usage site of the component.

    我们可以覆盖从@MappedSuperclass或嵌入式组件继承的某些字段/属性的审核行为,您可以在组件的子类型或使用站点上应用@AuditOverride批注。
  • If parent entity is annotated with @Audited and associates entities are not audited, then it will try to fetch the latest associations, if it not found then it will throw exception, in this case we can @NotFound annotation to ignore the exception.

    如果父实体使用@Audited注释,并且未审核关联实体,则它将尝试获取最新的关联,如果未找到,则将引发异常,在这种情况下,我们可以使用@NotFound注释忽略该异常。

Hibernate的Hibernate配置属性 (Hibernate Configuration Properties for Envers)

  • hibernate.envers.audit_table_prefix :String that will be prepended to the name of an audited entity to create the name of the entity and that will hold audit information.

    hibernate.envers.audit_table_prefix:字符串,该字符串将附加在被审核实体的名称之前以创建该实体的名称,并将保留审核信息。
  • hibernate.envers.audit_table_suffix (default: _AUD):String that will be appended to the name of an audited entity to create the name of the entity and that will hold audit information. We can override using @AuditTable annotation for an entity.

    hibernate.envers.audit_table_suffix(默认值:_AUD):将附加到被审核实体名称上的字符串,以创建该实体的名称,并将保留审核信息。 我们可以为实体使用@AuditTable批注覆盖。
  • hibernate.envers.revision_field_name (default: REV):Name of a field in the audit entity that will hold the revision number.

    hibernate.envers.revision_field_name(默认值:REV):审核实体中将保留修订号的字段的名称。
  • hibernate.envers.revision_type_field_name (default: REVTYPE ):Name of a field in the audit entity that will hold the type of the revision.

    hibernate.envers.revision_type_field_name(默认值:REVTYPE):审核实体中将保留修订类型的字段的名称。

HibernateEnvers的用法 (Usage of Hibernate Envers)

  • We need to implement RevisionListener for creating new revision entity, by implementing newRevision(Object revisionEntity)

    我们需要通过实现newRevision(ObjectversionEntity)来实现用于创建新修订版本实体的RevisionListener。
  • error.错误

阅读审核实体 (Reading Audited Entities)

AuditReader is the class to read the entity revisions, it has various methods to read the entity revisions of an entity id. It also allows to get access to lists of revisions associated with an entity type or restricted by a date range. The API also provides a way to get the revision metadata so we can know when a change occurred plus any additional custom attributes we may have stored on the revision entity based on implementation needs.

AuditReader是读取实体修订的类,它具有多种方法来读取实体ID的实体修订。 它还允许访问与实体类型相关联或受日期范围限制的修订列表。 API还提供了一种获取修订元数据的方法,因此我们可以知道何时发生更改,以及根据实现需要我们可能存储在修订实体中的任何其他自定义属性。

Eaxmple:

极端:

AuditReader reader = AuditReaderFactory.get( entityManager );

Event firstRevision = reader.find( Event.class, 2L, 1 );

有条件的审计 (Conditional Auditing)

Hibernate Envers add a series of Hibernate session listeners and it will be auto registered.

Hibernate Envers添加了一系列Hibernate会话侦听器,它将被自动注册。

  • For creating conditional auditing we need to turn off the auto registration of event listeners, hibernate.envers.autoRegisterListeners by setting this property to false.

    为了创建条件审计,我们需要通过将属性设置为false来关闭事件监听器hibernate.envers.autoRegisterListeners的自动注册。
  • update and delete, suppose if we want to add conditional audit for insertion, then create a class which extends EnversPostInsertEventListenerImpl class, add custom logic in it.更新和删除)提供事件侦听器,假设我们要添加条件审计以进行插入,然后创建一个扩展EnversPostInsertEventListenerImpl类的类,并在其中添加自定义逻辑。
  • Create custom implementation of Integrator or extends EnversIntegrator class and use previously created event listener instead of default one.

    创建Integrator的自定义实现或扩展EnversIntegrator类,并使用以前创建的事件侦听器代替默认事件侦听器。
  • Hibernate Envers will use META-INF/services/org.hibernate.integrator.spi.Integrator file for loading Integrator class, create a above file and write the fully qualified name of the class implementing the interface so that it will be loaded instead of default one.

    Hibernate Envers将使用META-INF / services / org.hibernate.integrator.spi.Integrator文件加载Integrator类,创建一个上述文件,并编写实现该接口的类的完全限定名称,以便将其加载而不是默认设置之一。

修订期间修改的跟踪实体名称 (Tracking entity names modified during revisions)

By default, entity types that have been changed in each revision are not being tracked. This implies the necessity to query all tables storing audited data in order to retrieve changes made during specified revision. Envers provides a simple mechanism that creates REVCHANGES table which stores entity names of modified persistent objects. Single record encapsulates the revision identifier (foreign key to REVINFO table) and a string value.

默认情况下,不会跟踪在每个修订版中已更改的实体类型。 这意味着必须查询所有存储审核数据的表,以检索在指定修订期间所做的更改。 Envers提供了一种简单的机制来创建REVCHANGES表,该表存储已修改的持久对象的实体名称。 单个记录封装了修订标识符(REVINFO表的外键)和字符串值。

We can implement this in multiple ways:

我们可以通过多种方式实现:

  • Set org.hibernate.envers.track_entities_changed_in_revision parameter to true. In this case org.hibernate.envers.DefaultTrackingModifiedEntitiesRevisionEntity will be implicitly used as the revision log entity.

    将org.hibernate.envers.track_entities_changed_in_revision参数设置为true。 在这种情况下,org.hibernate.envers.DefaultTrackingModifiedEntitiesRevisionEntity将被隐式用作修订日志实体。
  • Create a custom revision entity that extends org.hibernate.envers.DefaultTrackingModifiedEntitiesRevisionEntity class.

    创建一个扩展org.hibernate.envers.DefaultTrackingModifiedEntitiesRevisionEntity类的自定义修订实体。
  • Mark an appropriate field of a custom revision entity with @org.hibernate.envers.ModifiedEntityNames annotation. The property is required to be of Set<String> type.

    使用@ org.hibernate.envers.ModifiedEntityNames批注标记自定义修订版实体的适当字段。 该属性必须为Set <String>类型。

结论 (Conclusion)

Hibernate Envers is the framework for providing the auditing in an easier way, we can also customize the Hibernate default behavior, easily can read the entity revisions, and also it is database indepent.

Hibernate Envers是用于以更简单的方式提供审核的框架,我们还可以自定义Hibernate的默认行为,轻松读取实体修订,并且它是数据库独立的。

All the content shared in this post is provided to you by the authors of Java application development company. If you wish to share your thoughts regarding the Hibernate Envers , comment below.

Java应用程序开发公司的作者向您提供了本文中共享的所有内容。 如果您想分享有关Hibernate Envers的想法,请在下面评论。

翻译自: https://www.systutorials.com/implement-hibernate-envers-application/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值