Object entity = null;
if (descriptor.hasCMPPolicy()) {
CMP3Policy policy = (CMP3Policy) descriptor.getCMPPolicy();
entity = policy.createBeanUsingKey(id, (AbstractSession) context.getServerSession());
} else if (entity instanceof DynamicEntity) {
DynamicEntityImpl dynamicEntity = (DynamicEntityImpl) context.newEntity(entityType);
// if there is only one PK mapping, we assume the id object
// represents the value of that mapping
if (pkMappings.size() == 1) {
dynamicEntity.set(pkMappings.get(0).getAttributeName(), id);
} else {
// If there are more that one PK, we assume an array as produced
// by buildId() above with the keys
// based on a sorted order of PK fields
List<SortableKey> pkIndices = new ArrayList<SortableKey>();
int index = 0;
for (DatabaseMapping mapping : pkMappings) {
pkIndices.add(new SortableKey(mapping, index));
index++;
}
Collections.sort(pkIndices);
Object[] keyElements = (Object[]) id;
for (SortableKey key : pkIndices) {
dynamicEntity.set(key.getMapping().getAttributeName(), keyElements[key.getIndex()]);
}
}
entity = dynamicEntity;
} else {
throw new RuntimeException("Could not create shell for entity.");