if (oldValue != null)
{
// Previously had "a.b = b1"; "a.b" has been changed
// Need to remove from the other side if still set
AbstractMemberMetaData relatedMmd = mmd.getRelatedMemberMetaDataForObject(clr, pc, oldValue);
ObjectProvider oldSM = om.findObjectProvider(oldValue);
if (oldSM != null)
{
boolean oldIsDeleted = om.getApiAdapter().isDeleted(oldSM.getObject());
if (!oldIsDeleted)
{
// Old still exists, so make sure its relation is correct
if (!oldSM.isFieldLoaded(relatedMmd.getAbsoluteFieldNumber()))
{
// Load the field in case we need to set the old value
oldSM.loadField(relatedMmd.getAbsoluteFieldNumber());
}
Object oldValueFieldValue = oldSM.provideField(relatedMmd.getAbsoluteFieldNumber());
if (oldValueFieldValue == null)
{
// Set to null so nothing to do
}
else if (oldValueFieldValue == pc)
{
// Still set to this object, so null out the other objects relation
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("013004",
StringUtils.toJVMIDString(oldValue), relatedMmd.getFullFieldName(),
StringUtils.toJVMIDString(pc), StringUtils.toJVMIDString(newValue)));
}
oldSM.replaceFieldValue(relatedMmd.getAbsoluteFieldNumber(), null);
}
}
else
{
ownerSM.replaceFieldValue(mmd.getAbsoluteFieldNumber(), null);
}
}
}
if (newValue != null)
{
// Previously had "a.b = b1"; Now have "a.b = b2"
// Need to set the other side if not yet set, and unset any related old value on the other side
AbstractMemberMetaData relatedMmd = mmd.getRelatedMemberMetaDataForObject(clr, pc, newValue);
ObjectProvider newSM = om.findObjectProvider(newValue);
if (newSM != null && relatedMmd != null)
{
if (!newSM.isFieldLoaded(relatedMmd.getAbsoluteFieldNumber()))
{
// Load the field in case we need to set the link from the old value
newSM.loadField(relatedMmd.getAbsoluteFieldNumber());
}
Object newValueFieldValue = newSM.provideField(relatedMmd.getAbsoluteFieldNumber());
if (newValueFieldValue == null)
{
// Was set to null so set to our object
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("013005",
StringUtils.toJVMIDString(newValue), relatedMmd.getFullFieldName(),
StringUtils.toJVMIDString(pc)));
}
newSM.replaceFieldValue(relatedMmd.getAbsoluteFieldNumber(), pc);
}
else if (newValueFieldValue != pc)
{
// Was set to different object, so null out the other objects relation
ObjectProvider newValueFieldSM = om.findObjectProvider(newValueFieldValue);
if (newValueFieldSM != null)
{
// Null out the field of the related object of the new value
if (!newValueFieldSM.isFieldLoaded(mmd.getAbsoluteFieldNumber()))
{
// Load the field in case we need to set the link from the old value
newValueFieldSM.loadField(mmd.getAbsoluteFieldNumber());
}
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("013004",
StringUtils.toJVMIDString(newValueFieldValue),
mmd.getFullFieldName(),
StringUtils.toJVMIDString(newValue), StringUtils.toJVMIDString(pc)));
}
newValueFieldSM.replaceFieldValue(mmd.getAbsoluteFieldNumber(), null);
}
// Update the field of the new value to our object
if (NucleusLogger.PERSISTENCE.isDebugEnabled())
{
NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("013005",