title: Should the id field of a JPA entity be considered in equals and hashCode?
body: <p>I hit a problem when writing tests for a database application using JPA2 and EclipseLink:</p>
  
  <p>I add some entity to a database, retrieve it later and want to compare it to an instance  which has the values I expect to confirm that the addition worked as I intended.</p>
  
  <p>First I wrote something like</p>
  
  <pre><code>assertEquals(expResult, dbResult);
  </code></pre>
  
  <p>which failed, because I can't really know the value of <code>id</code> field, which is generated by the database and therefore <code>dbResult</code> differs from <code>expResult</code> which I created with <code>new</code> and populated manually.</p>
  
  <p>I see two options:</p>
  
  <ul>
  <li><p>Either I remove the <code>id</code> field from <code>equals</code> and <code>hashCode</code> so that the comparison is only based on the "real values". I don't know if this causes problems in the database or elsewhere, though.</p></li>
  <li><p>Or I write my tests to explicitly check every field except <code>id</code> manually.</p></li>
  </ul>
  
  <p>What should I do?</p>