java - What is the best practice when implementing equals() for entities with generated ids -
if have table columns a, b, c, d
a: auto-generated id (pk)
b & c: combination must unique (these columns define identity in business sense)
d: other columns
now, if i'll create business objects based on table (e.g. in java), 1 better implementation of equals() method:
- define equality based on a
- define equality based on b , c
or, wouldn't matter of 2 choose.
definitely b , c, because want equals()
contract valid before entities persisted. yourself:
these columns define identity in business sense
if case, logic equals()
should use. database keys database's concern , should of no concern business layer.
and don't forget use same properties in hashcode()
, too.
Comments
Post a Comment