Getting old value and new value between two versions using Hibernate Envers

This is the next question Get the name of checked entities, old value and new value of a given version

I figured out how to get two revisions of an entity, but can't see it easy to find the difference between them. Is there anything in the change to help make a difference in essence across different versions? Or any good libraries?

It would be really great for me if I could get the fields of the field modified by properties (_mod).

+1


source to share


3 answers


So what I came up with, to make life easier, was to create an annotation to mark the fields that interested me. Without that, I had to face calling the conversation as soon as using only methods starting with "get". I found that there were many corner cases with this approach.

Annotations.

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

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

    enum CompareType {
        string, count
    }

}

      

which is 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 during checkout, both the Set change and the set object are both two different events I didn't want to compare if the set change. Then, to do the comparison, I have a 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);
    }
}

      

+2


source


There is currently no support for Envers. You will have to compare objects yourself.



+1


source


The Envers API is more for viewing history. It won't tell you what changed, but you can get two versions and look for differences. The point is that it does not save what has changed and saves all the fields of this object at this point in time. He would have to compare each one to figure out the differences, so you will need to write this code.

0


source







All Articles