Entities’ equals and hashCode meth...

Entities’ equals and hashCode methods must avoid the use of the Identifier field

Is this rule useful?
1 votesVote for this rule!

  • Sharebar

Never ever use the database identifier in the equals() and hashCode() methods, because a transient object doesn’t have an identifier value.

Description
This audit rule looks for uses of the identifier field in the implementation of either the equals() or hashCode() methods. A transient object doesn’t have an identifier value because Hibernate will only assign a value when the object is saved. To implement equals() and hashCode(), use a unique business key, that is, compare a unique combination of class properties.

Example
The equals() method would be flagged because it uses the identifier property.

Usage of @Id field in equals() should be avoided as long as possible. Equals() should be implemented with a business logic independent from the persistence framework.

Read https://community.jboss.org/wiki/EqualsAndHashCode to learn more.

Tags: , , , , , , , , , , , , , ,
Available within Scertify™

Leave a Comment

Run the Code Refactory
Scertify™ Professional is an Eclipse plugin dedicated to analyze, control and correction of code quality defects, through computer-assisted code refactoring. It includes +1,600 coding rule controls and several rules for automated-refactoring).

Who we are?

Tocea provides Development Teams with innovative solutions enabling them to continuously control and improve the quality and integrity of their applications. We support developers, architects and project managers in their permanent effort to tackle IT debt and costs, by providing them with unique information on Code & Architecture Quality and some exclusive computer-aided refactoring features.
Visit Tocea.com

LoadingRetrieving latest tweet...

Back to Top

2014 © All rights reserved, Tocea