}
descriptor = descriptor.getSubclassDescriptor(source.getClass());
// presumably id's entity name should be of the right subclass.
final ClassDescriptor targetDescriptor = targetResolver.getClassDescriptor(id
.getEntityName());
final Persistent target = (Persistent) targetDescriptor.createObject();
target.setObjectId(id);
seen.put(id, target);
descriptor.visitProperties(new PropertyVisitor() {
public boolean visitToOne(ToOneProperty property) {
if (prefetchTree != null) {
PrefetchTreeNode child = prefetchTree.getNode(property.getName());
if (child != null) {
Object destinationSource = property.readProperty(source);
Object destinationTarget = destinationSource != null ? detach(
destinationSource,
property.getTargetDescriptor(),
child) : null;
ToOneProperty targetProperty = (ToOneProperty) targetDescriptor
.getProperty(property.getName());
Object oldTarget = targetProperty.isFault(target)
? null
: targetProperty.readProperty(target);
targetProperty
.writeProperty(target, oldTarget, destinationTarget);
}
}
return true;
}
public boolean visitToMany(ToManyProperty property) {
if (prefetchTree != null) {
PrefetchTreeNode child = prefetchTree.getNode(property.getName());
if (child != null) {
Collection collection = (Collection) property
.readProperty(source);
Collection targetCollection = new ArrayList(collection.size());
Iterator it = collection.iterator();
while (it.hasNext()) {
Object destinationSource = it.next();
Object destinationTarget = destinationSource != null
? detach(destinationSource, property
.getTargetDescriptor(), child)
: null;
targetCollection.add(destinationTarget);
}
ToManyProperty targetProperty = (ToManyProperty) targetDescriptor
.getProperty(property.getName());
targetProperty.writeProperty(target, null, targetCollection);
}
}
return true;
}
public boolean visitAttribute(AttributeProperty property) {
Property targetProperty = targetDescriptor
.getProperty(property.getName());
targetProperty.writeProperty(target, null, property.readProperty(source));
return true;
}
});