3

I have this @Override for equals() in my MyClass class:

@Entity( name = "MyClass" )
@Table( name = "my_class" )
public class MyClass extends MySuperClass
{
  ...
  @Override
  public boolean equals( Object o )
  {
    if ( this == o )
    {
      return true;
    }
    if ( o == null || this.getClass() != o.getClass() )
    {
      return false;
    }
    if ( !super.equals( o ) )
    {
      return false;
    }
    MyClass that = ( MyClass ) o;
    return this.var1.equals( that.var1 ) && this.var2.equals( that.var2 );
  }
  ...
}

Pretty standard. Matter of fact, it follows Java best practices.
Later in life I have this in another sub-package class (my controller class):

...
package com.a.b.api.controllers;
...
import com.a.b.jpa.models.MyClass;
...
MyClass myObject1 = new MyClass( var1, var2 );
MyClass myObject2 = this.myClassRepository.getById( 1 ); // SpringBoot/Jpa/Hibernate

if ( myObject2.equals( myObject1 ) )
{
   ...do something...
}
...
this.myClassRepository.save( myObject1 );
...

My problem is that the .equals() is always failing here:

if ( o == null || this.getClass() != o.getClass() )

because java says that this.getClass() and o.getClass() are NOT equal. When I debug the code (in Intellij IDEA 2022.1 UE) I see this:

this.getClass() = MyClass@13706

but

o.getClass = com.a.b.jpa.models.MyClass@8f7462

But they are the same class! Almost every Java book, tutorial, blog, Intellij IDEA, etc. demonstrates the .equals() this way. I have tried this in Ubuntu 20.04.4 LTS java-14-openjdk-amd64 and java-17-openjdk-amd64 with the same results.

What am I doing wrong?

Van
  • 211
  • 1
  • 9
  • 4
    This, `if ( !super.equals( o ) ) { return false; }` is new to me and doesn't really make sense. If super is Object, it falls back to checking for reference equality, which is what an equals override is being created to *avoid*. – Hovercraft Full Of Eels Apr 14 '22 at 15:00
  • @HovercraftFullOfEels irrelevant as the execution never gets to that point in the code but, fyi, MyClass is extended from a super class. – Van Apr 14 '22 at 15:08
  • @khelwood Yes, it's the same running application instance (SpringBoot 2.2.6). The code provided is almost verbatim. And, as I stated in the OP, the controller is creating a new object (to persist in the database) and loading a previous object from the database to compare. – Van Apr 14 '22 at 15:08
  • use `!this.getClass().equals(o.getClass())` instead of `!=` – Zircon Apr 14 '22 at 15:15
  • @Zircon Yep, tried it before and just tried it again. It also fails. :( – Van Apr 14 '22 at 15:17
  • @Zircon Impossible for this to make a difference. `Class` inherits `equals` from `Object`. – Boann Apr 14 '22 at 15:51
  • `if ( o == null || this.getClass() != o.getClass() ) { return false; }` violates Liskov substitution principle. Objects are equals only if they have the same implementation class. – grigouille Apr 15 '22 at 08:55
  • suggested reading https://www.baeldung.com/java-comparing-objects#equals-instance – KNU Aug 29 '23 at 03:16

4 Answers4

7

myObject2 is an instance of a proxy class, generated at runtime by Hibernate using Byte Buddy. The generated proxy intercepts all method invocations, that's why getClass() returns different results.

As an alternative to getClass(), using instanceof might be another approach:

if ( !(this instanceof MyClass && o instanceof MyClass) )
{
   return false;
}

However keep in mind that instanceof has its drawbacks. It violates the symmetry principle.

You shouldn't compare these objects in the first place, since a new object should be different from a Hibernate managed one that has a persistent state.

happy songs
  • 835
  • 8
  • 21
  • 1
    +1 for most of this, but I think the right approach for this sort of check is to use a `canEqual` method, so that each subclass can decide whether it's equal to superclass instances. See https://www.artima.com/articles/how-to-write-an-equality-method-in-java (search within that page for `canEqual`). – ruakh Apr 14 '22 at 15:47
2

happy songs correctly stated in his response:

myObject2 is an instance of a proxy class, generated at runtime by Hibernate using Byte Buddy. The generated proxy intercepts all method invocations, that's why getClass() returns different results.

I really didn't want to use instanceof because that is considered bad practice so I started poking around and stumbled onto a post having a similar issue. Their solution was to add the final keyword to their class declaration. I thought this insignificant but gave it try - AND IT WORKED! Adding the final keyword caused

if ( o == null || this.getClass() != o.getClass() )

and

if ( o == null || !this.getClass().equals( o.getClass() ) )

to work properly. My class code is now:

@Entity( name = "MyClass" )
@Table( name = "my_class" )
final public class MyClass extends MySuperClass
{
  ...
  @Override
  public boolean equals( Object o )
  {
    if ( this == o )
    {
      return true;
    }
    if ( o == null || !this.getClass().equals( o.getClass() ) )
    {
      return false;
    }
    if ( !super.equals( o ) )
    {
      return false;
    }
    MyClass that = ( MyClass ) o;
    return this.var1.equals( that.var1 ) && this.var2.equals( that.var2 );
  }
  ...
}

Thank you all for your assistance! And a big thank you to happy songs for pointing me in the proper direction!

Van
  • 211
  • 1
  • 9
0

I use this standard equals and hashcode implementations for JPA entities (as described here):

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (!(o instanceof MyClass)) {
        return false;
    }
    MyClass other = (MyClass) o;
    return id != null && id.equals(other.getId());
}


@Override
public int hashCode() {
    return getClass().hashCode();
}
David Mališ
  • 196
  • 4
0

You are not using a best practice. Change your equals to be something like the following (null check first, then same check, then class check):

 public boolean equals(Object obj) {
   if (obj == null) { return false; }
   if (obj == this) { return true; }
   if (obj.getClass() != getClass()) {
     return false;
   }
   
   ... do the actual comparison here
 }

Note: the code above is a paraphrase of Apache EqualsBuilder

DwB
  • 37,124
  • 11
  • 56
  • 82
  • if ( this.getClass() != o.getClass() ) { return false; } violates Liskov substitution principle. Objects are equals only if they have the same implementation class. – grigouille Apr 15 '22 at 10:52