Our domain layer entity classes have autogenerated equals
and
hashCode
methods defined. The Company
class, for example, defines these methods as shown:
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 if we regenerate the equals
method with the NetBeans IDE.
Delete the equals
method and right-click on the Company.java
file in the editor using the mouse to display the context menu. Select the Insert Code… option:
From the pop-up menu, select the equals()… option and ensure that the idCompany : Integer field is selected in the Generate equals(...