title: Spring boot OnetoMany with JPA
body: <p>I have started testing with spring boot to create a Restful webservice that has
  simple crud functions.i have two entity classes </p>
  
  <p>Company.java</p>
  
  <pre><code>@Entity
  @Table(name="Company_new")
  public class Company {
  
  @Id
  @NotNull
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int id;
  
  
  
  public Set&lt;User&gt; getUsers() {
      return users;
  }
  public void setUsers(Set&lt;User&gt; users) {
      this.users = users;
  }
  public void setId(int id) {
      this.id = id;
  }
  
  @NotNull
  @Column(name="name")
  private String name;
  
  @OneToMany(mappedBy="company",cascade=CascadeType.ALL)
  private Set&lt;User&gt; users;
  
  
  public Company(String name){
      this.name = name;
  }
  public Company(){
  
  }
  
  public void setName(String name){
      this.name = name;
  }
  
  public String getName(){
      return name;
  }
  
  public int getId(){
      return id;
  }
  }
  </code></pre>
  
  <p>and User.java</p>
  
  <pre><code>  @Entity
    @Table(name="user_new")
    public class User {
    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int idUser;
  
    @NotNull
    @Column
    private String name;
  
    @NotNull
    @Column
    private String userName;
  
    @NotNull
    @Column
    private String authLevel;
  
    @Column
    private String password;
  
    @ManyToOne
    @JoinColumn(name="idCompany")
    private Company company;
  
    // Public methods
  
  
    public Company getCompany(){
        return company;
    }
  
    public void setCompany(Company company){
        this.company = company;
    }
  
  
  
  
    public void setName(String name){
        this.name =name;
    }
  
    public void setUserName(String userName){
        this.userName = userName;
    }
  
   public String getName(){
       return this.name;
   }
  
   public String getUsername(){
       return this.userName;
   }
  
   public String getPassword(){
       return this.password;
   }
  
  
    public String getAuthLevel() {
      return authLevel;
  }
  
  public void setAuthLevel(String authLevel) {
      this.authLevel = authLevel;
  }
  
  public String getUserName() {
      return userName;
  }
  
  public void setId(int idUser) {
      this.idUser = idUser;
  }
  
  public void setPassword(String password) {
      this.password = password;
  }
  
  public int getId(){
        return this.idUser;
    }
  
  
  
  
   }
  </code></pre>
  
  <p>i want to have a relationship with a Company having many users.
  I have tried presisting a user record like this</p>
  
  <pre><code>@Autowired
  CompanyDao companyDao;
  @RequestMapping(value = "/user/create", method = RequestMethod.POST)
  
  public @ResponseBody ResponseEntity createUser(@RequestBody User user) {
  
  
      try {
          Company c = companyDao.findOne(user.getCompany().getId());
          user.setCompany(c);
          userDao.save(user);
  
      } catch (Exception e) {
  
          return new ResponseEntity&lt;&gt;(null, HttpStatus.BAD_REQUEST);
      }
  
      return new ResponseEntity&lt;&gt;(user, HttpStatus.OK);
  }
  </code></pre>
  
  <p>my data is presisting in the database the way i want
  but when i try to access a company record it loads like this</p>
  
  <p><a href="https://i.stack.imgur.com/7N0Kd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7N0Kd.png" alt="respones"></a></p>
  
  <p>obviously it loads relationships in a cycle and eventually gives stack overflow error. how to solve this ?</p>