-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with cascade and update
PostPosted: Wed Nov 29, 2006 2:03 pm 
Newbie

Joined: Thu Feb 02, 2006 9:39 am
Posts: 19
Hibernate version: 3.1

I do have a father object containing a collection of children, with a cascade set to "all-delete-orphan".

When I do a saveOrUpdate() on a newly created father, (ie: not in session), the children are correctly cascaded. For instance I could retrieve their database id (generated with "increment") just afterwards.

But if I modify an existing father (in my case, clearing the collection, and adding some children), and do a saveOrUpdate on the father, the children id are not set. (cascade not made ?)

I do have to wait after the session.commit() to be able to get the children id. But in my design I need to get this id after the saveOrUpdate(), not after the commit().

Note that my father does not have nay other value changed. Only the elements in its collection.

Is it a known behavior? Do I have to browse myself the hierarchy, and manually save each of the children?

--
Hugo


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 1:55 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
generally speaking needing the ID in some cases is bad design in my opinion. Although in some cases it is needed.

However, you can use session.flush() instead of commit to get your ids populated.

J


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 8:40 am 
Newbie

Joined: Thu Nov 02, 2006 8:41 am
Posts: 13
Location: Argentina
Hi
probe with the inverse clause, add inverse in true in the bag declaration, and add in the child mapping the many-to-one clause
and intent again


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 8:43 am 
Newbie

Joined: Thu Nov 02, 2006 8:41 am
Posts: 13
Location: Argentina
I forgot

If you have to persist an object and use it Id not use session.save(Object);
use
session.persist(Object);

is saves the object and the set it's identifier Id.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 11:25 am 
Newbie

Joined: Thu Feb 02, 2006 9:39 am
Posts: 19
Thanks for the replies. I replaced my "saveOrUpdate()" with a "persist()" and every things seems ok now. I could get the id just after the persist(). Good news for me ;)

Some comments anyway:
Quote:
Generally speaking needing the ID in some cases is bad design in my opinion. Although in some cases it is needed.

I agree with that, but I really need that children ID for speeding up some external index process, and I need it before the "commit()" to be able to rollback if something goes wrong.

Quote:
probe with the inverse clause, add inverse in true in the bag declaration, and add in the child mapping the many-to-one clause

I already use a inverse relation, so this was not the reason of the cascade failure. (I did not post my mapping files sorry for that, but I wasn't sure they'll be usefull, since this is a very general problem).


Final note, the hibernate documentation is not so clear about that problem. I was not able to find a clear distinction between "saveOrUpdate()" and "persist()".

And I found some inconsistency between the documentation and the actual code behavior. The docuementation says:
Quote:
"If a parent is passed to save(), update() or saveOrUpdate(), all children are passed to saveOrUpdate()"

but according to the definition of saveOrUpdate() :
Quote:
saveOrUpdate() does the following:
[...]
* if the object has no identifier property, save() it
[...]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.