title: Hibernate/JPA Parent-Child - is it okay for Parent equals()/hashCode() to use the DB Id?
body: <p>Given the following:</p>
  
  <pre><code>@Entity
  public class Parent implements Serializable {
    @Id
    private Long id;
    // mapped ManyToOne below...
    private List&lt;Child&gt; children = new ArrayList&lt;Child&gt;();
  
    ...
  
  }
  </code></pre>
  
  <p>Is it a bad practice to have Parent.equals() and Parent.hashCode() use only id? I understand that Child.equals() and Child.hashCode() should use a immutable set of attributes for a "natural key" for them to be correctly managed by Parent. However, if Parent is always a top-level object (ie it's never the inverse side of any association), is there anything wrong with using only id? </p>
  
  <p>Are there any unwanted effects that could manifest themselves by doing this? I am guessing that maybe if I do this, that when I add a child (or remove), Hibernate won't be able to tell that Parent has changed (and needs to be updated in DB)? In this case, should I use the children property for Parent.equals() and Parent.hashCode()?</p>
  
  <p>I am asking because the Hibernate docs explicity say not to use the @Id property for a "natural key"...</p>