This question is basically a follow-up to questions:
Should I write equals() methods in JPA entities? and What is the best practice when implementing equals() for entities with generated ids
Some background first...
You can regularly encounter the following primary key constellations:
- Natural keys (business keys): usually a set of real, multi-column attributes of the entity
- Artificial keys (surrogate keys): meaningless, usually auto-increment (IDENTITY, AUTO_INCREMENT, AUTOINCREMENT, SEQUENCE, SERIAL, ...) IDs
- Hybrid keys (semi-natural/semi-artificial keys): usually consisting of an artificial ID and some additional, natural column/s, e.g any table that references another table which uses an ID and extends that key (entity_id, ordinal_nbr) or similar.
Frequent scenario: many-to-one references to a root, branch, or leaf inheritance table, which all share a common, "stupid" ID via identifying relationship/dependent key. Root (and branch) tables often make sense when another table needs to reference all entity types, e.g. PostAddresses -> Contacts, where Contacts has sub tables Persons, Clubs, and Facilities, which have nothing in common but being "contactable".
Now to JPA:
In Java, we can create new entity objects whose PK may be incomplete (null or partly null), an entity (row) that a DBMS would ultimately prevent us from being inserted into the DB.
However, when working with application code, it's often handy to have new (or detached) entities that can be compared to existing (managed) entities, even if the new entity objects don't have a PK value yet. To achieve this for any entities that have natural key columns, use them for implementing equals() and hashCode() (as suggested by the other two SO postings).
Question:
But what do you do when no natural/business key can be determined, as in the case of the Contacts table, which is basically just an ID (plus a discriminator)? What would be a good column selection policy for basing equals() and hashCode() implementations on? (artificial keys 2. and 3. above)
There's obviously not much of a choice...
One (naive) goal would be to achieve the same "transient comparability". Can it be done? If not, what does the general approach look like for artificial ID equals() and hashCode() implementations?
Note: I'm already using Apache EqualsBuilder and HashCodeBuilder... I have intentionally "naivified" my question.