What is the best practice when implementing equals() for entities with generated ids

后端 未结 3 1452
萌比男神i
萌比男神i 2020-12-16 01:06

If I have a table with columns A, B, C, D
 A: auto-generated id (PK)
 B & C: combination must be unique (these are the columns that actually de

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 01:55

    If (B,C) is a unique pair, there's no need for an auto-generated id in addition. For the table, A is equivalent to (B,C) (one-to-one relation).

    You may want or need the extra key, but I agree with seanizer, use (B,C) for equals and because A is redundant (and null before the object is persisted), don't use that for equals (and hashcode)

提交回复
热议问题