-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: cascades updates and saves
PostPosted: Wed Mar 22, 2006 6:52 pm 
Beginner
Beginner

Joined: Thu Mar 09, 2006 1:45 pm
Posts: 26
I have am curious as to whether hibernate can accomplish my goal with cascading="save-update"

I have a Parent and child relationship, 1 to many.

Parent class
pId
Set child

Child class
cId
Parent p



I am receiving information from a source and updating my database with it. If I get a new Parent, I do a save and it will save its information as well as the children's information.

From my source, I can receive new Parents ( which works fine ) or updates to the Parent and/or the children receiving deletions,additions and updates.

If I get a deletion, addition and update to children associated to a Parent,
will hibernate do all of this to the child table by calling

session.update(Parent).

The result I want is the new child added, old child deleted and existing child to have new information. I was told it could not do this so I wrote some other code and now i was told it could do this.

There is a foreign key from the child to the parent.

The code I have now gets all the children for the parent from the source and db, I compare them and add, delete or update as need be. I then try and do an update to the parent and I get an exception. I think hibernate is trying to insert the children in the db, but get a constraint violation.

Any help would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 5:01 am 
Newbie

Joined: Tue Nov 15, 2005 4:21 am
Posts: 11
Hibernate can delete orphaned children if you add "delete-orphan" to the cascade attribute.

Your exception might not be related to this, if you are using update() incorrectly. Calling session.update(parent) is not necessary if parent was loaded in this same session.

If the above doesn't solve your problem, post more details, including the stack trace,mapping documents and relevant code fragments.


Top
 Profile  
 
 Post subject: cascade in hbm.xml file
PostPosted: Thu Mar 23, 2006 11:52 am 
Beginner
Beginner

Joined: Thu Mar 09, 2006 1:45 pm
Posts: 26
I used cascade="save-update", can you set it to "delete-orphan" as well,

is there are cascade option that incorporates both of these into one?
cascade="save-update" and then cascade"delete-orphan"

cascade="all" did not seem to work


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 12:41 pm 
Newbie

Joined: Tue Nov 15, 2005 4:21 am
Posts: 11
yes, cascade="save-update,delete-orphan" or cascade="all-delete-orphan", which I think could be more appropriate for your particular case.

edit: corrected typo


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 5:05 pm 
Beginner
Beginner

Joined: Thu Mar 09, 2006 1:45 pm
Posts: 26
I have some data in the db - a few Parents with each having some Children.

I then receive a List of Parents that need to be either updated in the db or some that are not in the db so they must be saved.

I have code doing a find and if it is not in the db I call sesssion.save(parent), if it is I call session.update( parent );

I commented out this code and tried to do a session.saveOrUpdate( parent ) thinking that it will update the db if it is already there and save it if it is not.

However, it tries to update all parents, even if it is not there. The weird thing is that the children are trying to be saved and I get an exception saying the parentId is not found ( this would be the foreign key )

Is there something I am missing with the saveOrUpdate method?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 6:16 am 
Newbie

Joined: Tue Nov 15, 2005 4:21 am
Posts: 11
If it's trying to update an instance that does not exist on the database it might be because it has an identifier with the wrong value. What identifier generation scheme are you using? Have you set unsaved-value to an appropriate value? What exception(s) do you get? Post relevant code, mapping documents and log.

BTW once you load an object in a session, do not call update(), the session will take care of updating the database if it sees that the object has been modified since it was loaded.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.