i have the following two entities:
1- PlayList:
@OneToMany(fetch = FetchType.EAGER, mappedBy = \"playlist\", orphanRemoval = true, c
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.
You need to remove association on the mapping object:
playList.getPlaylistadMaps().setPlayList(null);
session.delete(playList);
problem solved after changing the FetchType to Lazy
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.
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.
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()