Program: 65795
Project: 65795
Buggy source:
/** 
 * Cache primary key and non primary key mappings.
 */
public void initializePrimaryKey(AbstractSession session) throws DescriptorException {
  List primaryKeyFields=this.descriptor.getPrimaryKeyFields();
  if (primaryKeyFields.isEmpty() && getDescriptor().isAggregateCollectionDescriptor()) {
    DatabaseTable defaultTable=getDescriptor().getDefaultTable();
    Iterator<DatabaseField> it=getDescriptor().getFields().iterator();
    while (it.hasNext()) {
      DatabaseField field=it.next();
      if (field.getTable().equals(defaultTable) && getMappingsByField().containsKey(field)) {
        primaryKeyFields.add(field);
      }
    }
    List<DatabaseField> additionalFields=this.descriptor.getAdditionalAggregateCollectionKeyFields();
    for (int i=0; i < additionalFields.size(); i++) {
      DatabaseField additionalField=additionalFields.get(i);
      if (!primaryKeyFields.contains(additionalField)) {
        primaryKeyFields.add(additionalField);
      }
    }
  }
  createPrimaryKeyExpression(session);
  getPrimaryKeyMappings().clear();
  getNonPrimaryKeyMappings().clear();
  for (Iterator fields=getMappingsByField().keySet().iterator(); fields.hasNext(); ) {
    DatabaseField field=(DatabaseField)fields.next();
    if (!primaryKeyFields.contains(field)) {
      DatabaseMapping mapping=getMappingForField(field);
      if (!getNonPrimaryKeyMappings().contains(mapping)) {
        getNonPrimaryKeyMappings().add(mapping);
      }
    }
  }
  for (int index=0; index < primaryKeyFields.size(); index++) {
    DatabaseField primaryKeyField=(DatabaseField)primaryKeyFields.get(index);
    DatabaseMapping mapping=getMappingForField(primaryKeyField);
    if (mapping == null) {
      if (this.descriptor.isDescriptorTypeAggregate()) {
        this.mayHaveNullInPrimaryKey=true;
      }
 else {
        throw DescriptorException.noMappingForPrimaryKey(primaryKeyField,this.descriptor);
      }
    }
    getPrimaryKeyMappings().add(mapping);
    if (mapping != null) {
      mapping.setIsPrimaryKeyMapping(true);
    }
    if (this.descriptor.hasMultipleTables() && (mapping != null)) {
      for (      Map keyMapping : this.descriptor.getAdditionalTablePrimaryKeyFields().values()) {
        DatabaseField secondaryField=(DatabaseField)keyMapping.get(primaryKeyField);
        if (secondaryField != null) {
          getMappingsByField().put(secondaryField,mapping);
          if (mapping.isAggregateObjectMapping()) {
            ((AggregateObjectMapping)mapping).addPrimaryKeyJoinField(primaryKeyField,secondaryField);
          }
        }
      }
    }
  }
  boolean hasSimplePrimaryKey=true;
  for (int index=0; index < getPrimaryKeyMappings().size(); index++) {
    DatabaseMapping mapping=getPrimaryKeyMappings().get(index);
    if ((mapping == null) || (!mapping.isDirectToFieldMapping())) {
      hasSimplePrimaryKey=false;
      break;
    }
  }
  this.descriptor.setHasSimplePrimaryKey(hasSimplePrimaryKey);
  boolean wasIdValidationSet=true;
  if (this.descriptor.getIdValidation() == null) {
    wasIdValidationSet=false;
    if (this.descriptor.getPrimaryKeyFields().size() > 1) {
      this.descriptor.setIdValidation(IdValidation.NULL);
    }
 else {
      this.descriptor.setIdValidation(IdValidation.ZERO);
    }
  }
  if (this.descriptor.getPrimaryKeyIdValidations() == null) {
    this.descriptor.setPrimaryKeyIdValidations(new ArrayList(this.descriptor.getPrimaryKeyFields().size()));
    for (    DatabaseField field : this.descriptor.getPrimaryKeyFields()) {
      if (!wasIdValidationSet && this.descriptor.usesSequenceNumbers() && field.equals(this.descriptor.getSequenceNumberField())) {
        this.descriptor.getPrimaryKeyIdValidations().add(IdValidation.ZERO);
      }
 else {
        this.descriptor.getPrimaryKeyIdValidations().add(this.descriptor.getIdValidation());
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Cache primary key and non primary key mappings.
 */
public void initializePrimaryKey(AbstractSession session) throws DescriptorException {
  List primaryKeyFields=this.descriptor.getPrimaryKeyFields();
  if ((null == primaryKeyFields || primaryKeyFields.isEmpty()) && getDescriptor().isAggregateCollectionDescriptor()) {
    DatabaseTable defaultTable=getDescriptor().getDefaultTable();
    Iterator<DatabaseField> it=getDescriptor().getFields().iterator();
    while (it.hasNext()) {
      DatabaseField field=it.next();
      if (field.getTable().equals(defaultTable) && getMappingsByField().containsKey(field)) {
        primaryKeyFields.add(field);
      }
    }
    List<DatabaseField> additionalFields=this.descriptor.getAdditionalAggregateCollectionKeyFields();
    for (int i=0; i < additionalFields.size(); i++) {
      DatabaseField additionalField=additionalFields.get(i);
      if (!primaryKeyFields.contains(additionalField)) {
        primaryKeyFields.add(additionalField);
      }
    }
  }
  createPrimaryKeyExpression(session);
  if (null != primaryKeyMappings) {
    primaryKeyMappings.clear();
  }
  if (null != nonPrimaryKeyMappings) {
    nonPrimaryKeyMappings.clear();
  }
  for (Iterator fields=getMappingsByField().keySet().iterator(); fields.hasNext(); ) {
    DatabaseField field=(DatabaseField)fields.next();
    if (null == primaryKeyFields || !primaryKeyFields.contains(field)) {
      DatabaseMapping mapping=getMappingForField(field);
      if (nonPrimaryKeyMappings != null && !getNonPrimaryKeyMappings().contains(mapping)) {
        getNonPrimaryKeyMappings().add(mapping);
      }
    }
  }
  if (null != primaryKeyFields) {
    for (int index=0; index < primaryKeyFields.size(); index++) {
      DatabaseField primaryKeyField=(DatabaseField)primaryKeyFields.get(index);
      DatabaseMapping mapping=getMappingForField(primaryKeyField);
      if (mapping == null) {
        if (this.descriptor.isDescriptorTypeAggregate()) {
          this.mayHaveNullInPrimaryKey=true;
        }
 else {
          throw DescriptorException.noMappingForPrimaryKey(primaryKeyField,this.descriptor);
        }
      }
      getPrimaryKeyMappings().add(mapping);
      if (mapping != null) {
        mapping.setIsPrimaryKeyMapping(true);
      }
      if (this.descriptor.hasMultipleTables() && (mapping != null)) {
        for (        Map keyMapping : this.descriptor.getAdditionalTablePrimaryKeyFields().values()) {
          DatabaseField secondaryField=(DatabaseField)keyMapping.get(primaryKeyField);
          if (secondaryField != null) {
            getMappingsByField().put(secondaryField,mapping);
            if (mapping.isAggregateObjectMapping()) {
              ((AggregateObjectMapping)mapping).addPrimaryKeyJoinField(primaryKeyField,secondaryField);
            }
          }
        }
      }
    }
  }
  boolean hasSimplePrimaryKey=true;
  if (null != primaryKeyMappings) {
    for (int index=0; index < getPrimaryKeyMappings().size(); index++) {
      DatabaseMapping mapping=getPrimaryKeyMappings().get(index);
      if ((mapping == null) || (!mapping.isDirectToFieldMapping())) {
        hasSimplePrimaryKey=false;
        break;
      }
    }
  }
  this.descriptor.setHasSimplePrimaryKey(hasSimplePrimaryKey);
  boolean wasIdValidationSet=true;
  if (this.descriptor.getIdValidation() == null) {
    wasIdValidationSet=false;
    List<DatabaseField> descriptorPrimaryKeyFields=this.descriptor.getPrimaryKeyFields();
    if (descriptorPrimaryKeyFields != null && descriptorPrimaryKeyFields.size() > 1) {
      this.descriptor.setIdValidation(IdValidation.NULL);
    }
 else {
      this.descriptor.setIdValidation(IdValidation.ZERO);
    }
  }
  if (this.descriptor.getPrimaryKeyFields() != null && this.descriptor.getPrimaryKeyIdValidations() == null) {
    this.descriptor.setPrimaryKeyIdValidations(new ArrayList(this.descriptor.getPrimaryKeyFields().size()));
    for (    DatabaseField field : this.descriptor.getPrimaryKeyFields()) {
      if (!wasIdValidationSet && this.descriptor.usesSequenceNumbers() && field.equals(this.descriptor.getSequenceNumberField())) {
        this.descriptor.getPrimaryKeyIdValidations().add(IdValidation.ZERO);
      }
 else {
        this.descriptor.getPrimaryKeyIdValidations().add(this.descriptor.getIdValidation());
      }
    }
  }
}