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