使用Hibernate Envers在两个修订版之间获得旧值和新值 [英] Getting the old value and new value between two revisions with Hibernate Envers

查看:194
本文介绍了使用Hibernate Envers在两个修订版之间获得旧值和新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是后续问题,检索经过审核的实体名称,旧值和给定修订版本的新值

我找到了如何获取修改一个实体,但没有看到任何容易找到两者之间的区别。 envers中有什么可以帮助做一个实体的差异在不同的修订吗?还是任何好的图书馆?

I have figured out how to get the two revision of an entity but don't see any easy to find the difference between the two. Is there anything in envers that will help doing a diff of an entity at different revisions? Or any good libraries?

如果我可以获得属性修改(_mod)字段字段,我会很酷。

I would be really cool if I could get the property modified (_mod) field fields.

推荐答案

所以我想出来让生活更容易是创建一个注释来标记我有兴趣比较的字段。没有我最终不得不坚持命名对话,就像只使用以get开头的方法。我发现这种方法有很多角落情况。

So what I came up with to make life easier was to create an annotation to mark the fields I was interested in comparing. Without I ended up having to get with sticking to naming conversation like only only using methods that start with 'get'. I found there was a lot of corner cases with this approach.

注释。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AuditCompare {

    public String name() default "";
    public CompareType compareBy() default CompareType.string;

    enum CompareType {
        string, count
    }

}

使用像

@Entity
@Audited
public class Guideline {

    .....

    @AuditCompare
    private String name;

    @AuditCompare
    private String owner;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval=true, mappedBy="guideline")
    private Set<GuidelineCheckListItem> checkListItems = new HashSet<GuidelineCheckListItem>();

    .........

}

由于envers将Set的更改和对象设置为两个不同的事件,我不想比较如果设置更改。然后做比较我有方法看起来像

Since envers audits both the Set changing and the object of the set as two different events I didn't want to compare if the set change. Then to do the comparison I have method that looks like

private void findMatchingValues(Object oldInstance, Object newInstance, ActivityEntry entry) {
    try {

        Class oldClass = oldInstance.getClass();
        for (Field someField : oldClass.getDeclaredFields()) {
            if (someField.isAnnotationPresent(AuditCompare.class)) {

                String name = someField.getAnnotation(AuditCompare.class).name();
                name = name.equals("") ? someField.getName() : name;

                Method method = oldClass.getDeclaredMethod(getGetterName(name));

                if(someField.getAnnotation(AuditCompare.class).compareBy().equals(AuditCompare.CompareType.count)) {
                    int oldSize = getCollectionCount(oldInstance, method);
                    int newSize = getCollectionCount(newInstance, method);
                    if (oldSize != newSize) entry.addChangeEntry(name, oldSize, newSize);

                } else {
                    Object oldValue = getObjectValue(oldInstance, method);
                    Object newValue = getObjectValue(newInstance, method);
                    if (!oldValue.equals(newValue)) entry.addChangeEntry(name, oldValue, newValue);
                }
            }
        }

    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
}

这篇关于使用Hibernate Envers在两个修订版之间获得旧值和新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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