• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate child parent update

 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


any ideas


i have 2 tables, tableParent and tableChild,


tableChild is a child table of tableParent,


the relationship is 1-to-many and in tableChild there is a field "tableParentId",

i want to do an update on tableParent and there will be new rows in tableChild,

but what i'm trying to do is:
* remove previous rows in tableChild that corresponded to tableParent
* update tableParent
* insert new rows into tableChild with details of the updated tableParent


any ideas?


Cheers in advance,

Niall
 
Author
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just need to wipe out the object collection in the parent object that refereces the child class (and in turn the child table). You should be able to either instantiate a new collection, or just clear the contents of the existing collection. Then you'll add your new child objects to the collection. Once the transaction commits, it should wipe out the old records and add the new records. You'll need to have to appropriate cascade setting for the one-to-many relationship. For Hibernate that would be all-delete-orphan so that it actually deletes the rows and not just the foreign keys.
 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


what i have is:

Parent parent = findPartentById(id);

parent.getChildren().clear();
parent.addChildren(newChildren);
getCurrentSession().update(parent)

but it doesn't seem to be working, whats wrong with it?

also the hibernate xml file has a cascade of "all-delete-orphan" with the one-to-many relationship


Cheers in advance
 
I am a man of mystery. Mostly because of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic