找出两个用于版本跟踪的java bean之间的差异 [英] find out the differences between two java beans for version tracking

查看:206
本文介绍了找出两个用于版本跟踪的java bean之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个java bean /一个有100个字段的实体(在这种情况下是继承的还是不相关的)。更新操作后 - 在事务中,我想确定修改哪些字段以跟踪CVS之类的更新。最简单的方法是什么?任何框架建议?我应该制作这个对象的两个实例并迭代所有字段并匹配字段的值吗?在这种情况下,最好的等于方法怎么样呢?以下等于()似乎非常尴尬:

say i have a java bean/an entity with 100 fields (inherited or not it is not relevant in this case). After update operations - in a transaction, i want to determine which fields are modified to track updates like a CVS. What is the easiest way to do this? Any Framework suggestion? Should i make two instances of this object and iterate over all fields and match the values of fields ? How would the best equals method seem in such situations ? The following equals() seems very awkward :

return (field1.equals(o.field1)) && 
(field2.equals(o.field2)) &&  
(field3.equals(o.field3)) &&  
...
(field100.equals(o.field100));


推荐答案

您可以使用Apache Commons Beanutils。这是一个简单的例子:

You could use Apache Commons Beanutils. Here's a simple example:

package at.percom.temp.zztests;

import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.PropertyUtilsBean;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class Main {

    public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Main main = new Main();
        main.start();
    }

    public void start() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        SampleBean oldSample = new SampleBean("John", "Doe", 1971);
        SampleBean newSample = new SampleBean("John X.", "Doe", 1971);

        SampleBean diffSample = (SampleBean) compareObjects(oldSample, newSample, new HashSet<>(Arrays.asList("lastName")), 10L);
    }

public Object compareObjects(Object oldObject, Object newObject, Set<String> propertyNamesToAvoid, Long deep) {
    return compareObjects(oldObject, newObject, propertyNamesToAvoid, deep, null);
}

private Object compareObjects(Object oldObject, Object newObject, Set<String> propertyNamesToAvoid, Long deep,
        String parentPropertyPath) {
    propertyNamesToAvoid = propertyNamesToAvoid != null ? propertyNamesToAvoid : new HashSet<>();
    parentPropertyPath = parentPropertyPath != null ? parentPropertyPath : "";

    Object diffObject = null;
    try {
        diffObject = oldObject.getClass().newInstance();
    } catch (Exception e) {
        return diffObject;
    }

    BeanMap map = new BeanMap(oldObject);

    PropertyUtilsBean propUtils = new PropertyUtilsBean();

    for (Object propNameObject : map.keySet()) {
        String propertyName = (String) propNameObject;
        String propertyPath = parentPropertyPath + propertyName;

        if (!propUtils.isWriteable(diffObject, propertyName) || !propUtils.isReadable(newObject, propertyName)
                || propertyNamesToAvoid.contains(propertyPath)) {
            continue;
        }

        Object property1 = null;
        try {
            property1 = propUtils.getProperty(oldObject, propertyName);
        } catch (Exception e) {
        }
        Object property2 = null;
        try {
            property2 = propUtils.getProperty(newObject, propertyName);
        } catch (Exception e) {
        }
        try {
            if (property1 != null && property2 != null && property1.getClass().getName().startsWith("com.racing.company")
                    && (deep == null || deep > 0)) {
                Object diffProperty = compareObjects(property1, property2, propertyNamesToAvoid,
                        deep != null ? deep - 1 : null, propertyPath + ".");
                propUtils.setProperty(diffObject, propertyName, diffProperty);
            } else {
                if (!Objects.deepEquals(property1, property2)) {
                    propUtils.setProperty(diffObject, propertyName, property2);
                    System.out.println("> " + propertyPath + " is different (oldValue=\"" + property1 + "\", newValue=\""
                            + property2 + "\")");
                } else {
                    System.out.println("  " + propertyPath + " is equal");
                }
            }
        } catch (Exception e) {
        }
    }

    return diffObject;
}

    public class SampleBean {

        public String firstName;
        public String lastName;
        public int yearOfBirth;

        public SampleBean(String firstName, String lastName, int yearOfBirth) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.yearOfBirth = yearOfBirth;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public int getYearOfBirth() {
            return yearOfBirth;
        }
    }
}

这篇关于找出两个用于版本跟踪的java bean之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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