This article is a repost of my comments to the question on how to implement hashCode and equals on stackoverflowThere are some issues worth noticing if you’re implementing hashCode and equals for classes that are persisted using an Object-Relationship Mapper (ORM) like Hibernate. If you didn’t think this topic is stupidly overcomplicated already!Lazy loaded objects are subclassesIf your objects are persisted using an ORM, in many cases you will be dealing with dynamic proxies to avoid loading object too early from the data store. These proxies are implemented as subclasses of your own class. This means that the commonly recommended this.getClass() == o.getClass() will return false. For example:Person saved = <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"John Doe"</span><span style="color: #66cc66;">)</span>; <span style="color: #aaaadd; font-weight: bold;">Long</span> key = dao.<span style="color: #006600;">save</span><span style="color: #66cc66;">(</span>saved<span style="color: #66cc66;">)</span>; dao.<span style="color: #006600;">flush</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>; Person retrieved = dao.<span style="color: #006600;">retrieve</span><span style="color: #66cc66;">(</span>key<span style="color: #66cc66;">)</span>; saved.<span style="color: #006600;">getClass</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">(</span>retrieved.<span style="color: #006600;">getClass</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>; <span style="color: #808080; font-style: italic;">// Will return false if Person is loaded lazy</span>If you’re dealing with an ORM using o instanceof Person is the only thing that will behave correctly. Lazy loaded objects have null-fieldsORMs usually use the getters to force loading of lazy loaded objects. This means that person.name will be null if person is lazy loaded, even after person.getName() forces loading and returns “John Doe”. In my experience, this crops up often in hashCode and equals.If you’re dealing with an ORM, make sure to always use getters, and never field references in hashCode and equals.Saving an object will change it’s statePersistent objects often use a id field to hold the key of the object. This field will be automatically updated when an object is first saved. Don’t use an id field in hashCode. But you can use it in equals. A pattern I often use is<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">getId</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span> == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span> == other; <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">getId</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span> == other.<span style="color: #006600;">getId</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>; <span style="color: #66cc66;">}</span>But: You cannot include getId() in hashCode(). If you do, when an object is persisted, it’s hashCode changes. If the object is in a HashSet, you’ll “never” find it again.In my Person example, I probably would use getName() for hashCode and getId() plus getName() (just for paranoia) for equals. It’s okay if there are some risk of “collisions” for hashCode, but never okay for equals. hashCode should use the non-changing subset of properties from equals Java