{
// We're deleting the FK at this side so shouldnt be an issue
AbstractMemberMetaData relatedMmd = mmd.getRelatedMemberMetaDataForObject(clr, sm.getObject(), pc);
if (relatedMmd != null)
{
StateManager otherSM = om.findStateManager(pc);
if (otherSM != null)
{
// Managed Relations : 1-1 bidir, so null out the object at the other
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("041019",
StringUtils.toJVMIDString(pc), relatedMmd.getFullFieldName(),
StringUtils.toJVMIDString(sm.getObject())));
}
otherSM.replaceField(relatedMmd.getAbsoluteFieldNumber(), null, true);
}
}
}
}
else if (relationType == Relation.ONE_TO_ONE_BI && mmd.getMappedBy() != null)
{
// 1-1 with FK at other side
DatastoreClass relatedTable = storeMgr.getDatastoreClass(relatedMmds[0].getClassName(), clr);
JavaTypeMapping relatedMapping = relatedTable.getMemberMapping(relatedMmds[0]);
boolean isNullable = relatedMapping.isNullable();
StateManager otherSM = om.findStateManager(pc);
if (dependent)
{
if (isNullable)
{
// Null out the FK in the datastore using a direct update (since we are deleting)
otherSM.replaceField(relatedMmds[0].getAbsoluteFieldNumber(), null, true);
otherSM.getStoreManager().getPersistenceHandler().updateObject(
otherSM, new int[]{relatedMmds[0].getAbsoluteFieldNumber()});
}
// Mark the other object for deletion
om.deleteObjectInternal(pc);
}
else if (!hasFK)
{
if (isNullable())
{
// Null out the FK in the datastore using a direct update (since we are deleting)
otherSM.replaceField(relatedMmds[0].getAbsoluteFieldNumber(), null, true);
otherSM.getStoreManager().getPersistenceHandler().updateObject(
otherSM, new int[]{relatedMmds[0].getAbsoluteFieldNumber()});
}
else
{
// TODO Remove it
}
}
else
{
// User has a FK defined (in MetaData) so let the datastore take care of it
}
}
else if (relationType == Relation.MANY_TO_ONE_BI)
{
StateManager otherSM = om.findStateManager(pc);
if (relatedMmds[0].getJoinMetaData() == null)
{
// N-1 with FK at this side
if (otherSM.isDeleting())
{
// Other object is being deleted too but this side has the FK so just delete this object
}
else
{
// Other object is not being deleted so delete it if necessary
if (dependent)
{
if (isNullable())
{
// TODO Datastore nullability info can be unreliable so try to avoid this call
// Null out the FK in the datastore using a direct update (since we are deleting)
sm.replaceField(fieldNumber, null, true);
sm.getStoreManager().getPersistenceHandler().updateObject(sm, new int[]{fieldNumber});
}
if (om.getApiAdapter().isDeleted(pc))
{
// Object is already tagged for deletion but we're deleting the FK so leave til flush()
}
else
{
// Mark the other object for deletion
om.deleteObjectInternal(pc);
}
}
else
{
// Managed Relations : remove element from collection/map
if (relatedMmds[0].hasCollection())
{
// Only update the other side if not already being deleted
if (!om.getApiAdapter().isDeleted(otherSM.getObject()) && !otherSM.isDeleting())
{
// Make sure the other object is updated in any caches
om.markDirty(otherSM, false);
Collection otherColl = (Collection)otherSM.provideField(relatedMmds[0].getAbsoluteFieldNumber());
if (otherColl != null)
{
// TODO Localise this message
NucleusLogger.JDO.debug("ManagedRelationships : delete of object causes removal from collection at " + relatedMmds[0].getFullFieldName());
otherColl.remove(sm.getObject());
}
}
}
else if (relatedMmds[0].hasMap())
{
// TODO Cater for maps, but what is the key/value pair ?
}
}
}
}
else
{
// N-1 with join table so no FK here so need to remove from Collection/Map first? (managed relations)
if (dependent)
{
// Mark the other object for deletion
om.deleteObjectInternal(pc);
}
else
{
// Managed Relations : remove element from collection/map
if (relatedMmds[0].hasCollection())
{
// Only update the other side if not already being deleted
if (!om.getApiAdapter().isDeleted(otherSM.getObject()) && !otherSM.isDeleting())
{
// Make sure the other object is updated in any caches
om.markDirty(otherSM, false);
// Make sure the other object has the collection loaded so does this change
otherSM.isLoaded((PersistenceCapable)otherSM.getObject(), relatedMmds[0].getAbsoluteFieldNumber());
Collection otherColl = (Collection)otherSM.provideField(relatedMmds[0].getAbsoluteFieldNumber());
if (otherColl != null)
{
// TODO Localise this
NucleusLogger.JDO.debug("ManagedRelationships : delete of object causes removal from collection at " + relatedMmds[0].getFullFieldName());
otherColl.remove(sm.getObject());