-->
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.  [ 4 posts ] 
Author Message
 Post subject: One-to-many Collection question
PostPosted: Fri Jan 09, 2004 4:27 pm 
Beginner
Beginner

Joined: Thu Nov 06, 2003 10:04 pm
Posts: 22
I have a question about one-to-many collections. I have the following two classes.
Code:
<class name="Parent" table="parents">

  <key>
      <column name="parent_id", sql-type="char(36)"/>
  </key>

  <list name="children" inverse="true" cascade="all-delete-orphan">
       <key>
           <column name="parent_id" sql-type="char(36)"/>
       </key>
       <index column="index"/>
       <one-to-many class="Child"/>
  </list>

</class>

<class name="Child" table="children">

   <key>
       <column name="child_id" sql-type="char(36)"/>
   </key>

   <many-to-one name="parent" class="Parent">
       <column name="parent_id" sql-type="char(36)" not-null="true"/>
   </many-to-one>

</class>


Now for a Parent instance that had been saved in a previous session, I make some changes in a UI tier, like adding new Children, modifying some existing Children and removing some other exisiting Children from the Parent's children collection.

Now in order to saveOrUpdate() this Parent instance in a new Session, do I simply do session.saveOrUpdate( parent )? If so, will Hibernate know which Children are new, which ones modified and which ones to delete from the database?

Or would I have to manually iterate over the children collection in the dao tier to see which ones to saveOrUpdate and which ones to delete?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 10, 2004 8:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please read http://www.hibernate.org/hib_docs/reference/html/parent-child.html#parent-child-cascades. It is explained there pretty well


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 10:36 pm 
Beginner
Beginner

Joined: Thu Nov 06, 2003 10:04 pm
Posts: 22
The explanation is pretty good for one-to-many "bidirectional" relationship. But what I am actually trying to do is a one-to-many "uni-directional" relationship with a "list" on many end. It is like this.

Code:
<class name="Parent" table="parents">

  <key>
      <column name="parent_id", sql-type="char(36)"/>
  </key>

  <list name="children" cascade="all-delete-orphan">
       <key>
           <column name="parent_id" sql-type="char(36)"/>
       </key>
       <index column="index"/>
       <one-to-many class="Child"/>
  </list>

</class>

<class name="Child" table="children">

   <key>
       <column name="child_id" sql-type="char(36)"/>
   </key>

</class>


And in this, when I save a Parent instance the first time (using just session.saveOrUpdate( parent ); session.flush(); ), everything is fine.

But if I try to save the same Parent instance, with some modifications to the list of children using the same two above statements, then it gives me an exception saying "you may not dereference a collection with cascade="all-delete-orphan" option. If I change that option to just cascade="all", then it does save my Parent instance, but leaves behind any orphaned children in the childrens table.

If anyone can throw some light on the correct way of mapping this relation and also the steps (or method calls) to save or update such instances, I would appreciate. Just to note, all changes to my Parent instance (including adding/modifying/deleting children from the collection) are being done in a UI tier where I dont have the session instance.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 5:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
With all-delete-orphan you can not do something like this: object.setList(someOtherList).

With delete, you have to delete the orphaned childs manually.

In every case, saveOrUpdate(parent) is all you need to do.


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