Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

java - Getting the old value and new value between two revisions with Hibernate Envers

This is a follow up question to Retrieve audited entities name, old value and new value of the given revision

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

The annotation.

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

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

    enum CompareType {
        string, count
    }

}

which gets used like

@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>();

    .........

}

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...