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
🌐
GitHub
github.com › SQiShER › java-object-diff
GitHub - SQiShER/java-object-diff: Library to diff and merge Java objects with ease · GitHub
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 970 users
Forked by 176 users
Languages: 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
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.
🌐
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
🌐
GitHub
github.com › openrewrite › java-object-diff
GitHub - openrewrite/java-object-diff: Library to diff and merge Java objects with ease · GitHub
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: openrewrite
🌐
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
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 23491934 › how-to-use-java-object-diff-library-to-compare-object-graph-involving-one-to-man
How to use java-object-diff library to compare object graph involving one-to-many relationships implemented using lists? - Stack Overflow
Regarding your example: given that you pass two versions of the User to the ObjectDiffer, java-object-diff would return a node (root node), representing the User. As long as you don't mark the User class as equals-only-type, it would simply be compared on a per-property basis. Each property would end up as a child of the root node. Now let's say one of the properties is a List of Roles: the library will group the items that exist in the base and working version (known items), those that only exist in the base version (removed) and those that only exist in the working version (added).
🌐
Maven Central Repository
search.maven.org › de.danielbechler › java-object-diff › 0.95
Maven Central: de.danielbechler:java-object-diff
pkg:maven/de.danielbechler/java-object-diff@0.95 · Used in: components · Overview · Overview · Versions · Versions · Dependents · Dependents · Dependencies · Dependencies · Library to diff and merge Java objects with ease · Copy to clipboard · <dependency> <groupId>de.danielbec...
🌐
Maven Repository
mvnrepository.com › artifact › de.danielbechler › java-object-diff
Maven Repository: de.danielbechler » java-object-diff
January 16, 2018 - Concurrency Libraries · Web Frameworks · Android Platform · Code Generators · View All · Home » de.danielbechler » java-object-diff · Library to diff and merge Java objects with ease · LicenseApache 2.0 · CategoriesDiff and Patch Libraries · Tagsdiffdeltapatch ·
🌐
Javers
javers.org
JaVers — The Leading Framework for Data Audit and Diff in Java
JaVers is the industry standard for data versioning and object comparing in modern Java projects. It provides a database-independent engine that automates change tracking across SQL and NoSQL stacks.
🌐
GitHub
github.com › SQiShER › java-object-diff › blob › master › docs › getting-started.md
java-object-diff/docs/getting-started.md at master · SQiShER/java-object-diff
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.
Author: SQiShER
🌐
GitHub
github.com › SQiShER › java-object-diff › releases
Releases · 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
🌐
GitHub
github.com › SQiShER › java-object-diff › blob › master › LICENSE
java-object-diff/LICENSE 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
🌐
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)
Typically these differences are displayed using toString() method, which returns a string describing the fields that differ between the objects.
🌐
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.
🌐
Readthedocs
java-object-diff.readthedocs.io › en › latest › user-guide
User Guide - java-object-diff Documentation - Read the Docs
It acts as a factory to get hold of an actual ObjectDiffer instance and exposes a powerful configuration API in order to customize its behavior to suit your needs. The following sections describes the different parts of the configuration API.
🌐
Maven Central
central.sonatype.com › artifact › de.danielbechler › java-object-diff
java-object-diff - de.danielbechler - Maven Central - Sonatype
<dependency> <groupId>de.danielbechler</groupId> <artifactId>java-object-diff</artifactId> <version>0.95</version> </dependency>