title: Stackoverflow error when saving an Object's toString value - Java/Hibernate/Spring
body: <p>I have the following sample entities:</p>
  <p><strong>Institution</strong></p>
  <pre><code>@Data
  @Entity
  @NoArgsConstructor
  @EntityListeners(InstitutionAuditListener.class)
  public class Institution extends Auditable&lt;String&gt; {
  
      @OneToMany(mappedBy = &quot;institution&quot;, cascade = CascadeType.ALL)
      @JsonManagedReference
      private List&lt;RegisteredProgram&gt; registeredPrograms;
  
      private String name;
  }
  </code></pre>
  <p><strong>RegisteredProgram</strong></p>
  <pre><code>@Data
  @NoArgsConstructor
  @Entity
  @EntityListeners(RegisteredProgramAuditListener.class)
  public class RegisteredProgram extends Auditable&lt;String&gt; {
  
     private String name;
  
     @ManyToOne
     @JoinColumn(name = &quot;institution_id&quot;, nullable = false)
     @JsonBackReference
     private Institution institution;
  
     @Embedded
     private RegistrationRequirement registrationRequirement;
  
     @OneToMany(mappedBy = &quot;registeredProgram&quot;, cascade = CascadeType.ALL)
     @JsonManagedReference
     private List&lt;Official&gt; officialList;
  }
  </code></pre>
  <p><strong>Official</strong></p>
  <pre><code>@Data
  @Entity
  @EntityListeners(OfficialAuditListener.class)
  @NoArgsConstructor
  public class Official extends Auditable&lt;String&gt; {
  
      private String name;
      private String position;
  
      @ManyToOne
      @JoinColumn(name = &quot;REGISTERED_PROGRAM_ID&quot;, nullable = false)
      @JsonBackReference
      private RegisteredProgram registeredProgram;
  }
  </code></pre>
  <p><strong>Audit Log</strong></p>
  <pre><code>@Entity
  @Data
  @NoArgsConstructor
  public class AuditLog extends AuditBase {
  
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      private Long id;
  
      private Long entityId;
  
      @Type(type = &quot;yes_no&quot;)
      private Boolean isDeleted = false;
  
      @Column(columnDefinition = &quot;json&quot;)
      @Convert(converter = JpaConverterJson.class)
      private String jsonObject;
  }
  </code></pre>
  <p>When I attempted to save an instance of <strong>Official</strong>'s object <em>toString()</em> value into <strong>AuditLog</strong>'s <em><strong>jsonObject</strong></em> field, I encountered the error below:</p>
  <pre><code>java.lang.StackOverflowError: null
      at java.base/java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:173) ~[na:na]
      at java.base/java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:538) ~[na:na]
      at java.base/java.lang.StringBuilder.append(StringBuilder.java:174) ~[na:na]
      at com.tesda8.region8.program.registration.model.entities.RegisteredProgram.toString(RegisteredProgram.java:26) ~[classes/:na]
      at java.base/java.lang.String.valueOf(String.java:2951) ~[na:na]
      at java.base/java.lang.StringBuilder.append(StringBuilder.java:168) ~[na:na]
      at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:473) ~[na:na]
      at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
      at java.base/java.lang.String.valueOf(String.java:2951) ~[na:na]
      at java.base/java.lang.StringBuilder.append(StringBuilder.java:168) ~[na:na]
      at com.tesda8.region8.program.registration.model.entities.Institution.toString(Institution.java:23) ~[classes/:na]
      at java.base/java.lang.String.valueOf(String.java:2951) ~[na:na]
      at java.base/java.lang.StringBuilder.append(StringBuilder.java:168) ~[na:na]
      at com.tesda8.region8.program.registration.model.entities.RegisteredProgram.toString(RegisteredProgram.java:26) ~[classes/:na]
  </code></pre>
  <p>I was wondering if there are any annotations in spring/hibernate that could prevent this issue to occur or is there any other way that i could save the object's (JSON) value to an object's column in spring? I was assuming that <code>@JsonManagedReference</code> and <code>@JsonBackReference</code> should've prevented this issue to happen.</p>
  <p><strong>UPDATE</strong></p>
  <ol>
  <li>I already tried to used <em><strong>toString()</strong></em> method for objects through Lombok's <code>@Data</code> annotation</li>
  <li>I also tried to use <code>String.valueOf(official)</code></li>
  </ol>