When creating a complete new Set of entities and storing that set using the entity's set method I thought that hibernate would pick up that fact that since this is a new set all other elements in the old set (db rows) will be deleted.
I.E. A document contains many text items
I want to be able to store text items for a doc and have hibernate delete all old text items for that doc.
Why is this not deleting the 10 old text items since I used a new set with just one item in it?
Is this a problem because the relationship is bi-directional.
I am confused the setTextItems(Set<TextItem> items) takes a Set object. I thought that the old set would be completely overridden when you set a new object of type Set.
I am replacing the entire set not just one element, right? When debugging I can see the object change, but hibernate is not deleting all the old TextItems that were in the old list. Also the code posted as a solution does not work either, i.e. just clearing the list. I actually have to iterate though the list and remove all the textItems from the db.
I think this is due to the bidirectional dependency. I.E. a TextItem has a reference back to a Doc and a Doc has a set of text items.
This is a JPA "feature". There is a Hibernate annotation that supports automatic deletion of orphan records, however it is not supported by JPA, so it is Hibernate specific. Unfortunately, I don't know what the annotation is, because where I work we try to stick with JPA code.
The items that were in the set, but no longer are, are not deleted from the database. JPA only breaks the link between the parent entity and the data record in the child entity.
We have to put in code that deletes the orphans. Hope this helps.
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad