After implementing hashCode() and equals() for all @Entitys we must admit: Most values might change over time - i.e. making them useless for usage in hashCode() (if they change, the hashcode changes and HashSets will not find them anymore). But also there often are just no attributes which are "natural keys" (i.e. the will not change) and could be used (what Hibernate and other documentation often suggests). This would make the id the only attribute which could be used - but is null if not yet persisted.
Ergo: We must set an UUID ourselves as soon as possible and just use this UUID in hashCode (end equals()).
See also https://github.com/debuglevel/greeting-microservice/issues/23
After implementing
hashCode()
andequals()
for all@Entity
s we must admit: Most values might change over time - i.e. making them useless for usage inhashCode()
(if they change, the hashcode changes andHashSet
s will not find them anymore). But also there often are just no attributes which are "natural keys" (i.e. the will not change) and could be used (what Hibernate and other documentation often suggests). This would make theid
the only attribute which could be used - but isnull
if not yet persisted.Ergo: We must set an UUID ourselves as soon as possible and just use this UUID in
hashCode
(endequals()
).