Program: 63287
Project: 63287
Buggy source:
/** 
 * INTERNAL: Create an instance of the Id class or value from the object.
 */
public Object createPrimaryKeyInstance(Object object,AbstractSession session){
  KeyElementAccessor[] pkElementArray=this.getKeyClassFields();
  ObjectBuilder builder=getDescriptor().getObjectBuilder();
  if (pkElementArray.length == 1 && pkElementArray[0] instanceof KeyIsElementAccessor) {
    DatabaseMapping mapping=builder.getMappingForAttributeName(pkElementArray[0].getAttributeName());
    Object fieldValue=mapping.getRealAttributeValueFromObject(object,session);
    if (mapping.isObjectReferenceMapping()) {
      fieldValue=mapping.getReferenceDescriptor().getCMPPolicy().createPrimaryKeyInstance(fieldValue,session);
    }
    return fieldValue;
  }
  Object keyInstance=getPKClassInstance();
  Set<ObjectReferenceMapping> usedObjectReferenceMappings=new HashSet<ObjectReferenceMapping>();
  for (int index=0; index < pkElementArray.length; index++) {
    Object keyObj=object;
    KeyElementAccessor accessor=pkElementArray[index];
    DatabaseField field=accessor.getDatabaseField();
    DatabaseMapping mapping=builder.getMappingForField(field);
    if (!mapping.isObjectReferenceMapping() || !usedObjectReferenceMappings.contains(mapping)) {
      while (mapping.isAggregateObjectMapping()) {
        keyObj=mapping.getRealAttributeValueFromObject(keyObj,session);
        mapping=mapping.getReferenceDescriptor().getObjectBuilder().getMappingForField(field);
      }
      Object fieldValue=mapping.getRealAttributeValueFromObject(keyObj,session);
      if (mapping.isObjectReferenceMapping()) {
        fieldValue=mapping.getReferenceDescriptor().getCMPPolicy().createPrimaryKeyInstance(fieldValue,session);
        usedObjectReferenceMappings.add((ObjectReferenceMapping)mapping);
      }
      accessor.setValue(keyInstance,fieldValue);
    }
  }
  return keyInstance;
}

Fixed version (ground truth):
/** 
 * INTERNAL: Create an instance of the Id class or value from the object.
 */
public Object createPrimaryKeyInstance(Object object,AbstractSession session){
  KeyElementAccessor[] pkElementArray=this.getKeyClassFields();
  ObjectBuilder builder=getDescriptor().getObjectBuilder();
  if (pkElementArray.length == 1 && pkElementArray[0] instanceof KeyIsElementAccessor) {
    DatabaseMapping mapping=builder.getMappingForAttributeName(pkElementArray[0].getAttributeName());
    Object fieldValue=mapping.getRealAttributeValueFromObject(object,session);
    if (mapping.isObjectReferenceMapping()) {
      fieldValue=mapping.getReferenceDescriptor().getCMPPolicy().createPrimaryKeyInstance(fieldValue,session);
    }
    return fieldValue;
  }
  Object keyInstance=getPKClassInstance();
  Set<ObjectReferenceMapping> usedObjectReferenceMappings=new HashSet<ObjectReferenceMapping>();
  for (int index=0; index < pkElementArray.length; index++) {
    Object keyObj=object;
    KeyElementAccessor accessor=pkElementArray[index];
    DatabaseField field=accessor.getDatabaseField();
    DatabaseMapping mapping=builder.getMappingForField(field);
    Object nestedKeyInstance=keyInstance;
    if (!mapping.isObjectReferenceMapping() || !usedObjectReferenceMappings.contains(mapping)) {
      while (mapping.isAggregateObjectMapping()) {
        keyObj=mapping.getRealAttributeValueFromObject(keyObj,session);
        mapping=mapping.getReferenceDescriptor().getObjectBuilder().getMappingForField(field);
        if (mapping.isAggregateMapping()) {
          Object nestedObject=mapping.getRealAttributeValueFromObject(nestedKeyInstance,session);
          if (nestedObject == null) {
            nestedObject=getClassInstance(mapping.getReferenceDescriptor().getJavaClass());
          }
          mapping.setRealAttributeValueInObject(nestedKeyInstance,nestedObject);
          nestedKeyInstance=nestedObject;
        }
      }
      Object fieldValue=mapping.getRealAttributeValueFromObject(keyObj,session);
      if (mapping.isObjectReferenceMapping()) {
        fieldValue=mapping.getReferenceDescriptor().getCMPPolicy().createPrimaryKeyInstance(fieldValue,session);
        usedObjectReferenceMappings.add((ObjectReferenceMapping)mapping);
      }
      accessor.setValue(nestedKeyInstance,fieldValue);
    }
  }
  return keyInstance;
}