SamWiicks
SamWiicks

Reputation: 95

Spring boot OnetoMany with JPA

I have started testing with spring boot to create a Restful webservice that has simple crud functions.i have two entity classes

Company.java

@Entity
@Table(name="Company_new")
public class Company {

@Id
@NotNull
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;



public Set<User> getUsers() {
    return users;
}
public void setUsers(Set<User> 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<User> 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;
}
}

and User.java

  @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;
  }




 }

i want to have a relationship with a Company having many users. I have tried presisting a user record like this

@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<>(null, HttpStatus.BAD_REQUEST);
    }

    return new ResponseEntity<>(user, HttpStatus.OK);
}

my data is presisting in the database the way i want but when i try to access a company record it loads like this

respones

obviously it loads relationships in a cycle and eventually gives stack overflow error. how to solve this ?

Upvotes: 1

Views: 2309

Answers (1)

miensol
miensol

Reputation: 41608

Depending on your desired outcome you can:

  • use @JsonIgnore on public Set<User> getUsers() to prevent serializing the users collection or @JsonIgnore on public Company getCompany() to prevent company serialization

  • use @JsonManagedReference on public Set<User> getUsers() and @JsonBackReference on public Company getCompany() to let Jackson know that it's a bidirectional relation.

PS. If the API you're exposing will be consumed by code you do not control consider not exposing entities directly but mapping them to DTOs

Upvotes: 3

Related Questions