title: Is it proper for equals() to depend only on an ID?
body: <p>Let's suppose I have class <code>User</code>:</p>
  
  <pre><code>public class User {
    private Long id;
    private String name;
    private Integer age;
    private BigDecimal account;
    // other fields, getters and setters
  }
  </code></pre>
  
  <p>Is it proper to override the <code>equals</code> method as follows?</p>
  
  <pre><code>@Override
  public boolean equals(Object ob) {
     if (ob == null) {
         return false;
     }
     if (this == ob) {
         return true;
     }
     if (ob instanceof User) {
         User other = (User) ob;
         return this.id.equals(other.getId());
     }
     return false;
  }
  </code></pre>
  
  <p>It turns out that the uniqueness of an object is determined only by its ID. But in my application, <code>id</code> is always unique. It's provided at the database. Is my <code>equals</code> implementation competent enough to account for this? Or is this not best practice?</p>
  
  <p>Of course I understand that in this case the <code>hashCode</code> implementation should be the following:</p>
  
  <pre><code>@Override
  public int hashCode() {
     return id.intValue();
  }
  </code></pre>