-->
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: Cascading stops after 2nd child level
PostPosted: Mon Sep 22, 2003 12:01 pm 
Newbie

Joined: Tue Sep 16, 2003 6:48 pm
Posts: 2
I have many related objects, mostly one-to-many relationships, that are not cascading past a certain level. In the code, I iterate through each child object and create a new Set and populate the Set member with the appropriate child objects, then call update on the root object. I can see that all the child objects are there in the debugger before I call update, but are not persisted to the DB (which is DB2) after I call update. The relationship goes like this:

Root object -> many-to-many -> Child object 1 -> one-to-many -> Child object 2 -> one-to-many -> ...

Here are my mapping files:
Code:
<!-- ROOT LEVEL 0 -->
<hibernate-mapping>
    <class name="FooTop" table="foo_top">
        <jcs-cache usage="read-write"/>
       <id name="fooTopId" column="foo_top_id" type="integer">
           <generator class="native"/>
        </id>
        <set name="fooSets" table="foo_top_foo_set" cascade="all">
           <jcs-cache usage="read-write"/>
           <key column="foo_top_id"/>
           <many-to-many column="foo_set_id" class="FooSet"/>
        </set>
        <joined-subclass name="joinedClass" table="joined_class">
           <key column="joined_class_id"/>
         <property name="startDate"  column="start_dt" type="date"/>
         <property name="endDate"  column="end_dt" type="date"/>
        </joined-subclass>       
    </class>
</hibernate-mapping>

<!-- CHILD LEVEL 1 -->
<hibernate-mapping>
    <class name="FooSet" table="foo_set">
        <jcs-cache usage="read-write"/>
       <id name="fooSetId" column="foo_set_id" type="integer">
           <generator class="native"/>
        </id>
        <set name="foos" cascade="all">
           <jcs-cache usage="read-write"/>
           <key column="foo_set_id"/>
           <one-to-many class="Foo"/>
        </set>

    </class>

</hibernate-mapping>

<!-- CHILD LEVEL 2 -->
<hibernate-mapping>
    <class name="Foo" table="foo">
       <jcs-cache usage="read-write"/>
        <id name="fooId" column="foo_id" type="integer">
           <generator class="native"/>
        </id>
    </class>
</hibernate-mapping>


The Foo object is not being persisted to the database (as well as all subsequent child sets, which are not shown here). Both FooTop and FooSet appear in the DB after I call session.update(fooTop). Does this have anything to do with the order in which it persists the objects (breadth-first vrs. depth-first, or anything like that)?

Thanks,
BP


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 11:36 am 
Regular
Regular

Joined: Tue Sep 09, 2003 9:37 pm
Posts: 56
Location: Ogden, Utah, USA
I could be wrong, but I don't think hibernate cascades past the links on a many to many relationship.

Table1 1->* LinkTable *<-1 Table2

So the many-to-many relationship depicted above, the cascade will save records in the link table but not in table2, or beyond.

This is the behaviour for all of the many-to-many relationships that I have set up, and in my case that's the desired behaviour since Table2 is a separate 'top level' entity.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 2:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
I could be wrong, but I don't think hibernate cascades past the links on a many to many relationship.


I find this incredibly difficult to believe....


Top
 Profile  
 
 Post subject: Solution
PostPosted: Wed Sep 24, 2003 12:32 pm 
Newbie

Joined: Tue Sep 16, 2003 6:48 pm
Posts: 2
Thanks for the reply. I found the solution and it was a dumb mistake in my code. I was clearing the foos Set before setting it. At some point, hibernate re-saves the Set with the same Set object before cascading. My code did the following:

public class FooSet {

private Set foos;

public void setFoos(Set foos) {
if (this.foos != null) {
this.foos.clear();
this.foos.addAll(foos);
} else {
this.foos = foos;
}
}

}


Since hibernate was re-setting with the same object, I was clearing the very same object I was going to set it with. If any one else happens to be doing the same thing (I don't recall why I was in the first place), they should avoid this.

BP


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.