-->
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.  [ 3 posts ] 
Author Message
 Post subject: after cascade no need session.save()
PostPosted: Sun Jan 15, 2006 3:23 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 11:19 am
Posts: 24
I still do not understand the why after cascade=all, we do not have to call the save()? Can anyone explain to me?

The explicit call to save() is still annoying. We will address this by using cascades.

<set name="children" inverse="true" cascade="all">
<key column="parent_id"/>
<one-to-many class="Child"/>
</set>
This simplifies the code above to

Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
p.addChild(c);
session.flush();


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 15, 2006 8:04 am 
Newbie

Joined: Thu Jan 05, 2006 7:33 pm
Posts: 13
hi jimmy

Not sure what your question is but can say that when cascade is set, Hibernate will treat the linked entities as being value types. Basically, cascaded operations will pass on.

Firstly, all operations on instances that are associated with (in) a Session are always persisted. So in your code, the creation of the relationship between the parent and child will be persisted immediately (not actually written immediately to the DB though). Of course, the child will not have been persisted when the call is made. Because cascade is set, Hibernate calls Session.saveOrUpdate(child) before it calls Session.saveOrUpdate(parent). If cascade wasnt set, you may get an error when the action is performed - because in a relation using a join table for example, HIbernate would try to add an entry to the collection table but it would refer to a child that didnt exist (in the DB).

http://www.hibernate.org/hib_docs/v3/re ... d-cascades

Happy hibernating

Michael


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 11:08 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 11:19 am
Posts: 24
Code:
Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
p.addChild(c);
session.save(c);
session.flush();
21.3. Cascading lifecycle
The explicit call to save() is still annoying. We will address this by using cascades.

<set name="children" inverse="true" cascade="all">
    <key column="parent_id"/>
    <one-to-many class="Child"/>
</set>
This simplifies the code above to

Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
p.addChild(c);
session.flush();


You have a look at the 2 flush there. How come the bottom one does not need the session.save(c)?


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