return;
}
// Check if the field has a FK defined
ClassLoaderResolver clr = om.getClassLoaderResolver();
MappedStoreManager storeMgr = (MappedStoreManager)om.getStoreManager();
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
// TODO Cater for more than 1 related field
boolean dependent = mmd.isDependent();
boolean hasFK = false;
if (!dependent)
{
// Not dependent, so check if the datastore has a FK and will take care of it for us
if (mmd.getForeignKeyMetaData() != null)
{
hasFK = true;
}
if (relatedMmds != null && relatedMmds[0].getForeignKeyMetaData() != null)
{
hasFK = true;
}
if (om.getOMFContext().getPersistenceConfiguration().getStringProperty("datanucleus.deletionPolicy").equals("JDO2"))
{
// JDO2 doesnt currently (2.0 spec) take note of foreign-key
hasFK = false;
}
}
// Basic rules for the following :-
// 1. If it is dependent then we delete it (maybe after nulling).
// 2. If it is not dependent and they have defined no FK then null it, else delete it
// 3. If it is not dependent and they have a FK, let the datastore handle the delete
// There may be some corner cases that this code doesnt yet cater for
int relationType = mmd.getRelationType(clr);
if (pc != null)
{
if (relationType == Relation.ONE_TO_ONE_UNI ||
(relationType == Relation.ONE_TO_ONE_BI && mmd.getMappedBy() == null))
{
// 1-1 with FK at this side (owner of the relation)
if (dependent)
{
boolean relatedObjectDeleted = om.getApiAdapter().isDeleted(pc);
if (isNullable() && !relatedObjectDeleted)
{
// Other object not yet deleted - just null out the FK
// TODO Not doing this would cause errors in 1-1 uni relations (e.g AttachDetachTest)
// TODO Log this since it affects the resultant objects
sm.replaceField(fieldNumber, null, true);
sm.getStoreManager().getPersistenceHandler().updateObject(sm, new int[]{fieldNumber});
}
if (!relatedObjectDeleted)
{
// Mark the other object for deletion since not yet tagged
om.deleteObjectInternal(pc);
}
}
else
{
// 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)
{