title: Could not write JSON: Infinite recursion (StackOverflowError); nested exception spring boot
body: <p>This is my District Controller, when I try to fetch data after saving I get the error, even when I try get object form <code>getDistrict(Long id)</code> the same strikes please suggest some way, am very new at spring environment:</p>
  
  <pre><code>    package com.gad.services;
  
      import java.util.List;
  
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
  
      import com.gad.repositories.DistrictMasterRepositories;
      import com.gad.rmodels.Districtmaster;
      import com.gad.rmodels.Statemaster;
  
      @Service
      public class DistricMasterServices {
  
          @Autowired
          DistrictMasterRepositories districtMasterRepositories;
          @Autowired
          StateMasterServices stateMasterServices;
          List&lt;Districtmaster&gt; districtmaster;
  
          public Iterable&lt;Districtmaster&gt; savenewdistrict(Long id,Districtmaster districtmaster_rec){
               System.out.println(id);
               Statemaster statemaster=null;
               statemaster = stateMasterServices.getStateById(id);
               System.out.println("savenewdistrict");
  
  
  
                  districtmaster_rec.setStatemaster(statemaster);
                  districtMasterRepositories.save(districtmaster_rec);
                      Iterable&lt;Districtmaster&gt;districtmaster2 = districtMasterRepositories.findAll();
                      return  districtmaster2;
  
  
          }
  
  
          public Districtmaster  getDistrict(Long id){
              Districtmaster districtmaster =  districtMasterRepositories.findOne(id);
              return districtmaster;
  
          }
      }
  </code></pre>
  
  <p>The model class for state:</p>
  
  <pre><code> package com.gad.rmodels;
      import static javax.persistence.GenerationType.SEQUENCE;
      import java.util.HashSet;
      import java.util.Set;
      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.OneToMany;
      import javax.persistence.SequenceGenerator;
      import javax.persistence.Table;
  
      /**
       * Statemaster generated by hbm2java
       */
      @Entity
      @Table(name="statemaster"
          ,schema="aop_gad_v1"
      )
      public class Statemaster  implements java.io.Serializable {
  
  
           private long id;
           private String stateName;
           private Set&lt;Districtmaster&gt; districtmasters = new HashSet&lt;Districtmaster&gt;(0);
  
          public Statemaster() {
          }
  
  
          public Statemaster(long id) {
              this.id = id;
          }
          public Statemaster(long id, String stateName, Set&lt;Districtmaster&gt; districtmasters) {
             this.id = id;
             this.stateName = stateName;
             this.districtmasters = districtmasters;
          }
  
  
  
          @SequenceGenerator(name="generator_statemasterid", sequenceName="aop_gad_v1.gad_statemaster_seq")
          @Id 
          @GeneratedValue(strategy=SEQUENCE, generator="generator_statemasterid")
          @Column(name="id", unique=true, nullable=false)
          public long getId() {
              return this.id;
          }
  
          public void setId(long id) {
              this.id = id;
          }
  
          @Column(name="state_name", length=20)
          public String getStateName() {
              return this.stateName;
          }
  
          public void setStateName(String stateName) {
              this.stateName = stateName;
          }
  
      @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="statemaster")
          public Set&lt;Districtmaster&gt; getDistrictmasters() {
              return this.districtmasters;
          }
  
          public void setDistrictmasters(Set&lt;Districtmaster&gt; districtmasters) {
              this.districtmasters = districtmasters;
          }
  
  
  
  
      }
  </code></pre>
  
  <p>Distric model:</p>
  
  <pre><code>package com.gad.rmodels;
  
  
  import static javax.persistence.GenerationType.SEQUENCE;
  
  import java.util.HashSet;
  import java.util.Set;
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.OneToMany;
  import javax.persistence.SequenceGenerator;
  import javax.persistence.Table;
  
  /**
   * Districtmaster generated by hbm2java
   */
  @SuppressWarnings("serial")
  @Entity
  @Table(name="districtmaster",schema="aop_gad_v1")
  public class Districtmaster  implements java.io.Serializable {
  
  
       private long id;
       private Statemaster statemaster;
       private String districtName;
       private Set&lt;GadGuestHouseMaster&gt; gadGuestHouseMasters = new HashSet&lt;GadGuestHouseMaster&gt;(0);
  
      public Districtmaster() {
      }
  
  
      public Districtmaster(long id) {
          this.id = id;
      }
      public Districtmaster(long id, Statemaster statemaster, String districtName, Set&lt;GadGuestHouseMaster&gt; gadGuestHouseMasters) {
         this.id = id;
         this.statemaster = statemaster;
         this.districtName = districtName;
         this.gadGuestHouseMasters = gadGuestHouseMasters;
      }
  
  
       @SequenceGenerator(name="generator_districtmasterid", sequenceName="aop_gad_v1.gad_districtmasterid_seq")
       @Id 
       @GeneratedValue(strategy=SEQUENCE, generator="generator_districtmasterid")
       @Column(name="id", unique=true, nullable=false)
      public long getId() {
          return this.id;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  @ManyToOne(fetch=FetchType.LAZY)
  
      @JoinColumn(name="district_of_state")
      public Statemaster getStatemaster() {
          return this.statemaster;
      }
  
      public void setStatemaster(Statemaster statemaster) {
          this.statemaster = statemaster;
      }
  
      @Column(name="district_name", length=20)
      public String getDistrictName() {
          return this.districtName;
      }
  
      public void setDistrictName(String districtName) {
          this.districtName = districtName;
      }
      @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="districtmaster")
      public Set&lt;GadGuestHouseMaster&gt; getGadGuestHouseMasters() {
          return this.gadGuestHouseMasters;
      }
  
      public void setGadGuestHouseMasters(Set&lt;GadGuestHouseMaster&gt; gadGuestHouseMasters) {
          this.gadGuestHouseMasters = gadGuestHouseMasters;
      }
  
  
  
  
  }
  </code></pre>
  
  <p>The Error I get:</p>
  
  <blockquote>
    <p>[{"timestamp":1512641978311,"status":200,"error":"OK","exception":"org.springframework.http.converter.HttpMessageNotWritableException","message":"Could
    not write JSON: Infinite recursion (StackOverflowError); nested
    exception is com.fasterxml.jackson.databind.JsonMappingException:
    Infinite recursion (StackOverflowError) (through reference chain:
    com.gad.rmodels.Statemaster[\"districtmasters\"]->org.hibernate.collection.internal.PersistentSet[0]-</p>
  </blockquote>