What is the type of cascading type that I can use, and where so that Hibernate automatically deletes the image when there are no more "Things" referencing it? (like Garbagecollecting in Hibernate, basically)
Database: Thing table - a table of images, a lot for one, so many things can refer to the same image.
Entities: Thing, Image
This is a lot, so, for example, 5 things are related to one image.
Now I am doing:
public void delete(Thing thing) { if (countReferences(thing.getImage()) > 1) { thing.setImage(null); } getSession().delete(thing); }
If I do not execute the countReferences function, and there is CascaseType.REMOVE regarding relations, Hibernate also tries to delete the image. A database restriction is triggered when the image is still mentioned somewhere, causing an exception.
So, in short, how can I say that hibernate deletes the image, when the last Thing refers to it, is deleted?
Is an
org.hibernate.event.PreDeleteEventListener
maybe a solution?
source share