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;
}
}
}