• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate child parent update

 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • 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
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic