Exclusion of related tables

Asked

Viewed 28 times

-1

Good morning, I am trying to delete my user class but am having the following error "Cannot delete or update a Parent Row: a Foreign key Constraint fails (pitangdb.phone, CONSTRAINT FKss9h0qo6opj3b1hvh9k3x3o9j FOREIGN KEY (idUser) REFERENCES user (userId))", would have some way to resolve this directly by JPA?

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;

    @Column(nullable = false)
    @Size(min = 2, max = 50)
    private String name;

    @Column(nullable = false)
    @Size(min = 2, max = 100)
    private String email;

    @Column(nullable = false)
    @Size(min = 6, max = 50)
    private String password;
    
    
    @OneToMany(mappedBy = "user", orphanRemoval = true)
    @Column(name = "phones", nullable = false)
    private List<Phone> phones;











@Entity
public class Phone {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idPhone;
    
    
    @ManyToOne(cascade = CascadeType.DETACH)
    @JoinColumn(name = "idUser")
    private User user;
    
    @Column
    private String number;
    
    @Column
    private String typePhone;

1 answer

-1

I played your code and had no problem.

I did the mapping as you did above.

User:

@Entity
@Data
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String email;
    
    @OneToMany(mappedBy = "user", orphanRemoval = true)
    @Column(name = "phones", nullable = false)
    private List<Phone> phones;
}
@Entity
@Data
public class Phone {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idPhone;
    
    @ManyToOne(cascade = CascadeType.DETACH)
    @JoinColumn(name = "idUser")
    private User user;
    
    @Column
    private String number;
    
    @Column
    private String typePhone;
}

I saved the information using a call in a controller:

  @PostMapping(path="/add")
  public @ResponseBody String addNewUser (@RequestParam String name
      , @RequestParam String email) {
    
    User n = new User();
    n.setName(name);
    n.setEmail(email);
    
    Phone p = new Phone();
    p.setUser(n);
    p.setNumber("10101010");
    
    userRepository.save(n);
    phoneRepository.save(p);
    return "Saved";
  }

Then I called this delete and it works by deleting the user and phone.

  @GetMapping(path="/test-delete")
  public @ResponseBody String testDelete() {
      Optional<User> user = userRepository.findById(2L);
      if (user.isPresent())
          userRepository.delete(user.get());
      return "Ok!";
  }
  • I wanted to understand who signaled as a negative answer, which could have been done better in the answer, since I sought to contribute reproducing the code, did not generate error and posted the same here for demonstration.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.