Why would you want to include a List in a equals/hashCode comparison for a root entity? This is a wrong approach.
Equality should be based on the underlying table record unicity constraint. Ideally, you should have a natural key in every database table
like a SSN, email address, domain name, UUID
Sometimes, you don't have a natural id, but then you still have the primary key. Contrary to popular belief, you can use the PK for equals/hashCode
, you just have to make sure that hashCode renders a constant value for every entity state transition.
If you worry about hashCode performance, then you should know that a persistent collection is not meant to store large amounts of data. So, prior to having the hashCode as a real bottleneck (as depicted in Effective Java), you'd have to fetch the collection, and the fetching tons of data is way more costly anyway. If that's the case, you are better off using a query instead.