• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Sets in hibernate?

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not change the "reference" of the set that is already present:


 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please relook in hibernate.cfg.xml
 
Bobby Anderson
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic