{
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);
org.jpox.StateManager oldSM = om.findStateManager(oldValue);
if (oldSM != null)
{
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 (JPOXLogger.PERSISTENCE.isDebugEnabled())
{
JPOXLogger.PERSISTENCE.debug(
LOCALISER.msg("013004",
StringUtils.toJVMIDString(oldValue), relatedMmd.getFullFieldName(),
StringUtils.toJVMIDString(pc), StringUtils.toJVMIDString(newValue)));
}
oldSM.replaceFieldValue(relatedMmd.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);
org.jpox.StateManager newSM = om.findStateManager(newValue);
if (newSM != null && relatedMmd != null)
{
newSM.loadField(relatedMmd.getAbsoluteFieldNumber());
Object newValueFieldValue = newSM.provideField(relatedMmd.getAbsoluteFieldNumber());
if (newValueFieldValue == null)
{
// Was set to null so set to our object
if (JPOXLogger.PERSISTENCE.isDebugEnabled())
{
JPOXLogger.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
org.jpox.StateManager newValueFieldSM = om.findStateManager(newValueFieldValue);
if (newValueFieldSM != null)
{
// Null out the field of the related object of the new value
newValueFieldSM.loadField(mmd.getAbsoluteFieldNumber());
if (JPOXLogger.PERSISTENCE.isDebugEnabled())
{
JPOXLogger.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 (JPOXLogger.PERSISTENCE.isDebugEnabled())
{
JPOXLogger.PERSISTENCE.debug(
LOCALISER.msg("013005",
StringUtils.toJVMIDString(newValue), relatedMmd.getFullFieldName(),
StringUtils.toJVMIDString(pc)));
}
newSM.replaceFieldValue(relatedMmd.getAbsoluteFieldNumber(), pc);
}
}
}
}