0

I have a problem with a self bidirectional relationship does not work cascade remove, i have the following entity that what it does is basically put together a family tree with many levels, if I try something like em.remove (family) it does not remove the children and catch that exception "integrity constraint violated - child record found"

public class Family{

  @Id
  public Long id;

  public String name;

  @ManyToOne
  @JoinColumn(name = "id_father")
  public Family father;

  @OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true)
  @JoinColumn(name = "id_father")
  public List<Family> children;

  //getters setters
  //hashcode equals

}

Table

id

name

id_father

1 Answers1

0

First and foremost I don't think that this entity makes much sense, it seems pretty flawed to me. You have a family with a name, a list of children (also of type Family) and one father who can have many families. Wouldn't it be more useful to create an entity Person, who can have one father and a list of children? But this is up to you.

I created a family with 2 children an was able to run your code, it successfully deleted the family with every child. Can you successfully remove a family without any children? Perhaps that's where the problem lies.

Rosty
  • 130
  • 1
  • 7