我有下面的表实体,因为我正在尝试更新现有的记录,同时我得到了以下错误。我知道我在实体定义中做了一些错误,但我不能找出它。感谢你的帮助。
**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:所属实体实例com.entity.C.D不再引用具有cascade=的集合
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]发布于 2020-02-13 20:52:38
映射依赖于关联的所有权,考虑到您的示例,很难指出这一点,但您可以检查此post,以便在使用一种或另一种方法时更好地理解数据库中的结果。我不知道您的更新是否是这种情况,但是当您更新childs时,您需要清除您过去的集合,并使用addAll方法来避免此类错误。也要检查这个post。
https://stackoverflow.com/questions/60203002
复制相似问题