实体更新导致错误:所属实体不再引用级联=“所有删除的孤儿”

问题描述 投票:0回答:1

我具有以下表实体,因为我正在尝试更新现有记录,但同时出现以下错误。我知道我在实体定义中犯了一些错误,但无法弄清楚。感谢您的帮助。

                **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]
java hibernate spring-boot spring-data-jpa hibernate-5.x
1个回答
0
投票

映射取决于关联的所有权,考虑您的示例,很难指出,但是您可以选中post以更好地理解使用一种或另一种方法时的后果以及数据库中的后果。我不知道这种情况是否发生在您的更新中,但是在更新子项时,您需要清除过去的集合并使用addAll方法来避免这种错误。还要检查此post

© www.soinside.com 2019 - 2024. All rights reserved.