Is it proper for equals() to depend only on an ID?

Whether you should do this depends on the semantics of your class. That is, what does it mean to say that two objects of your class are equivalent?

The most important distinction is between objects with value semantics and objects with entity semantics. Entity objects are not equivalent even if they have equivalent attributes (colour, length, etc.). In many cases, including when the object has been read from a database table that has a primary key, entity objects will have an ID field that is unique. Comparing only the ID field in that case is the right thing to do.

Leave a Comment

tech