System.err.println("Cannot swizzle objects of different classes: "
+ newObj.getClass() + " and " + oldObj.getClass());
return newObj;
}
ClassDescriptor mif = pb.getClassDescriptor(newObj.getClass());
FieldDescriptor[] fieldDescs = mif.getFieldDescriptions();
for (int i = 0; i < fieldDescs.length; i++)
{
FieldDescriptor fd = fieldDescs[i];
PersistentField f = fd.getPersistentField();
f.set(oldObj, f.get(newObj));
}
// N:1 relations
Iterator iter = mif.getObjectReferenceDescriptors().iterator();
ObjectReferenceDescriptor rds;
PersistentField field;
Object newRelObj;
Identity newRelOid;
Object oldRelObj;
while (iter.hasNext())
{
rds = (ObjectReferenceDescriptor) iter.next();
field = rds.getPersistentField();
newRelObj = field.get(newObj);
oldRelObj = field.get(oldObj);
if ((newRelObj == null) && (oldRelObj != null))
{
field.set(oldObj, null);
}
else if (newRelObj != null)
{
newRelOid = new Identity(newRelObj, pb);
if ((oldRelObj == null) ||
!newRelOid.equals(new Identity(oldRelObj, pb)))
{
// seek for existing old object with the new identity
oldRelObj = cache.lookup(newRelOid);
if (oldRelObj == null)
{
throw new IllegalStateException("Related object not found in the context: " + newRelOid);
}
field.set(oldObj, oldRelObj);
}
}
}
// 1:N relations
Iterator collections = mif.getCollectionDescriptors().iterator();
CollectionDescriptor collectionDescriptor;
while (collections.hasNext())
{
collectionDescriptor = (CollectionDescriptor) collections.next();