Search code examples
javahibernateannotations

grandchild object does not updated


I have a parent, a child and a grandchild object. The problem is, when I delete the grandchildren in the application and save the parent object, grandchildren changes don't be updated.

In Parent Class

  @OneToMany(fetch = FetchType.EAGER, mappedBy = "parent")
  @ForeignKey(name = "FK_1")
  @Cascade({CascadeType.ALL})
  private Set<Child> children;

In Child Class

  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "parent")
  @XmlTransient
  private parent parent;

  @OneToMany(fetch = FetchType.EAGER, mappedBy = "child")
  @ForeignKey(name = "FK_2")
  @Cascade({CascadeType.ALL})
  private Set<GrandChild> grandchildren;

In GrandChild Class

  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "child", nullable = false)
  @XmlTransient
  private Child child;

I use hibernate 4.2.2.Final.

any idea?


Solution

  • try adding orphanRemoval to your collections.

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "parent", orphanRemoval=true)
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "child", orphanRemoval=true)