title: java, playframework, ajax, json - Infinite recursion occurs when converting Object to JSON
body: <p>I need to convert this Object List to JSON so i can pass it to my AJAX. </p>
  
  <p>So far I've got (using java playframework)</p>
  
  <hr>
  
  <h2>Application.java</h2>
  
  <pre><code>@Transactional
  public static Result fetchOptions(String strCategoryId, String strCategoryName) {
  
      Main mainAppModel = new Main();
      mainAppModel.populateOptions(strCategoryId, strCategoryName);
      return ok(Json.toJson(mainAppModel));
    }
  </code></pre>
  
  <p><br></p>
  
  <h2>Main.java</h2>
  
  <pre><code>  public void populateCountryOptions() {
      Countries countriesDb = new Countries();
      lstCountries = countriesDb.getCountriesByRegion(strRegionId);
    }
  
    public void populateLocationOptions() {
      Locations locationsDb = new Locations();
      lstLocations = locationsDb.getLocationByCountry(strCountryId);
    }
  
    public void populateDepartmentOptions() {
      Departments departmentsDb = new Departments();
      lstDepartments = departmentsDb.getDepartmentByLocationId(strLocationId);
    }
  
    public void populateOptions(String strCategoryId, String strCategoryName) {
      switch (strCategoryName) {
        case "region":
          strRegionId = strCategoryId;
          populateCountryOptions();
          break;
        case "country":
          strCountryId = strCategoryId;
          populateLocationOptions();
          break;
        case "location":
          strLocationId = strCategoryId;
          populateDepartmentOptions();
          break;
        default:
          break;
      }
    }
  </code></pre>
  
  <p><br></p>
  
  <h2>conf/routes</h2>
  
  <pre><code>POST    /fetchOptions/:strCategoryId/:strCategoryName   controllers.Application.fetchOptions(strCategoryId: String, strCategoryName: String)
  </code></pre>
  
  <p><br></p>
  
  <h2>Departments.java (removed getters and setters)</h2>
  
  <pre><code>package models.db;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  import play.db.jpa.JPA;
  
  @Table(name = "DEPARTMENTS")
  @Entity
  public class Departments {
  
    @Id
    @Column(name = "DEPARTMENT_ID")
    private String strDepartmentId;
  
    @Column(name = "DEPARTMENT_NAME")
    private String strDepartmentName;
  
    @Column(name = "MANAGER_ID")
    private String strManagerId;
  
    @Column(name = "LOCATION_ID")
    private String strLocationId;
  
  
    @SuppressWarnings("unchecked")
    public List&lt;Departments&gt; getDepartmentByLocationId(String strLocationId) {
  
      StringBuilder sb = new StringBuilder();
      sb.append(" SELECT ");
      sb.append("         DEPARTMENT_ID ");
      sb.append("         , DEPARTMENT_NAME ");
      sb.append("         , MANAGER_ID ");
      sb.append("         , LOCATION_ID ");
      sb.append(" FROM  ");
      sb.append("         DEPARTMENTS ");
      sb.append(" WHERE ");
      sb.append("         LOCATION_ID = '" + strLocationId + "'");
      sb.append(" ORDER BY  ");
      sb.append("         DEPARTMENT_NAME ASC ");
  
      List&lt;Departments&gt; lstDepartments = new ArrayList&lt;Departments&gt;();
      try {
        lstDepartments = JPA.em().createNativeQuery(sb.toString(), Departments.class).getResultList();
      } catch (Exception e) {
        // TODO: handle exception
      }
  
      return lstDepartments;
  
    }
  }
  </code></pre>
  
  <p><br></p>
  
  <h2>Countries.java (for comparison)</h2>
  
  <pre><code>package models.db;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  import play.db.jpa.JPA;
  
  @Table(name = "COUNTRIES")
  @Entity
  public class Countries {
  
  
    @Id
    @Column(name = "COUNTRY_ID")
    private String strCountryId;
  
    @Column(name = "COUNTRY_NAME")
    private String strCountryName;
  
    @Column(name = "REGION_ID")
    private String strRegionId;
  
    @SuppressWarnings("unchecked")
    public List&lt;Countries&gt; getCountriesByRegion(String strRegionId) {
  
      StringBuilder sb = new StringBuilder();
      sb.append(" SELECT ");
      sb.append("         COUNTRY_ID ");
      sb.append("         , COUNTRY_NAME ");
      sb.append("         , REGION_ID ");
      sb.append(" FROM  ");
      sb.append("         COUNTRIES ");
      sb.append(" WHERE ");
      sb.append("         REGION_ID = '" + strRegionId + "'");
      sb.append(" ORDER BY ");
      sb.append("         COUNTRY_NAME ASC ");
  
      List&lt;Countries&gt; lstCountries = new ArrayList&lt;Countries&gt;();
      try {
        lstCountries = JPA.em().createNativeQuery(sb.toString(), Countries.class).getResultList();
      } catch (Exception e) {
        // TODO: handle exception
      }
  
      return lstCountries;
    }
  
  
  }
  </code></pre>
  
  <hr>
  
  <p><br>
  I think javascript is not needed anymore since the error occurs on returning <strong>mainAppModel</strong> object as <strong>Json</strong>. 
  <br>
  So I have 3 methods on <em>Main.java</em> using the same method on <em>Application.java</em>. if I use <strong>populateCountryOptions()</strong> and <strong>populateLocationOptions()</strong> Json works fine, but whenever I use <strong>populateDepartmentOptions()</strong> infinite recursion occurs. The model looks similar to each other. I don't know what causes this error. Thanks for your help!</p>