mmd != null ? mmd.getFullFieldName() : "", getType(), value.getClass().getName()));
}
if (value != null)
{
ApiAdapter api = ec.getApiAdapter();
ClassLoaderResolver clr = ec.getClassLoaderResolver();
// Make sure the value is persisted if it is persistable in its own right
if (!ec.isInserting(value))
{
// Object either already exists, or is not yet being inserted.
Object id = api.getIdForObject(value);
// Check if the PersistenceCapable exists in this datastore
boolean requiresPersisting = false;
if (ec.getApiAdapter().isDetached(value) && ownerSM != null)
{
// Detached object that needs attaching (or persisting if detached from a different datastore)
requiresPersisting = true;
}
else if (id == null)
{
// Transient object, so we need to persist it
requiresPersisting = true;
}
else
{
ExecutionContext valueEC = api.getExecutionContext(value);
if (valueEC != null && ec != valueEC)
{
throw new NucleusUserException(LOCALISER.msg("041015"), id);
}
}
if (requiresPersisting)
{
// The object is either not yet persistent or is detached and so needs attaching
Object pcNew = ec.persistObjectInternal(value, null, -1, ObjectProvider.PC);
ec.flushInternal(false);
id = api.getIdForObject(pcNew);
if (ec.getApiAdapter().isDetached(value) && ownerSM != null)
{
// Update any detached reference to refer to the attached variant
ownerSM.replaceFieldMakeDirty(ownerFieldNumber, pcNew);
RelationType relationType = mmd.getRelationType(clr);