-->
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: Problem trying to move the children of a Parent to another
PostPosted: Wed Jan 27, 2010 7:06 pm 
Newbie

Joined: Wed Jan 06, 2010 9:09 pm
Posts: 14
Hi All,

I have a one-to-many parent->child relationship, an ARSEServerNode has a set of TravelTimeLinks. I need to move the children of one parent to be children of another parent, then delete the first parent. I can't figure out the config. and code to get this working. I can delete the parent I need to delete OK, but the FK parent id's on the child are not being re-assigned to the new parent. I've included my mapping files below and some code I tried to get working. I would be super-grateful for any advice - Thanks.

ArseServerNode.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 18, 2010 3:23:48 PM by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
    <class name="xx.ARSEServerNode" table="ARSEServerNode" catalog="snaps">
        <comment>
        </comment>

        <id name="id" type="long">
            <column name="id" />

            <generator class="assigned" />
        </id>

        <property name="serverIpAddress" type="string">
            <column name="serverIpAddress" length="64" not-null="true" unique="true">
                <comment>
                </comment>
            </column>
        </property>

        <property name="localIpAddress" type="string">
            <column name="localIpAddress" length="64">
                <comment>
                </comment>
            </column>
        </property>

        <property name="capacity" type="int">
            <column name="capacity" not-null="true">
                <comment>
                </comment>
            </column>
        </property>

        <property name="isDead" type="byte">
            <column name="isDead" not-null="true">
                <comment>
                </comment>
            </column>
        </property>

        <property name="modifyTime" type="timestamp">
            <column name="modifyTime" length="19" not-null="true">
                <comment>
                </comment>
            </column>
        </property>

        <property name="arsewatchdog" type="timestamp">
            <column name="ARSEWatchdog" length="19" not-null="true">
                <comment>
                </comment>
            </column>
        </property>
       
        <set name="ttLinks" table="TravelTimeLink" lazy="false" inverse="true" cascade="all">
            <key column="ARSEServerNodeId"/>
            <one-to-many class="xx.TravelTimeLink"/>
        </set>
       
    </class>
</hibernate-mapping>


TravelTimeLink.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 18, 2010 3:23:48 PM by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
    <class name="xx.TravelTimeLink" table="TravelTimeLink" catalog="snaps">
        <comment>
        </comment>

        <id name="id" type="long">
            <column name="id" />

            <generator class="assigned" />
        </id>
       
        <property name="listenPort" type="int">
            <column name="listenPort" not-null="true">
                <comment>
                </comment>
            </column>
        </property>

        <property name="linkCapacity" type="int">
            <column name="linkCapacity" not-null="true">
                <comment>
                </comment>
            </column>
        </property>

        <property name="maxTravelTime" type="int">
            <column name="maxTravelTime" not-null="true">
                <comment>
                </comment>
            </column>
        </property>

        <property name="maxLatency" type="int">
            <column name="maxLatency" not-null="true">
                <comment>
                </comment>
            </column>
        </property>

        <property name="linkName" type="string">
            <column name="linkName" length="64" not-null="true">
                <comment>
                </comment>
            </column>
        </property>

        <property name="arseserverNodeId" type="long">
            <column name="ARSEServerNodeId" not-null="true">
                <comment>
                </comment>
            </column>
        </property>

        <property name="travelTimeSegmentId" type="long">
            <column name="travelTimeSegmentId" not-null="true">
                <comment>
                </comment>
            </column>
        </property>

        <property name="isDead" type="byte">
            <column name="isDead" not-null="true">
                <comment>
                </comment>
            </column>
        </property>

        <property name="modifyTime" type="timestamp">
            <column name="modifyTime" length="19" not-null="true">
                <comment>
                </comment>
            </column>
        </property>
       
        <!-- Added by Sean -->
        <set name="locations" table="TravelTimeLinkLocationRelationship" lazy="false">
            <key column="travelTimeLinkId"/>
            <many-to-many column="locationId" class="com.sensysnetworks.traveltime.orm.Location"/>
        </set>
               
    </class>
</hibernate-mapping>


The code I'm trying to get working:
Code:
                        // search through all the engines for one with enough capacity to move over the linksToReallocate
                        // remove the linksToReallocate from the engineToDelete and store them under the new engine
                  for (ARSEServerNode engine : reIDEngines) {
                     // skip the engine we're about to delete in the engine search
                      if (engine.getId() != id) {
                         final int linkCapacity = engine.getCapacity() - engine.getTtLinks().size();
                         if (linkCapacity >= numLinksToReallocate) {
                            engine.getTtLinks().addAll(linksToReallocate);
                            engineToDelete.getTtLinks().removeAll(linksToReallocate);
                            
                            // This code doesn't update the the parentids of the linksToReallocate to point to the new parent (ARSEServerNode)
                            HibernateUtil.getSessionFactory().getCurrentSession().save(engine);
                            HibernateUtil.getSessionFactory().getCurrentSession().save(engineToDelete);
                            for (TravelTimeLink link : linksToReallocate) {
                               HibernateUtil.getSessionFactory().getCurrentSession().save(link);
                        }
                            break;
                         }
                      }
      }


Top
 Profile  
 
 Post subject: Re: Problem trying to move the children of a Parent to another
PostPosted: Thu Jan 28, 2010 3:25 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi,

you are using a bidirectional association with inverse owner.
Hibernate documentation (Book "Java persistence with Hibernate" chapter 6) writes in this context:
Code:
if you only call anItem.getBids().add(bid), no changes are made persistent!
You get what you want only if the other side aBid.setItem(anItem) is set correctly


In you code you just handle one part
Code:
engine.getTtLinks().addAll(linksToReallocate);
engineToDelete.getTtLinks().removeAll(linksToReallocate);


so that is the problem.


Top
 Profile  
 
 Post subject: Re: Problem trying to move the children of a Parent to another
PostPosted: Thu Jan 28, 2010 4:22 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
There are multiple issues in the mapping and code.

The first issue is the inverse="true" setting in the <set name="ttLinks" ..> mapping, but there is no mapping in the other direction. Well... you have mapped the ARSEServerNodeId column as a <property ...>, but it should be mapped as a <many-to-one ...>.

The second issue is that your code is only modifying the set returned by getTtLinks(). This will not result in anything since with the inverse="true" setting you have told Hibernate to ignore changes to this set. The correct way to move a TravelTimeLink to another ARSEServerNode is to update the many-to-one on the TraveTimelLink object. For example, something like this:

Code:
for (TravelTimeLink ttl : linksToReallocate)
{
   ttl.setArseServerNode(engine);
}


The following chapter in the Hibernate documentation contains a lot of useful information and examples:
http://docs.jboss.org/hibernate/stable/ ... child.html


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.