// data below
orig.load(_fc, StateManagerImpl.LOAD_ALL, null, null, false);
}
// create new state manager with embedded metadata
ClassMetaData meta = ownerMeta.getEmbeddedMetaData();
if (meta == null)
throw new InternalException(_loc.get("bad-embed", ownerMeta));
if (id == null)
id = StateManagerId.newInstance(this);
StateManagerImpl sm = new StateManagerImpl(id, meta, this);
sm.setOwner((StateManagerImpl) owner, ownerMeta);
PersistenceCapable copy;
PCState state;
Class type = meta.getDescribedType();
if (type.isInterface())
type = meta.getInterfaceImpl();
if (obj != null) {
// give copy and the original instance the same state manager
// so that we can copy fields from one to the other
StateManagerImpl copySM;
PersistenceCapable pc;
if (orig == null) {
copySM = sm;
pc = assertPersistenceCapable(obj);
pc.pcReplaceStateManager(sm);
} else {
copySM = orig;
pc = orig.getPersistenceCapable();
}
try {
// copy the instance. we do this even if it doesn't already
// have a state manager in case it is later assigned to a
// PC field; at that point it's too late to copy
copy = PCRegistry.newInstance(type, copySM, false);
int[] fields = new int[meta.getFields().length];
for (int i = 0; i < fields.length; i++)
fields[i] = i;
copy.pcCopyFields(pc, fields);
state = PCState.ECOPY;
copy.pcReplaceStateManager(null);