Might be a little late, but I was in the same situation like you and ended up creating my own library for exactly your use-case. Since I was forced to come up with a solution myself, I decided to release it on Github, to spare others the hard work. You can find it here: https://github.com/SQiShER/java-object-diff

--- Edit ---

Here is a little usage example based on the OPs code:

SomeClass a = new SomeClass();
SomeClass b = new SomeClass();

a.setProp1("A");
a.setProp2("X");

b.setProp1("B");
b.setProp2("X");

DiffNode diff = ObjectDifferBuilder.buildDefault().compare(a, b);

assert diff.hasChanges();
assert diff.childCount() == 1;
assert diff.getChild('prop1').getState() == DiffNode.State.CHANGED;
Answer from SQiShER on Stack Overflow
🌐
Readthedocs
java-object-diff.readthedocs.io
java-object-diff Documentation
java-object-diff is a simple, yet powerful library to find differences between Java objects. It takes two objects and generates a tree structure that represents any differences between the objects and their children.
Using with Maven
In order to use java-object-diff with Maven, all you need to do is to add this dependency to your POM:
User Guide
Hopefully this brief overview over the most important classes and concepts of java-object-diff helps you getting the best out of this library.
Getting Started
This example was pretty simple and we only worked on a simple Map. But don't let this foul you. Working with other objects isn't any different. Of course there is much more to cover. Depending on your objects, there may be the need to do do some adjustments and tell java-object-diff how certain ...
🌐
GitHub
github.com › SQiShER › java-object-diff
GitHub - SQiShER/java-object-diff: Library to diff and merge Java objects with ease
java-object-diff is a simple, yet powerful library to find differences between Java objects. It takes two objects and generates a tree structure that represents any differences between the objects and their children.
Starred by 969 users
Forked by 177 users
Languages   Java 53.4% | Groovy 46.6% | Java 53.4% | Groovy 46.6%
Top answer
1 of 8
56

Might be a little late, but I was in the same situation like you and ended up creating my own library for exactly your use-case. Since I was forced to come up with a solution myself, I decided to release it on Github, to spare others the hard work. You can find it here: https://github.com/SQiShER/java-object-diff

--- Edit ---

Here is a little usage example based on the OPs code:

SomeClass a = new SomeClass();
SomeClass b = new SomeClass();

a.setProp1("A");
a.setProp2("X");

b.setProp1("B");
b.setProp2("X");

DiffNode diff = ObjectDifferBuilder.buildDefault().compare(a, b);

assert diff.hasChanges();
assert diff.childCount() == 1;
assert diff.getChild('prop1').getState() == DiffNode.State.CHANGED;
2 of 8
54

http://javers.org is library that does exacly what you need: has methods like compare(Object leftGraph, Object rightGraph) returning the Diff object. Diff contains a list of changes (ReferenceChange, ValueChange, PropertyChange) e.g.

given:
DummyUser user =  dummyUser("id").withSex(FEMALE).build();
DummyUser user2 = dummyUser("id").withSex(MALE).build();
Javers javers = JaversTestBuilder.newInstance()

when:
Diff diff = javers.compare(user, user2)

then:
diff.changes.size() == 1
ValueChange change = diff.changes[0]
change.leftValue == FEMALE
change.rightValue == MALE

It can handle cycles in graphs.

In addition you can get Snapshot of any graph object. Javers has JSON serializers and deserializers to snapshot, and changes so you can easily save them in database. With this library you can easily implement a module for auditing.

🌐
Readthedocs
java-object-diff.readthedocs.io › en › latest › getting-started
Getting Started - java-object-diff Documentation
java-object-diff provides a very simple API and tries to make everything as self-explanatory as possible. It can handle a wide variety of object structures without the need for any configuration.
🌐
Javers
javers.org › documentation › diff-examples
Diff Examples — JaVers Documentation
Diff: * new object: Employee/Hired One - 'boss' = 'Employee/Big Boss' - 'name' = 'Hired One' - 'salary' = '10000' * new object: Employee/Hired Second - 'boss' = 'Employee/Big Boss' - 'name' = 'Hired Second' - 'salary' = '10000' * changes in Employee/Big Boss : - 'subordinates' collection changes : 1.
🌐
Readthedocs
java-object-diff.readthedocs.io › en › latest › user-guide
User Guide - java-object-diff Documentation - Read the Docs
The objects given to the ObjectDiffer are called working and base, where working is considered to be a modified version of base. There is actually no technical reason for this. The ObjectDiffer doesn't care how the objects relate to each other. They could actually be two completely different objects that never shared a mutual history.
🌐
GitHub
github.com › SQiShER › java-object-diff › blob › master › README.md
java-object-diff/README.md at master · SQiShER/java-object-diff
`java-object-diff` is a simple, yet powerful library to find differences between Java objects. It takes two objects and generates a tree structure that represents any differences between the objects and their children.
Author   SQiShER
Find elsewhere
🌐
Sqisher
sqisher.github.io › java-object-diff
java-object-diff by SQiShER
java-object-diff is a simple, yet powerful library to find differences between Java objects. It takes two objects and generates a tree structure that represents any differences between the objects and their children.
🌐
GitHub
github.com › SQiShER › java-object-diff › blob › master › docs › user-guide.md
java-object-diff/docs/user-guide.md at master · SQiShER/java-object-diff
Library to diff and merge Java objects with ease. Contribute to SQiShER/java-object-diff development by creating an account on GitHub.
Author   SQiShER
🌐
Maven Repository
mvnrepository.com › artifact › de.danielbechler › java-object-diff
Maven Repository: de.danielbechler » java-object-diff
Home » de.danielbechler » java-object-diff · Library to diff and merge Java objects with ease · LicenseApache 2.0 · CategoriesDiff and Patch Libraries · Tagsdiffdeltapatch · HomePage https://github.com/SQiShER/java-object-diff 🔍 Inspect URL · Ranking#12801 in MvnRepository (See Top Artifacts)#6 in Diff and Patch Libraries ·
🌐
Apache Commons
commons.apache.org › proper › commons-lang › javadocs › api-3.9 › org › apache › commons › lang3 › builder › DiffResult.html
DiffResult (Apache Commons Lang 3.9 API)
java.lang.Object · org.apache.commons.lang3.builder.DiffResult · All Implemented Interfaces: Iterable<Diff<?>> public class DiffResult extends Object implements Iterable<Diff<?>> A DiffResult contains a collection of the differences between two Diffable objects.
🌐
Baeldung
baeldung.com › home › java › using the apache commons lang 3 for comparing objects in java
Using the Apache Commons Lang 3 for Comparing Objects in Java | Baeldung
January 8, 2024 - Various libraries can be used for comparing objects in Java, such as JaVers or Apache Commons Lang 3, which we will cover in this article. Apache Commons Lang 3 represents the 3.0 version of the Apache Commons Lang library, which offers many functionalities. We will explore the DiffBuilder class to compare and obtain the differences between two objects of the same type.
🌐
Javadoc.io
javadoc.io › doc › de.danielbechler › java-object-diff › latest › index.html
java-object-diff 0.95 javadoc (de.danielbechler)
Latest version of de.danielbechler:java-object-diff · https://javadoc.io/doc/de.danielbechler/java-object-diff · Current version 0.95 · https://javadoc.io/doc/de.danielbechler/java-object-diff/0.95 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/de...
🌐
GitHub
github.com › SQiShER › java-object-diff › wiki › Getting-Started
Home · SQiShER/java-object-diff Wiki · GitHub
Library to diff and merge Java objects with ease. Contribute to SQiShER/java-object-diff development by creating an account on GitHub.
🌐
Maven Repository
mvnrepository.com › artifact › de.danielbechler › java-object-diff › 0.10.2
Maven Repository: de.danielbechler » java-object-diff » 0.10.2
Home » de.danielbechler » java-object-diff » 0.10.2 · Library to diff and merge Java objects with ease · Note: There is a new version for this artifact · Maven · Gradle · SBT · Mill · Ivy · Grape · Leiningen · Buildr · Scope: compile · test · provided ·
🌐
Read the Docs
app.readthedocs.org › projects › java-object-diff
java-object-diff - Read the Docs Community
java-object-diff · EN · diff groovy java jvm merge · Maintainers · Repository https://github.com/SQiShER/java-object-diff · Versions 2 Builds 89 · View docs · Debug information · Spam score 0 ·
🌐
Netlify
mallim.netlify.app › java-tips-how-to-compare-the-difference-for-two-given-java-objects
Java Tips: How to compare the difference for two given Java objects? | Mallim's Site
September 24, 2020 - JaVers Homepage: https://javers.org/documentation/ Quick intro: https://www.baeldung.com/javers java-object-dff Homepage: http://sqisher.github.io/java-object-diff/ Example 1 : https://morioh.com/p/c98c7721a9ed Example 2 (Chinese) : ...
🌐
GitHub
github.com › SQiShER › java-object-diff › issues › 210
How to get the difference between two simple objects? · Issue #210 · SQiShER/java-object-diff
July 31, 2019 - Let's say I have two simple objects and I need to know if there is a difference between them and if there is I need to know where and I also need to know the previous value of that field and the new value.
Author   franzisk