Note that there are some explanatory texts on larger screens.

plurals
  1. POThe JPA hashCode() / equals() dilemma
    text
    copied!<p>There have been <a href="https://stackoverflow.com/questions/4762573/should-embeddable-jpa-class-implement-equals-and-hashcode">some</a> <a href="https://stackoverflow.com/questions/3147166/how-to-implement-equals-and-hashcode-methods-in-baseentity-of-jpa">discussions</a> here about JPA entities and which <code>hashCode()</code>/<code>equals()</code> implementation should be used for JPA entity classes. Most (if not all) of them depend on Hibernate, but I'd like to discuss them JPA-implementation-neutrally (I am using EclipseLink, by the way).</p> <p>All possible implementations are having their own <strong>advantages</strong> and <strong>disadvantages</strong> regarding:</p> <ul> <li><strong><code>hashCode()</code>/<code>equals()</code></strong> contract <strong>conformity</strong> (immutability) for <code>List</code>/<code>Set</code> operations</li> <li>Whether <strong>identical</strong> objects (e.g. from different sessions, dynamic proxies from lazily-loaded data structures) can be detected</li> <li>Whether entities behave correctly in <strong>detached (or non-persisted) state</strong> <br></li> </ul> <p>As far I can see, there are <strong>three options</strong>: </p> <ol> <li>Do not override them; rely on <code>Object.equals()</code> and <code>Object.hashCode()</code> <ul> <li><code>hashCode()</code>/<code>equals()</code> work</li> <li>cannot identify identical objects, problems with dynamic proxies</li> <li>no problems with detached entities</li> </ul></li> <li>Override them, based on the <strong>primary key</strong> <ul> <li><code>hashCode()</code>/<code>equals()</code> are broken</li> <li>correct identity (for all managed entities)</li> <li>problems with detached entities</li> </ul></li> <li>Override them, based on the <strong>Business-Id</strong> (non-primary key fields; what about foreign keys?) <ul> <li><code>hashCode()</code>/<code>equals()</code> are broken</li> <li>correct identity (for all managed entities)</li> <li>no problems with detached entities</li> </ul></li> </ol> <p><strong>My questions are:</strong></p> <ol> <li>Did I miss an option and/or pro/con point?<br></li> <li>What option did you choose and why?</li> </ol> <p><br><br> <strong>UPDATE 1:</strong></p> <p>By "<code>hashCode()</code>/<code>equals()</code> are broken", I mean that successive <code>hashCode()</code> invocations may return differing values, which is (when correctly implemented) not broken in the sense of the <code>Object</code> API documentation, but which causes problems when trying to retrieve a changed entity from a <code>Map</code>, <code>Set</code> or other hash-based <code>Collection</code>. Consequently, JPA implementations (at least EclipseLink) will not work correctly in some cases.</p> <p><strong>UPDATE 2:</strong></p> <p>Thank you for your answers -- most of them have remarkable quality.<br> Unfortunately, I am still unsure which approach will be the best for a real-life application, or how to determine the best approach for my application. So, I'll keep the question open and hope for some more discussions and/or opinions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload