• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sets in hibernate?

 
Bobby Anderson
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I.E.


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.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not change the "reference" of the set that is already present:


 
xsunil kumar
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please relook in hibernate.cfg.xml
 
Bobby Anderson
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.E.


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.

Also what does
Please relook in hibernate.cfg.xml
mean???

Thanks
 
denis sorn
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could eventually use Session merge method. I.e. .

[EDIT]

Forget it. I have not read what you wrote. Sorry.

 
Patricia Hall
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic