Program: 66572
Project: 66572
Buggy source:
/** 
 * INTERNAL: Create an instance of the composite primary key class for the key object.
 */
public Object createPrimaryKeyInstance(Object key,AbstractSession session){
  KeyElementAccessor[] pkElementArray=this.getKeyClassFields(getPKClass());
  ObjectBuilder builder=getDescriptor().getObjectBuilder();
  if (pkElementArray.length == 1 && pkElementArray[0] instanceof KeyIsElementAccessor) {
    DatabaseMapping mapping=builder.getMappingForAttributeName(pkElementArray[0].getAttributeName());
    Object fieldValue=mapping.getRealAttributeValueFromObject(key,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=key;
    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(getPKClass());
  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;
}