Good practise to use Equals and Hashcode Method in Hibernate

ISOP
ISOP Nepal
Published in
3 min readDec 2, 2021

--

Hibernate is a popular, open-source Java persistent framework, which provides Object Relational Mapping, also known as the ORM framework. It uses the equals and hashcode method to provide an object’s equality on the Java side. You should override equals() and hashcode() if :

1) You are storing instances of the persistent class in a Set for representing many-valued associations.

2) You are using reattachment of detached persistent instances.

Another worth noting point is that Hibernate only guarantees equivalence of database row (persistent identity) and Java object inside a particular Session. This means if you store instances retrieved in different Sessions in a Set, you will be having duplicates. Now the most important aspect of overriding equals and hashcode() for hibernate entity classes, you should never decide equality just based upon identifier.

Though it’s convenient to compare identifiers to see if they belong to the same database row, Unfortunately, we can’t use this approach with generated identifiers. Since Hibernate only assigns identifier values to object that is persistent, a newly created instance will not have any identifier value.

Similarly, if an instance is not persisted, and currently in a Set, saving it to the database will assign an identifier value, which will further change the value of the hashcode() method, finally resulting in breaking the contract of the Set.

That’s why it’s best to implement equals and hashcode in Hibernate using business key equality e.g. an Employee is the same if its name, surname, father’s name, department, date of birth is same. Properties which are not prone to change e.g. date of birth are better candidate of business equality than those which is easier to change e.g. address and contact number.

In short, remember these best practices while overriding equals() and hashcode() for Hibernate entity class :

1) Don’t let your equals() method only uses identifier values for equivalence check.

2) Implement equals() and hashCode() using real word key that would identify instance in real world.

3) Use Immutable and unique properties of objects for equality.

4) Don’t use getClass() to compare object equality because Hibernate uses proxy and this check will always fail. Instead, use instanceof operator, it respect proxy because they have IS-A relationship with the actual object.

5) Use getter and setter methods to access properties instead of directly accessing the, because hibernate lazily initialize object, when there getProperty() method is called. Using name may return null but getName() may return value from database.

That’s all about overriding equals() and hashcode() methods in Java, I am reiterating this but its imperative for a Java programmer to be able to write equals, hashcode(), compareTo() method by hand. It is not just useful for learning purpose but to clear any coding exercise during Java interviews. Writing code for equals and hashcode is very popular programming interview questions now days.

For Hibernate persistent class its rather tricky to override equals() and hashCode() because otherwise bad practices turns into best practices because of extensive of proxy. You should not use Eclipse IDE code generator for equals() and hashCode() for hibernate entity class, as they use getClass() to check type equality.

--

--

ISOP
ISOP Nepal

ISOP app is your virtual classroom. Virtual classroom allows you to learn when you like, not when bell rings.