Entity, valuetype and equals

There are lots of different classes of equality, but the distinction between entities and valuetypes is probably the most important aspect, since it is commonly needed and I have witnessed some misunderstanding with it.

An entity is, by definition, identifiable by its identity. So it's an error to include any other attribute than the identifier in the equals method. A person won't change to another, when the hair colour attribute changes from "brown" to "none", for example.

(There may be some technical reasons, like unit of work, to implement a different equals, but I see this as a technical layer on top of the "actual" entity.)

A valuetype, on the other hand, is identifiable by all its observable attributes. So it's an error to omit any of the attributes in the equals method.

Originally published on 2008-10-24 at http://www.jroller.com/wipu/entry/entity_valuetype_and_equals under category Java