title: Implementing equals and hashCode for objects with circular references in Java
body: <p>I have two classes defined such that they both contain references to the other object. They look similar to this (this is simplified; in my real domain model class A contains a list of B and each B has a reference back to parent A):</p>
  
  <pre><code>public class A {
  
      public B b;
      public String bKey;
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((b == null) ? 0 : b.hashCode());
          result = prime * result + ((bKey == null) ? 0 : bKey.hashCode());
          return result;
      }
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (!(obj instanceof A))
              return false;
          A other = (A) obj;
          if (b == null) {
              if (other.b != null)
                  return false;
          } else if (!b.equals(other.b))
              return false;
          if (bKey == null) {
              if (other.bKey != null)
                  return false;
          } else if (!bKey.equals(other.bKey))
              return false;
          return true;
      }
  }
  
  public class B {
  
      public A a;
      public String aKey;
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((a == null) ? 0 : a.hashCode());
          result = prime * result + ((aKey == null) ? 0 : aKey.hashCode());
          return result;
      }
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (!(obj instanceof B))
              return false;
          B other = (B) obj;
          if (a == null) {
              if (other.a != null)
                  return false;
          } else if (!a.equals(other.a))
              return false;
          if (aKey == null) {
              if (other.aKey != null)
                  return false;
          } else if (!aKey.equals(other.aKey))
              return false;
          return true;
      }
  }
  </code></pre>
  
  <p>The <code>hashCode</code> and <code>equals</code> have been generated by Eclipse using both fields of both A and B. The problem is that calling the <code>equals</code> or <code>hashCode</code> method on either object results in a <code>StackOverflowError</code> since they both call the other object's <code>equals</code> and <code>hashCode</code> method. For example the following program will fail with <code>StackOverflowError</code> using the above objects:</p>
  
  <pre><code>    public static void main(String[] args) {
  
          A a = new A();
          B b = new B();
          a.b = b;
          b.a = a;
  
          A a1 = new A();
          B b1 = new B();
          a1.b = b1;
          b1.a = a1;
  
          System.out.println(a.equals(a1));
      }
  </code></pre>
  
  <p>If there is something inherently wrong with having a domain model defined with circular relationships in this way then please let me know. As far as I can tell though this is a fairly common scenario, correct?</p>
  
  <p>What is best practice for defining <code>hashCode</code> and <code>equals</code> in this case? I want to keep all fields in the <code>equals</code> method so that it is a true deep equality comparison on the object but I don't see how I can with this problem. Thanks!</p>