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)
private Set<ProductInfo> productInfos = new HashSet<>();

@JoinColumn(name = "username", referencedColumnName = "username")
private User user;

springboot (Controller)

public ResponseEntity<HttpStatus> deleteUserProduct(@PathVariable("id") long id) {
    try {
        User user = userRepository.findById(id).get();
        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