I asked "Is it really worth implementing toString()
for entity classes?". The answer I accepted demonstrated the utility of the toString()
method of an entity class writing the "name" or "ID" of the entity (and, I'd say, not much else). But that requires that the entity have a "name" or "ID".
So, should an entity class always have an ID field. If it does not naturally have one, should you add one, given that it is useful only for debugging? A private final long
, perhaps? That can be done elegantly and efficiently.
I'm not talking necessarily about database backed classes. Here by an entity I mean a class that has the invariant (for non-null x)
x.equals(y) == (x == y)