首页 > 解决方案 > 具有多个级别的休眠级联删除

问题描述

可悲的是,我找不到解决当前问题的方法。(如果我错过了什么,请发布链接)

我有一个多层实体结构如下

class Parent {

    @OneToMany(
            mappedBy = "parent",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private Set<ChildA> setOfChildA = new HashSet<>();

    @OneToMany( fetch = FetchType.EAGER, mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true )
    private Set<ChildB> setOfChildB = new HashSet<>();
}

class ChildB {
    @OneToMany( fetch = FetchType.LAZY, mappedBy = "childb", cascade = CascadeType.ALL, orphanRemoval = true )
    private Set<grandchild> grandchild = new HashSet<>();
}

当我添加Parent.setOfChildA和添加ChildB.grandchild并且只保存父实体时,一切正常。

但是当我删除Parent.setOfChildA和删除一些ChildB.grandchild我得到以下异常

.m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.orm.jpa.JpaSystemException: Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1; nested exception is org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1]

Hibernate 记录以下 sql 语句

delete 
        from
            tbl_childa
        where
            childa_linkparentid=? 
            and childa_linkotherentityid=?
delete 
        from
            tbl_grandchild
        where
            grandchild_id=? 
            and grandchild_version=?

有谁知道为什么添加有效但删除抛出异常?

您需要有关代码的更多信息吗?

标签: javahibernatejpahibernate-cascade

解决方案


没关系...错误是由损坏的数据引起的...


推荐阅读