I want to remove the child from the parent in a 1:N relationship. Below is the code I wrote.
springboot (JPA)
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, orphanRemoval = true) @JsonManagedReference private Set<ProductInfo> productInfos = new HashSet<>(); @ManyToOne @JoinColumn(name = "username", referencedColumnName = "username") @JsonBackReference private User user;
springboot (Controller)
@DeleteMapping("/productSetting/{id}") public ResponseEntity<HttpStatus> deleteUserProduct(@PathVariable("id") long id) { try { User user = userRepository.findById(id).get(); productInfoRepository.deleteAll(user.getProductInfo()); System.out.println(">>>>>>>>>>>>>>"); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } }
This code works. However, I want to get the product’s id and delete only the selected id. Is there a way?
Anonymous Changed status to publish May 13, 2021
Recent Comments