如何使用 spring 数据 envers 查找实体的所有修订? [英] How to find all revisions for an entity using spring data envers?

查看:32
本文介绍了如何使用 spring 数据 envers 查找实体的所有修订?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring Boot 应用程序中使用 spring-data-envers.我可以成功地记录对我的实体的审计.

I am using spring-data-envers in my spring boot application. I can successfully log the audits on my entities.

现在,我需要在 UI 中向用户显示审计数据.就像会有搜索表单,用户可以在其中选择他想要查看审核日志的持续时间和实体.

Now, I need to show audited data to the user in UI. Like there will be search form where user can select duration and entity for which he wants to see audit logs.

string-data-envers提供的RevisionRepository只有如下三种方法.

RevisionRepository provided by string-data-envers has only three methods as follows.

@NoRepositoryBean
public interface RevisionRepository<T, ID extends Serializable, N extends Number & Comparable<N>> {

    /**
     * Returns the revision of the entity it was last changed in.
     * 
     * @param id must not be {@literal null}.
     * @return
     */
    Revision<N, T> findLastChangeRevision(ID id);

    /**
     * Returns all {@link Revisions} of an entity with the given id.
     * 
     * @param id must not be {@literal null}.
     * @return
     */
    Revisions<N, T> findRevisions(ID id);

    /**
     * Returns a {@link Page} of revisions for the entity with the given id.
     * 
     * @param id must not be {@literal null}.
     * @param pageable
     * @return
     */
    Page<Revision<N, T>> findRevisions(ID id, Pageable pageable);
}

如何编写自定义查询以获取特定用户在两个日期之间对实体的所有修订.

How do I write a custom query to get all the revisions for an entity between two dates by a particular user.

请注意,我在 user_rev_entity 表中添加了额外的列,用于存储用户 ID 和修改日期.如果我将此表与 entity_aud 表连接,我可以获得结果.

Note that, I have added additional columns to the user_rev_entity table where I store user id and modified date. If I join this table with the entity_aud table I can get the results.

以下是我的审计表的脚本.

Below are the scripts of my Audit tables.

CREATE TABLE `user_rev_entity` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `timestamp` bigint(20) NOT NULL,
  `created_by` bigint(20) NOT NULL,
  `created_date` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1

CREATE TABLE `revchanges` (
  `rev` int(11) NOT NULL,
  `entityname` varchar(255) DEFAULT NULL,
  KEY `FK_et6b2lrkqkab5mhvxkv861n8h` (`rev`),
  CONSTRAINT `FK_et6b2lrkqkab5mhvxkv861n8h` FOREIGN KEY (`rev`) REFERENCES `user_rev_entity` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE `roles_aud` (
  `role_id` bigint(20) NOT NULL,
  `rev` int(11) NOT NULL,
  `revtype` tinyint(4) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  `description_mod` bit(1) DEFAULT NULL,
  `display_name` varchar(255) DEFAULT NULL,
  `display_name_mod` bit(1) DEFAULT NULL,
  `is_enabled` bit(1) DEFAULT NULL,
  `enabled_mod` bit(1) DEFAULT NULL,
  `title` varchar(255) DEFAULT NULL,
  `title_mod` bit(1) DEFAULT NULL,
  PRIMARY KEY (`role_id`,`rev`),
  KEY `FK_pkqm51vsc35w2axvnns4bpas9` (`rev`),
  CONSTRAINT `FK_pkqm51vsc35w2axvnns4bpas9` FOREIGN KEY (`rev`) REFERENCES `user_rev_entity` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

因此,基本上我正在寻找特定用户在特定时间为实体(例如角色)所做的所有更改.

So, basically I am looking for all the changes made by a particular user during particular time for an entity say Role.

将会有更多这样的实体.

There will be lot more such entities.

推荐答案

你看过 AuditReaderFactory 和 AuditReader?

Have you looked at AuditReaderFactory and AuditReader?

查看 AuditReader 的文档 创建查询:

Check out the documentation for AuditReader's createQuery:

与此 AuditReader 实例关联的查询创建者,可以使用它创建查询并稍后执行.

A query creator, associated with this AuditReader instance, with which queries can be created and later executed.

这会返回一个 AuditQueryCreator,您可以使用它来创建这样的查询:

That returns an AuditQueryCreator which you can use to create a query like so:

AuditQuery query = getAuditReader().createQuery()
.forRevisionsOfEntity(MyEntity.class, false, true);

对于RevisionsOfEntity 有几个选项,参见AuditQueryCreator 文档.

There are a couple of options for forRevisionsOfEntity, see AuditQueryCreator documentation.

查询应该允许您使用 审计准则

Hibernate 包含与此相关的文档:http://docs.jboss.org/hibernate/orm/5.0/userGuide/en-US/html_single/#revisions-of-entity

Hibernate envers documentation relating to this: http://docs.jboss.org/hibernate/orm/5.0/userGuide/en-US/html_single/#revisions-of-entity

您可以使用与前一个相同的方式向此查询添加约束.还有一些额外的可能性:

You can add constraints to this query in the same way as to the previous one. There are some additional possibilities:

  1. 使用 AuditEntity.revisionNumber() 可以指定约束,修订号的预测和顺序,其中审计实体被修改
  2. 同样,使用您可以指定的 AuditEntity.revisionProperty(propertyName)修订版属性的约束、预测和顺序实体,对应于被审计实体的修订被修改了
  3. AuditEntity.revisionType() 可让您按上述方式访问修订的类型(ADD、MOD、DEL).

编辑我尝试了一个实际的解决方案.我几乎没有 envers 和 hibernate 标准经验,所以这可能不正确,但也许它会帮助你开始.

EDIT I gave a shot at an actual solution. I have little envers and hibernate criteria experience, so this might not be right, but maybe it will help you get started.

AuditQuery query = getAuditReader().createQuery()
        .forRevisionsOfEntity(MyEntity.class, false, true);

query.add(AuditEntity.revisionProperty("createdDate").gt(minDate))
        .add(AuditEntity.revisionProperty("createdDate").lt(maxDate))
        .add(AuditEntity.revisionProperty("createdBy").eq(userId));

//the documentation shows getSingleResult returns a number
//so i'm guessing a resultList also contains numbers
List<Number> resultList = query.getResultList();

这篇关于如何使用 spring 数据 envers 查找实体的所有修订?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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