deleted object would be re-saved by cascade (remove deleted object from associations)

后端 未结 18 1030
轮回少年
轮回少年 2020-11-30 00:19

i have the following two entities:

1- PlayList:

@OneToMany(fetch = FetchType.EAGER, mappedBy = \"playlist\", orphanRemoval = true, c         


        
相关标签:
18条回答
  • 2020-11-30 01:02

    This problem will happen if you delete using PlaylistadMap modal instead of PlayList. in this case FetchType = Lazy is not the right option. It will not throw any exception but only data from PlaylistadMap will get deleted, the data in PlayList will remain in the table. check that also.

    0 讨论(0)
  • 2020-11-30 01:02

    You need to remove association on the mapping object:

    playList.getPlaylistadMaps().setPlayList(null);
    session.delete(playList);
    
    0 讨论(0)
  • 2020-11-30 01:08

    problem solved after changing the FetchType to Lazy

    0 讨论(0)
  • 2020-11-30 01:08

    If you don't know, which collection holds your object

    In my case it was really hard to apply TomAnderson's solution, since I didn't know what is the collection, which holds a link to an object, so here's the way to know, which objects holds the link to the deleted one: in the debugger you should enter the lowest execution stack level before the exception is thrown, there should be a variable called entityEntry, so you get a PersistenceContext object from this variable: entityEntry.persistenceContext.

    For me persistenceContext was an instance of StatefulPersistenceContext and this implementation has private field parentsByChild, from which you can retrieve information about the collection, which contains the element.

    I was using Eclipse debugger, so it was kinda hard to retrieve this private field in a straight way, so I used Detail Formatter (How can I watch private fields of other objects directly in the IDE when debugging?)

    After getting this information, TomAnderson's solution can be applied.

    0 讨论(0)
  • 2020-11-30 01:10

    The solution is to do exactly what the exception message tells you:

    Caused by: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)

    Remove the deleted object from an associations (sets, lists, or maps) that it is in. In particular, i suspect, from PlayList.PlaylistadMaps. It's not enough to just delete the object, you have to remove it from any cascading collections which refer to it.

    In fact, since your collection has orphanRemoval = true, you don't need to delete it explicitly. You just need to remove it from the set.

    0 讨论(0)
  • 2020-11-30 01:10

    I was able to resolve this by writing the code below. I used executeUpdate instead of .delete()

    def publicSupport = caseObj?.client?.publicSupport
            if(publicSupport)
                PublicSupport.executeUpdate("delete PublicSupport c where c.id = :publicSupportId", [publicSupportId:publicSupport.id])
                //publicSupport.delete()
    
    0 讨论(0)
提交回复
热议问题