[hibernate-dev] Entity implementation of equals()

Gail Badner gbadner at redhat.com
Tue Aug 27 19:48:12 EDT 2019


I'm looking into the impact of HHH-13590.

In the test for HHH-13590, I see that the mapped superclass entity defines
equals() as:

public boolean equals(Object o) {
   if (this == o) return true;
   if (o == null || getClass() != o.getClass()) return false;



Due to the bug:
* this is a Project instance;
* o is a HibernateProxy with target == this.

Because the getClass() != o.getClass(), false is returned, and that
ultimately causes the test to fail.

The fix for HHH-13590 results in comparing a Project instance with itself
(not the proxy).

I see that the documentation has a similar implementation of equals() in
"Example 111. Natural Id equals/hashCode" of [1].

In general, is it OK for equals() to require both instances to be of the
same class?



More information about the hibernate-dev mailing list