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:

  1. define equality based on a
  2. 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

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -