0

I am getting these error while integrating the spring-boot with JPA repository here is the code

@CrossOrigin(origins = "http://localhost:4200") @RestController @RequestMapping("/api") public class EmpController { @Autowired private CrudRepo crud; @Autowired private AddrCrudRepo addr; @Autowired private EntityManager entity; //@Autowired //private ModelMapper mapper; private static int count = 0; @Bean public ModelMapper model() { return new ModelMapper(); } //@Autowired // public EmpController(ModelMapper mapper) { // this.mapper = mapper; // } @RequestMapping(path = "/post-addr", method = RequestMethod.POST) public List<AddressModel> postAddr(@Valid @RequestBody List<AddressRequest> addr1){ // crud.findById(id) //AddressModel list = new AddressModel(); EmployeeModel emp = new EmployeeModel(); System.out.println("CALLING THE MAPPER "+addr1); List<AddressModel> addr_list = ObjectMapperUtils.mapAll(addr1, AddressModel.class); System.out.println("CALLED THE MAPPER "+addr_list); addr_list.forEach((a) -> { crud.findById(a.getEmpId()).ifPresent((b) -> { System.out.println(this.count++); a.setEmp_id(b); b.getAddress().add(a); }); }); // AddressModel addr_list = model().map(addr1, AddressModel.class); // // crud.findById(addr1.getEmp_id()).ifPresent((b) -> { // addr_list.setEmp_id(b); // // });`enter code here` System.out.println(addr_list.size()); List<AddressModel> addr3 = addr.saveAll(addr_list); System.out.println(addr3); return addr_list; } 

getting an error in the postAddr method as when it returns the List<AddressModel> and here is the AddressModel

@Entity @Table(name="Emp_Address") public class AddressModel implements Serializable{ @Column(name="address_id") @Id private Integer address_id; @Column(name="city") private String city; @Column(name="states") private String states; @Transient private Integer empId; @ManyToOne @JoinColumn(name="emp_id") private EmployeeModel emp_id; public AddressModel() { } //getter and setter 

and EmployeeModel

@Entity @Table(name="Employee") public class EmployeeModel implements Serializable{ @Column(name="Emp_id") @Id private Integer emp_id; @Column(name="Emp_Name") private String emp_name; @OneToMany(mappedBy="emp_id") private Collection<AddressModel> address = new ArrayList<>(); public EmployeeModel() { } //getter and setters 

so while saveAll is done properly but when the postAddr method returns the List it throws the StackOverflow

1
  • Are you printing the objects on console? Commented Mar 9, 2019 at 5:10

1 Answer 1

2

This StackOverflow error is coming because generated toString methods of both classes are circularly dependent on each other.

EmployeeModel tries to print AddressModel but again AddressModel tries to print EmployeeModel and hence the error.

Try removing AddressModel from toString method of EmployeeModel class or reverse, remove EmployeeModel from toString method of AddressModel class.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.