i have the following two entities:
1- PlayList:
@OneToMany(fetch = FetchType.EAGER, mappedBy = \"playlist\", orphanRemoval = true, c
Kind of Inception going on here.
for (PlaylistadMap playlistadMap : playlistadMaps) {
PlayList innerPlayList = playlistadMap.getPlayList();
for (Iterator<PlaylistadMap> iterator = innerPlayList.getPlaylistadMaps().iterator(); iterator.hasNext();) {
PlaylistadMap innerPlaylistadMap = iterator.next();
if (innerPlaylistadMap.equals(PlaylistadMap)) {
iterator.remove();
session.delete(innerPlaylistadMap);
}
}
}
Because i need FetchType to be EAGER, i remove all associations by setting them to (null) and save the object (that remove all association in the database) then delete it!
That add some milliseconds but it's fine for me, if there better way to keep those MS add your comment bellow..
Hope that help someone :)
I encountered this exception message as well. For me the problem was different. I wanted to delete a parent.
In one transaction:
It turns out that I had to do two separate transactions. I committed after referencing the field in the child. Then started a new commit for the delete.
There was no need to delete the child elements or empty the collections in the parent (assuming orphanRemoval = true
.). In fact, this didn't work.
In sum, this error appears if you have a reference to a field in a child object when that object is being deleted.
Some how all the above solutions did not worked in hibernate 5.2.10.Final.
But setting the map to null as below worked for me:
playlist.setPlaylistadMaps(null);
This post contains a brilliant trick to detect where the cascade problem is:
Try to replace on Cascade at the time with Cascade.None()
until you do not get the error and then you have detected the cascade causing the problem.
Then solve the problem either by changing the original cascade to something else or using Tom Anderson answer.
I was having same issue. I was trying to delete and insert in the same transaction. I added theEntityManager.flush();
after theEntityManager.remove(entity);
.