Refactoring Java equals() and hashCode()

Our domain layer entity classes have autogenerated equals and hashCode methods defined. The Company class, for example, defines these methods as shown:

Refactoring Java equals() and hashCode()

It is best practice to always provide correctly implemented equals and hashCode methods that use the entity ID to calculate the value that is returned. These methods are used by JPA to determine the equality between entities. Our autogenerated equals method will work correctly with JPA as the ID entity is used in the comparison for each method. However, the //TODO: Warning message on line 83 (see the previous screenshot) indicates an issue that can be avoided ...

Get Enterprise Application Development with Ext JS and Spring now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.