2

I have following table entities, In that I am trying update an existing record, while doing so i get following error. I know I'm doing some mistake in the entity definition but not able to figure it out. Appreciate your help.

                **A
        -------------------
        |                 |
        B                 C
                      ---------
                      |       |
                      D       E**

@Entity
@Table()
public class A {
    @Id
    Long id;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
    @JoinColumn(name = "id", referencedColumnName="id",  nullable = false)
    private Set<B> b;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
    @JoinColumn(name = "id", referencedColumnName="ID",  nullable = false)  
    private Set<C> c;
}

@Entity
@Table()
public class B{
    @Id
    Long id;
}

@Entity
@Table()
public class C{
    @Id
    Long id;
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
    @JoinColumn(name = "id", referencedColumnName="id",  nullable = false)
    private Set<D> D;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
    @JoinColumn(name = "id", referencedColumnName="ID",  nullable = false)  
    private Set<E> E;
}

@Entity
@Table()
public class D{
    @Id
    Long id;
}

@Entity
@Table()
public class E{
    @Id
    Long id;
}

ERROR:

org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.entity.C.D

at org.hibernate.engine.internal.Collections.processDereferencedCollection(Collections.java:100) ~[hibernate-core-5.3.12.Final.jar:5.3.12.Final]
at org.hibernate.engine.internal.Collections.processUnreachableCollection(Collections.java:51) ~[hibernate-core-5.3.12.Final.jar:5.3.12.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.flushCollections(AbstractFlushingEventListener.java:262) ~[hibernate-core-5.3.12.Final.jar:5.3.12.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:95) ~[hibernate-core-5.3.12.Final.jar:5.3.12.Final]
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
NivedhaLak
  • 199
  • 1
  • 1
  • 10

2 Answers2

4

in save mode you just set the List to the entity and it is work, but in update mode first you need to clear the entity's List and then add the List to it, do not set the List,

in save mode =>

person.setAddress(addressList);

in update mode =>

person.getAddress().clear();
person.getAddress().addAll(updatedAddressList);
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
3

The mapping depends of the ownership of association, considering your example, it's difficult to point out, but you can check this post to understand better when use one or another approach and the consequences in the database. I don't know if this is the case of your update, but when you update the childs you need to clear your past collection and use the addAll method to avoid this kind of error. Check this post as well.