HIbernate Envers:检索插入到同一事务中的快照 [英] HIbernate Envers: retrieve snapshots inserted in the same transaction

查看:101
本文介绍了HIbernate Envers:检索插入到同一事务中的快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个要求:使用Hibernate Envers在差异方面记录并保留事务中的实体更改.我们实现了RevisionListener:

We've got a requirement: log and persist the entity changes in the transaction in terms of diff using Hibernate Envers. We implemented a RevisionListener:

public class MyRevisionListener implements EntityTrackingRevisionListener {

    @Override
    public void newRevision(Object revision) {
        ...
    }

    @Override
    public void entityChanged(Class entityClass,
                              String entityName,
                              Serializable entityId,
                              RevisionType revisionType,
                              Object revisionEntity) {
        int revisionId = ((DefaultRevisionEntity) revisionEntity).getId();
        List<?> revisions = AuditReaderFactory.get(entityManager)
                .createQuery()
                .forRevisionsOfEntity(entityClass, false, true)
                .add(AuditEntity.id().eq(entityId))
                .add(AuditEntity.revisionNumber().le(revisionId + 1))
                .addOrder(AuditEntity.revisionNumber().desc())
                .setMaxResults(2)
                .getResultList();

        checkArgument(revisions.size() < 3, "Need at most two revisions: %s", revisions);
        checkArgument(revisions.size() > 0, "Need at least one revision: %s", revisions);

        // continue with diff calculation;
    }

}

问题:
1.第一个断言:我需要检查吗?进行以上查询时,结果是否可能包含两个以上的项目?
2.第二个断言:我认为对于每个更改(INSERT/UPDATE/DELETE),至少有一个快照(或修订版).是对的吗?如果是这样,为什么我的测试用例(使用UPDATE操作)由于该断言失败(意味着没有快照)而随机失败.

Questions:
1. First assertion: Do I need this checking? Having the above query Is it possible that the result contains more than two items?
2. Second assertion: I assume for every change (INSERT/UPDATE/DELETE) there's at least one snapshot (or revision). Is that right? If so why do my test cases (with an UPDATE operation) fail randomly due to this assertion failure (meaning there's no snapshot).

如果需要提供更多信息,请告诉我.

Please let me know if I need to provide more information.

更新
问题出在Spring Test,上下文和Bean管理(特别是我如何获取EntityManager).我接受了@Naros的帖子,因为它回答了第一个问题,并给第二个问题打了一个好主意:)

UPDATE
The problem was with Spring Test, context, and bean management (specifically how I obtain EntityManager). I accepted the post by @Naros as it answered the first question and gave a hit for the second question :)

推荐答案

我需要检查吗?进行以上查询时,结果是否可能包含两个以上的项目?

Do I need this checking? Having the above query Is it possible that the result contains more than two items?

否,setMaxResults(2)将保证查询最多返回2行;但是,您将需要处理查询返回小于2的用例.

No, setMaxResults(2) will guarantee that the query returns only a maximum of 2 rows; however you'll need to handle the use case where the query returns less-than 2.

我假设每次更改(插入/更新/删除)至少都有一个快照(或修订版).是对的吗?如果是这样,为什么我的测试用例(使用UPDATE操作)由于该断言失败(意味着没有快照)而随机失败.

I assume for every change (INSERT/UPDATE/DELETE) there's at least one snapshot (or revision). Is that right? If so why do my test cases (with an UPDATE operation) fail randomly due to this assertion failure (meaning there's no snapshot).

除非您在做不该做的事情,否则我不希望您的测试用例会随机失败. Envers的审核过程是确定性的,因此我希望它要么失败要么始终成功.

I wouldn't expect your test case to fail randomly unless you're doing something you shouldn't be. The audit process with Envers is pretty deterministic, so I would expect it to either fail or succeed consistently.

关于为什么未获得预期的行数的原因,我猜测可能是某些刷新模式配置正在起作用,这可能会干扰Envers审核查询,或者您可能是在保存实体的同时查询审核模式,并且它的关联,使得期望的有效谓词where子句在那个精确的时刻是无效的?

As to why you aren't getting the expected number of rows, my guess would be either some flush mode configuration is at play that could interfere with Envers audit queries or perhaps you're querying the audit schema amidst saving an entity and its associations such that an expected valid predicate where clause is invalid at that precise moment?

在没有看到您的测试用例的情况下,我只能推测出大部分.如果您可以发布测试用例和实体映射,那么我可以做得更深一些.

Without seeing your test case, I can only speculate for the most part. If you can post your test case and entity mappings, I could dig a bit deeper.

这篇关于HIbernate Envers:检索插入到同一事务中的快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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