I am trying to discuss how best to deal with the practice of implementing hash code and equality for entities (I mean an object in a general sense, but in most cases it will be a JPA entity).
Chapter 24 of the Hibernate Guide http://docs.jboss.org/hibernate/core/3.3/reference/en/html/best-practices.html talks about this ...
Define natural keys for all objects and match them with. Add equal () and hashCode () to compare the properties that make up the natural key.
It makes sense to have .equals and .hashcode include only these natural keys, but what if you have more than one instance of the same object (the same natural id, thus the same hashcode)? It seems like this practice can have subtle consequences elsewhere in your application. Has anyone tried this before on a large scale?
equals hashcode hibernate jpa entity
benstpierre
source share