title: JPA: java.lang.StackOverflowError on adding toString method in entity classes
body: <p>Everything worked fine until I added <code>toSting()</code> in my entity classes.</p>
  
  <p>After which I start getting the following error in runtime:</p>
  
  <pre><code>Exception in thread "main" java.lang.StackOverflowError
      at java.lang.AbstractStringBuilder.append(Unknown Source)
      at java.lang.StringBuilder.append(Unknown Source)
      at java.lang.StringBuilder.&lt;init&gt;(Unknown Source)
      at entity.Guide.toString(Guide.java:51)
      at java.lang.String.valueOf(Unknown Source)
      at java.lang.StringBuilder.append(Unknown Source)
      at entity.Student.toString(Student.java:45)
          ...
  </code></pre>
  
  <hr>
  
  <pre><code>@Entity
  public class Teacher {
  
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      private Long id;    
  
      private String name;
  
      @OneToMany(mappedBy="teacher", cascade={CascadeType.PERSIST})
      private Set&lt;Student&gt; students = new HashSet&lt;Student&gt;(); 
  
      public Teacher() {}
      public Teacher(String name) {
          this.name = name;
      }
  
      public Set&lt;Student&gt; getStudents() {
          return students;
      }       
      public void addStudent(Student student) {
          students.add(student);
          student.setTeacher(this);
      }
      @Override
      public String toString() {
          return "Teacher[id=" + id + ", name=" + name
                  + ", students=" + students + "]";
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public class SnafuClient {
      public static void main(String[] args) {
  
          EntityManagerFactory emf = Persistence.createEntityManagerFactory("snafu");
          EntityManager em = emf.createEntityManager();
          EntityTransaction txn = em.getTransaction();
  
                  try {
                      txn.begin();
  
                      Query query = em.createQuery("select teacher from Teacher teacher");
                      List&lt;Teacher&gt; teachers = query.getResultList();
  
                      for (Teacher teacher: teachers) {
                  System.out.println(teacher);
              }
  
  
                      txn.commit();
                  }   catch(Exception e) {
                      if(txn != null) { txn.rollback(); }
                      e.printStackTrace();
               }  finally {
                      if(em != null) { em.close(); }
                  }
  
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT: Code for Student entity added</strong></p>
  
  <pre><code>@Entity
  public class Student {
  
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      private Long id;
  
      private String name;
  
      @ManyToOne(cascade={CascadeType.PERSIST, CascadeType.REMOVE})
      @JoinColumn(name="teacher_id")
      private Teacher teacher;
  
      public Student() {}
      public Student(String name, Teacher teacher) {
          this.name = name;
          this.teacher = teacher;
      }
  
      public Teacher getTeacher() {
          return teacher;
      }
      public void setTeacher(Teacher teacher) {
          this.teacher = teacher;
      }
      @Override
      public String toString() {
          return "Student [id=" + id + 
                  + ", name=" + name + ", teacher=" + teacher + "]";
      }   
  
  }
  </code></pre>