Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5954945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:02:37+00:00 2026-05-22T18:02:37+00:00

say i have a java bean/an entity with 100 fields (inherited or not it

  • 0

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));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:02:38+00:00Added an answer on May 22, 2026 at 6:02 pm

    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;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following simple java bean: class MyBean { private Date startDate;
HI, lets say I have a Java interface B, something like this. B.java :
I have a java application which uses JPA. Say I have an entity called
Say you have the following java bean: public class MyBean { private List<String> names
Let's say I have a java program that makes an HTTP request on a
Let's say I have some Java code: public class SomeClass { static { private
Let's say I have a java.util.Properties object. The Properties object has a method called
Say I have a GUI Java project of something that simulates an ATM machine,
Say I have a couple of java runtime environments running on my system which
Say I have a binary file (generated with Java) containing a 32 bit int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.