我有以下表实体,因为我正在尝试更新现有记录,但这样做时出现以下错误。我知道我在实体定义中犯了一些错误,但无法弄清楚。 感谢您的帮助。
**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;
}
错误:
org.hibernate.HibernateException:具有cascade =“all-delete-orphan”的集合不再被拥有的实体实例引用: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]
最佳答案
在保存模式下,您只需将列表设置为实体即可, 但在更新模式下首先需要清除实体的List,然后将List添加到其中,不要设置List,
处于保存模式=>
person.setAddress(addressList);
处于更新模式=>
person.getAddress().clear();
person.getAddress().addAll(updatedAddressList);
关于java - 实体更新导致错误: cascade ="all-delete-orphan" was no longer referenced by the owning entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60203002/