Program: 66375
Project: 66375
Buggy source:
/** 
 * INTERNAL: Rebuild the multiple table primary key map.
 */
public void initializeMultipleTablePrimaryKeyFields(){
  int additionalTablesSize=getTables().size() - 1;
  boolean isChild=hasInheritance() && getInheritancePolicy().isChildDescriptor();
  if (isChild) {
    additionalTablesSize=getTables().size() - getInheritancePolicy().getParentDescriptor().getTables().size();
  }
  if (additionalTablesSize < 1) {
    return;
  }
  ExpressionBuilder builder=new ExpressionBuilder();
  Expression joinExpression=getQueryManager().getMultipleTableJoinExpression();
  for (int index=getTables().size() - additionalTablesSize; index < getTables().size(); index++) {
    DatabaseTable table=getTables().get(index);
    Map oldKeyMapping=getAdditionalTablePrimaryKeyFields().get(table);
    if (oldKeyMapping != null) {
      if (!getQueryManager().hasCustomMultipleTableJoinExpression()) {
        for (Iterator enumtr=oldKeyMapping.keySet().iterator(); enumtr.hasNext(); ) {
          DatabaseField sourceTableField=(DatabaseField)enumtr.next();
          DatabaseField targetTableField=(DatabaseField)oldKeyMapping.get(sourceTableField);
          DatabaseTable targetTable=targetTableField.getTable();
          if (!getFields().contains(sourceTableField)) {
            getFields().addElement(sourceTableField);
          }
          if (!getFields().contains(targetTableField)) {
            getFields().addElement(targetTableField);
          }
          Expression keyJoinExpression=builder.getField(targetTableField).equal(builder.getField(sourceTableField));
          joinExpression=keyJoinExpression.and(joinExpression);
          getQueryManager().getTablesJoinExpressions().put(targetTable,keyJoinExpression);
          if (isChild) {
            getInheritancePolicy().addChildTableJoinExpressionToAllParents(targetTable,keyJoinExpression);
          }
        }
      }
    }
 else {
      Map newKeyMapping=new HashMap(getPrimaryKeyFields().size() + 1);
      getAdditionalTablePrimaryKeyFields().put(table,newKeyMapping);
      List primaryKeyFields=getPrimaryKeyFields();
      for (int pkIndex=0; pkIndex < primaryKeyFields.size(); pkIndex++) {
        DatabaseField primaryKeyField=(DatabaseField)primaryKeyFields.get(pkIndex);
        DatabaseField secondaryKeyField=primaryKeyField.clone();
        secondaryKeyField.setTable(table);
        newKeyMapping.put(primaryKeyField,secondaryKeyField);
        getFields().addElement(secondaryKeyField);
        if (!getQueryManager().hasCustomMultipleTableJoinExpression()) {
          Expression keyJoinExpression=builder.getField(secondaryKeyField).equal(builder.getField(primaryKeyField));
          joinExpression=keyJoinExpression.and(joinExpression);
          getQueryManager().getTablesJoinExpressions().put(table,keyJoinExpression);
          if (isChild) {
            getInheritancePolicy().addChildTableJoinExpressionToAllParents(table,keyJoinExpression);
          }
        }
      }
    }
  }
  if (joinExpression != null) {
    getQueryManager().setInternalMultipleTableJoinExpression(joinExpression);
  }
  if (getQueryManager().hasCustomMultipleTableJoinExpression()) {
    Map tablesJoinExpressions=SQLSelectStatement.mapTableToExpression(joinExpression,getTables());
    getQueryManager().getTablesJoinExpressions().putAll(tablesJoinExpressions);
    if (isChild) {
      for (int index=getTables().size() - additionalTablesSize; index < getTables().size(); index++) {
        DatabaseTable table=getTables().elementAt(index);
        getInheritancePolicy().addChildTableJoinExpressionToAllParents(table,(Expression)tablesJoinExpressions.get(table));
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * INTERNAL: Rebuild the multiple table primary key map.
 */
public void initializeMultipleTablePrimaryKeyFields(){
  int tableSize=getTables().size();
  int additionalTablesSize=tableSize - 1;
  boolean isChild=hasInheritance() && getInheritancePolicy().isChildDescriptor();
  if (isChild) {
    additionalTablesSize=tableSize - getInheritancePolicy().getParentDescriptor().getTables().size();
  }
  if (additionalTablesSize < 1) {
    return;
  }
  ExpressionBuilder builder=new ExpressionBuilder();
  Expression joinExpression=getQueryManager().getMultipleTableJoinExpression();
  for (int index=tableSize - additionalTablesSize; index < tableSize; index++) {
    DatabaseTable table=getTables().get(index);
    Map<DatabaseField,DatabaseField> oldKeyMapping=getAdditionalTablePrimaryKeyFields().get(table);
    if (oldKeyMapping != null) {
      if (!getQueryManager().hasCustomMultipleTableJoinExpression()) {
        Expression keyJoinExpression=null;
        for (        Map.Entry<DatabaseField,DatabaseField> entry : oldKeyMapping.entrySet()) {
          DatabaseField sourceTableField=entry.getKey();
          DatabaseField targetTableField=entry.getValue();
          if (!getFields().contains(sourceTableField)) {
            getFields().add(sourceTableField);
          }
          if (!getFields().contains(targetTableField)) {
            getFields().add(targetTableField);
          }
          keyJoinExpression=builder.getField(targetTableField).equal(builder.getField(sourceTableField)).and(keyJoinExpression);
          joinExpression=keyJoinExpression.and(joinExpression);
        }
        getQueryManager().getTablesJoinExpressions().put(table,keyJoinExpression);
        if (isChild) {
          getInheritancePolicy().addChildTableJoinExpressionToAllParents(table,keyJoinExpression);
        }
      }
    }
 else {
      Map newKeyMapping=new HashMap(getPrimaryKeyFields().size());
      getAdditionalTablePrimaryKeyFields().put(table,newKeyMapping);
      Expression keyJoinExpression=null;
      for (      DatabaseField primaryKeyField : getPrimaryKeyFields()) {
        DatabaseField secondaryKeyField=primaryKeyField.clone();
        secondaryKeyField.setTable(table);
        newKeyMapping.put(primaryKeyField,secondaryKeyField);
        getFields().add(secondaryKeyField);
        if (!getQueryManager().hasCustomMultipleTableJoinExpression()) {
          keyJoinExpression=builder.getField(secondaryKeyField).equal(builder.getField(primaryKeyField)).and(keyJoinExpression);
          joinExpression=keyJoinExpression.and(joinExpression);
        }
      }
      getQueryManager().getTablesJoinExpressions().put(table,keyJoinExpression);
      if (isChild) {
        getInheritancePolicy().addChildTableJoinExpressionToAllParents(table,keyJoinExpression);
      }
    }
  }
  if (joinExpression != null) {
    getQueryManager().setInternalMultipleTableJoinExpression(joinExpression);
  }
  if (getQueryManager().hasCustomMultipleTableJoinExpression()) {
    Map tablesJoinExpressions=SQLSelectStatement.mapTableToExpression(joinExpression,getTables());
    getQueryManager().getTablesJoinExpressions().putAll(tablesJoinExpressions);
    if (isChild) {
      for (int index=tableSize - additionalTablesSize; index < tableSize; index++) {
        DatabaseTable table=getTables().get(index);
        getInheritancePolicy().addChildTableJoinExpressionToAllParents(table,(Expression)tablesJoinExpressions.get(table));
      }
    }
  }
}