Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ability to compare equal by equals() method objects with ReflectionBuilder #1137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yurbar
Copy link

@yurbar yurbar commented Nov 23, 2023

Useful when need to compare JPA entities with the same identifier.

Example: OptimisticLockingException thrown and you need to recover and print what's a difference between two entities ts with the same identifiers. You can't do it with the ReflectionDiffBuilder now because it will print nothing because those objects are equals by equals() method. So with this change you can have such an ability for ReflectionDiffBuilder

…ectionDiffBuilder

Useful when need to compare JPA entities with the same identifier
@yurbar yurbar changed the title Add an ability to compare equals by equals() method objects with ReflectionBuilder Add an ability to compare equal by equals() method objects with ReflectionBuilder Nov 27, 2023
@garydgregory
Copy link
Member

@yurbar
This does not make sense to me; use @DiffExclude or ReflectionDiffBuilder.Builder.setExcludeFieldNames(String...).

@berse2212
Copy link

Just came here to create the same PR. I need to compare JPA Entities with the same ID but different fields. This is because I want to find the changes between different versions of the entities. Sadly the ReflectionDiffBuilder always uses the shortcut of comparig by equals() which is always true in this case.

@garydgregory you cannot exclude the equals check this why because it's done before even creating the diff.

@garydgregory
Copy link
Member

I'll need to check back on this PR later this week.

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @yurbar

Thank you the PR.

You did not reply to my previous question:

Why not use @DiffExclude or ReflectionDiffBuilder.Builder.setExcludeFieldNames(String...).

Please see my embedded comments.

New elements should have a Javadoc @since tag of 3.17.0.

* @throws IllegalArgumentException
* if {@code lhs} or {@code rhs} is {@code null}
*/
public ReflectionDiffBuilder(final T lhs, final T rhs, final ToStringStyle style, boolean testTriviallyEqual) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not add another constructor. This class provides a builder for new settings.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor is the purpose of this change. If I remove it, then the change becomes useless

secondObject.field1 = "b";

final DiffResult list = firstObject.diff(secondObject);
assertEquals(1, list.getNumberOfDiffs());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should assert what the difference is; IOW, make assertions on the other properties of the DiffResult and Diff instances.

@@ -29,6 +29,31 @@ private static final class TypeTestChildClass extends TypeTestClass {
String field = "a";
}

@SuppressWarnings("unused")
private static class TypeTestEntityClass implements Diffable<TypeTestEntityClass> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename TypeTestEntityClass to EntityTestClass.
Add a class Javadoc that states the intent for the test fixture to emulate a JPA entity class.


@Override
public boolean equals(Object obj) {
if (this == obj) return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always use blocks ({...}}

@yurbar
Copy link
Author

yurbar commented Aug 28, 2024

Hello @yurbar

Thank you the PR.

You did not reply to my previous question:

Why not use @DiffExclude or ReflectionDiffBuilder.Builder.setExcludeFieldNames(String...).

Please see my embedded comments.

New elements should have a Javadoc @since tag of 3.17.0.

@DiffExclude just excludes particular field from output, but I need an ability to compare object which are equal by equals method, but different by other fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants