Hi all. I'm running into an issue migrating to hibernate that I've not been able to solve.
I'm modeling servers connected to a cluster. A server has a property that indicates what cluster the server was last connected to. The cluster does not have a reference back to what comm servers was last connected to the cluster.
I've recently migrated from JDO. In JDO I provided a hint in the mapping, and had the foreign key constraint was "on delete set null". If I do the same I get "org.hibernate.ObjectNotFoundException: No row with the given identifier exists" when I load a server object after I delete a cluster entity. I figured this is because hibernate is not aware of the field changing to null in the DB, and it's not flushing the server cache.
If I'm not using the "on delete set null" feture of the foreign key constraint, hibernate is violating the constraint.
This is a simplified version of the mapping I'm using
Code:
<class name="Server" table="servers">
<cache usage="read-write"/>
<id name="key" column="commserverKey" type="long">
<generator class="sequence">
<param name="sequence">sq_servers</param>
</generator>
</id>
<version name="objectVersion" access="field" type="long"/>
<property name="hostId" not-null="true"/>
<many-to-one name="cluster" column="clusterkey" class="Cluster"/>
<many-to-one name="lastCluster" column="lastCluster" class="Cluster"/>
</class>
<class name="Cluster" table="clusters">
<cache usage="read-write"/>
<id name="key" column="clusterkey" type="long">
<generator class="sequence">
<param name="sequence">sq_clusters</param>
</generator>
</id>
<version name="objectVersion" access="field" type="long"/>
<property name="binaryVersion" not-null="true" length="255"/>
<set name="servers">
<key column="clusterkey"/>
<one-to-many class="Server"/>
</set>
</class>
How can I do the mapping in a way that the lastCluster property of the servers will be set to null when a cluster is deleted?
I guess one possible solution is to set the lastCluster property to not-found="ignore", and have the DB set the column to null on delete. Is that a feasible solution?