-->
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.  [ 2 posts ] 
Author Message
 Post subject: cascading and many-to-many relationships
PostPosted: Sun Aug 31, 2003 4:15 am 
Newbie

Joined: Wed Aug 27, 2003 12:26 pm
Posts: 5
Hi there,

this is just a general begginer question. After reading chapter 8 of the hibernate reference about parent-child relationships I learnt about the use of the cascade attribute to automatically persist and link child objects in the database without having to explicity save them seperately from the parent object. (Hopefully i have grasped concept this correctly..)

The example in the reference was a one-to-many relationship. Here is a sinppet from the documentation:

Code:
<set name="children" inverse="true" cascade="all">
    <key column="parent_id"/>
    <one-to-many class="Child"/>
</set>


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


In the code above, the Child object is persisted in the database even though there was no explicit "session.save(c)"


Well, what I would like to know, is this possible in a many-to-many relationship? e.g. I would like to have a many-to-many relationship between two classes A and B, using a linking table. In my Java object model, I would like A objects to possess a set of B objects. When I add a brand new B object to an already persisted A object, can I get hibernate to automatically persist the B object and update the linking table? Or is B considered indepedant from A because it is a many-to-many relationship and hence has to be explicitly persisted?

Hope I made sense! Im not very good at database theory and Ive only been learning hibernate for 1 week =) Thanks for any help

Cheers

James


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 31, 2003 4:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes, of course. Usually, in the case of a many-to-many, you would use
Code:
cascade="save-update"


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