title: StackOverflowError while retrieving User list
body: <p>I was trying to get a list of users using bootstrap's datatables but I keep getting  <code>StackOverflowError</code>. Below is my code.</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>@RequestMapping(value = "/admin/login_management", method = RequestMethod.GET)
  public ModelAndView loginMgt(HttpServletRequest request, ModelMap m) {
      m.put("login_management", true);
      return new ModelAndView("login_management");
  }
  </code></pre>
  
  <p><strong>(Datatables Object)</strong></p>
  
  <pre><code>@RequestMapping(value = "/admin/usersList", method = RequestMethod.GET, produces = "application/json")
  public @ResponseBody Object userListPaginationDataTables(@RequestParam(value = "sEcho", required = false) String sEcho,
                                                              @RequestParam(value = "sSearch", required = false) String sSearch,
                                                              @RequestParam(value = "sColumns", required = false) String sColumns,
                                                              @RequestParam(value = "iDisplayStart", required = false) Integer iDisplayStart,
                                                              @RequestParam(value = "iDisplayLength", required = false) Integer iDisplayLength,
                                                              @RequestParam(value = "iColumns", required = false) Integer iColumns,
                                                              @RequestParam(value = "iSortCol_0", required = false) Integer iSortCol_0,
                                                              @RequestParam(value = "iSortColumnIndex", required = false) Integer iSortColumnIndex,
                                                              @RequestParam(value = "sSortDir_0", required = false) String sSortDir_0)
                                                                      throws IOException {
          String json = "";
  
          List&lt;User&gt; result = new ArrayList&lt;&gt;();
  
          result = userService.getUserList(iDisplayStart, iDisplayLength);
  
          int total = userService.getUserTotalSize();
  
          json = getJSonString(result, total);
  
  
      return json;
  }
  
  private static String getJSonString(List&lt;?&gt; result, int total){
  
      String json="";
  
      try {
          DataTablesObject dataTablesObject = new DataTablesObject();
          dataTablesObject.setAaData(result);
          if(total &gt; 0) {
              dataTablesObject.setiTotalRecords(total);
              dataTablesObject.setiTotalDisplayRecords(total);
          }
  
          Gson gson = new GsonBuilder().setPrettyPrinting().create();
  
          json = gson.toJson(dataTablesObject);
  
      } catch (Exception ex) {
  
          LOGGER.fatal(ex);
      }
  
      return json;
  }
  </code></pre>
  
  <p><strong>login_management.jsp:</strong></p>
  
  <pre><code>&lt;script&gt;
      $(function() {          
          $("#example2").dataTable({
              "bServerSide": true,
              "sAjaxSource": "${pageContext.request.contextPath}/admin/usersList",
              "bProcessing": true,
              "bFilter":false,
              "sPaginationType": "bootstrap",
              "bJQueryUI": true,                  
              "aoColumns": [
                  { "mData": "id" },
                  { "mData": "login" },
                  {
                      "mData": null,
                      "mRender": function (data, type, full) {
                          //alert(data);
                          return "&lt;form method='post' action='${pageContext.request.contextPath}/admin/editUser?id=1'&gt;"+
                                      "&lt;button type='submit'class='btn btn-primary'&gt;Edit&lt;/button&gt;"+
                                  "&lt;/form&gt;";
  
                      },
                      "fnCreatedCell": function ( cell ) {
                          $('input, label', cell)
                              .button( {
                                  text: true
                              } );
  
                      }
                  }
  
              ]
          });
      });
      &lt;/script&gt;
  </code></pre>
  
  <p>Models:</p>
  
  <pre><code>@Entity
  @Table(name="user")
  public class User {
  
  @Id
  @GeneratedValue
  private Integer id;
  
  private String login;
  
  private String password;
  
  
  @OneToOne(cascade=CascadeType.ALL, fetch =FetchType.EAGER)
  @JoinTable(name="user_roles",
      joinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")},
      inverseJoinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")}
  )
  private Role role;
  
  //...getters &amp; setters...
  }
  </code></pre>
  
  <p>Roles:</p>
  
  <pre><code>@Entity
  @Table(name="roles")
  public class Role {
  
  @Id
  @GeneratedValue
  private Integer id;
  
  private String role;
  
  @OneToMany(cascade=CascadeType.ALL, fetch =FetchType.EAGER)
  @JoinTable(name="user_roles", 
      joinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")},
      inverseJoinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")}
  )
  private Set&lt;User&gt; userRoles;
  //...getters and setters...
  }
  
  @Entity
  @Table(name="user_roles")
  public class UserRole {
  
  @Id
  @Column(name="user_id")
  private Integer id;
  
  @Column(name="role_id")
  private Integer roleId;
  //...getters and setters...
  }
  </code></pre>
  
  <p>However after I compiled and startup my tomcat, I was getting this error:</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [dispatcher] in context with path  threw exception     [Handler processing failed; nested exception is java.lang.StackOverflowError] with root cause
  java.lang.StackOverflowError
  at com.google.gson.stream.JsonWriter.beforeName(JsonWriter.java:580)
  at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:401)
  at com.google.gson.stream.JsonWriter.value(JsonWriter.java:495)
  at com.google.gson.internal.bind.TypeAdapters$7.write(TypeAdapters.java:246)
  at com.google.gson.internal.bind.TypeAdapters$7.write(TypeAdapters.java:231)
  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:91)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:206)
  at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:894)
  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:91)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:206)
  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:91)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:206)
  at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:894)
  </code></pre>
  
  <p>What is causing this?</p>