title: Implementing equals() with JDK Dynamic Proxies
body: <p>For the first time ever, I have to implement my own proxy classes using the standard JDK Dynamic Proxy. It works fairly well, except for one detail: the <code>equals(...)</code> method.</p>
  
  <p>Let's assume that we have a simple Interface like this, which we want to proxy:</p>
  
  <pre><code>public interface MyInterface {
      public String getID();
      public void setID(String id);
  }
  </code></pre>
  
  <p>... and our implementation looks like this (standard Java Bean with generated <code>hashCode()</code> and <code>equals</code>):</p>
  
  <pre><code>public class MyImplementation implements MyInterface {
      private String id;
  
      public String getID() { return this.id; }
      public void setID(String id) { this.id = id; }
  
      // hash code &amp; equals generated by eclipse
  
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + (this.databaseId == null ? 0 :      
          this.id.hashCode());
          return result;
      }
  
      public final boolean equals(final Object obj) {
          if (this == obj) {
              return true;
          }
          if (obj == null) {
              return false;
          }
          if (this.getClass() != obj.getClass()) {
              return false;
          }
          MyImplementation other = (MyImplementation) obj;
          if (this.databaseId == null) {
              if (other.databaseId != null) {
                  return false;
              }
          } else if (!this.databaseId.equals(other.databaseId)) {
              return false;
          }
          return true;
      }
  }
  </code></pre>
  
  <p>The problem is, that when I create a proxy, the <code>equals(...)</code> method is no longer symmetric:</p>
  
  <pre><code>original.equals(original); // true
  proxy.equals(original);    // true, as the proxy forwards the call to the wrapped object
  original.equals(proxy);    // false
  proxy.equals(proxy);       // false
  </code></pre>
  
  <p>This is also discussed in <a href="http://javasixpack.blogspot.co.at/2008/10/proxy-object-and-equals-method.html" rel="noreferrer">this article</a>.</p>
  
  <p>My question is: if I want all four "equals" cases to deliver <code>true</code>, what's the best (i.e. safest and least intrusive) way to go about it?</p>