javahibernatejpahibernate-cascade

hibernate cascading delete with multiple levels


sadly I couldn't find a solution for my current problem. (Please post a link if i missed something)

I have a multiple layer entity structure as following

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<>();
}

When I'm adding Parent.setOfChildA and adding ChildB.grandchild and only saving the Parent entity everything works fine.

But when I'm deleting Parent.setOfChildA and deleting some of ChildB.grandchild I'm getting the following Exception

.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 logs the following sql statements

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

Does anyone have a clue why adding is working but deleting throwing an exception?

Do you need more information on the code?


Solution

  • Nevermind... the error was caused by corrupt data...