I have a method in Controller:
@RequestMapping(value = "/children", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public void createChild(@RequestBody CreateChildRequestDTO childDTO) { Long guardianId = Long.parseLong(childDTO.getGuardianId()); Child child = childDTO.getChild(); Guardian guardian = guardianService.findById(guardianId).get(); boolean isExist = childService.isExist(child.getChildFullName(), child.getPassword(), child.getPhoneNumber()); if (isExist) { child = childService.findChildByNameAndPass(child.getChildFullName(), child.getPassword()); guardian.addChild(child); } else { guardian.addChild(child); childService.createChild(child); } } And also I have created a many-to-many relationship in Guradian class:
@ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "guardian_child", schema = "cheer", joinColumns = @JoinColumn(name = "guardianid"), inverseJoinColumns = @JoinColumn(name = "childid")) private List<Child> children = new ArrayList<>(); When I run the code and execute line guardian.addChild(child) child is added to guardian's list, but not created a new record in table. How to fix it? Commit change in objects relation?
elseis working. And new record adds to guardian_child tablechildService.createChild(child);I am not sure how you expect that to be answered. I would guess because childService saves the child??