Program: 63243
Project: 63243
Buggy source:
/** 
 * Pre-build the SQL statement from the expressions.
 */
@Override public void prepareUpdateAll(){
  ExpressionBuilder builder=((UpdateAllQuery)getQuery()).getExpressionBuilder();
  HashMap updateClauses=((UpdateAllQuery)getQuery()).getUpdateClauses();
  boolean updateClausesHasBeenCloned=false;
  OptimisticLockingPolicy policy=getDescriptor().getOptimisticLockingPolicy();
  if (policy != null) {
    if (policy.getWriteLockField() != null) {
      Expression writeLock=builder.getField(policy.getWriteLockField());
      Expression writeLockUpdateExpression=policy.getWriteLockUpdateExpression(builder,getQuery().getSession());
      if (writeLockUpdateExpression != null) {
        updateClauses=(HashMap)updateClauses.clone();
        updateClausesHasBeenCloned=true;
        updateClauses.put(writeLock,writeLockUpdateExpression);
      }
    }
  }
  if (getDescriptor().hasSerializedObjectPolicy()) {
    if (!updateClausesHasBeenCloned) {
      updateClauses=(HashMap)updateClauses.clone();
      updateClausesHasBeenCloned=true;
    }
    Expression sopFieldExpression=builder.getField(getDescriptor().getSerializedObjectPolicy().getField());
    updateClauses.put(sopFieldExpression,new ConstantExpression(null,sopFieldExpression));
  }
  HashMap tables_databaseFieldsToValues=new HashMap();
  HashMap<DatabaseTable,List<DatabaseField>> tablesToPrimaryKeyFields=new HashMap();
  Iterator it=updateClauses.entrySet().iterator();
  while (it.hasNext()) {
    Map.Entry entry=(Map.Entry)it.next();
    Object fieldObject=entry.getKey();
    DataExpression fieldExpression=null;
    Expression baseExpression=null;
    String attributeName=null;
    if (fieldObject instanceof String) {
      attributeName=(String)fieldObject;
    }
 else {
      fieldExpression=(DataExpression)fieldObject;
    }
    DatabaseField field=null;
    DatabaseMapping mapping=null;
    if (attributeName != null) {
      mapping=getDescriptor().getObjectBuilder().getMappingForAttributeName(attributeName);
      if (mapping != null && !mapping.getFields().isEmpty()) {
        field=mapping.getFields().get(0);
      }
      if (field == null) {
        throw QueryException.updateAllQueryAddUpdateDoesNotDefineField(getDescriptor(),getQuery(),attributeName);
      }
      baseExpression=((UpdateAllQuery)getQuery()).getExpressionBuilder().get(attributeName);
    }
 else     if (fieldExpression != null) {
      if (fieldExpression.getBaseExpression() instanceof ExpressionBuilder) {
        field=getDescriptor().getObjectBuilder().getFieldForQueryKeyName(fieldExpression.getName());
      }
      if (field == null) {
        DataExpression fieldExpressionClone=(DataExpression)fieldExpression.clone();
        fieldExpressionClone.getBuilder().setQueryClass(getQuery().getReferenceClass());
        fieldExpressionClone.getBuilder().setSession(getSession().getRootSession(null));
        field=fieldExpressionClone.getField();
        if (field == null) {
          throw QueryException.updateAllQueryAddUpdateDoesNotDefineField(getDescriptor(),getQuery(),fieldExpression.toString());
        }
      }
      mapping=getDescriptor().getObjectBuilder().getMappingForField(field);
      baseExpression=fieldExpression;
    }
    Object valueObject=entry.getValue();
    Vector fields;
    Vector values;
    Vector baseExpressions;
    if (mapping != null && mapping.isOneToOneMapping()) {
      fields=mapping.getFields();
      int fieldsSize=fields.size();
      values=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(fieldsSize);
      baseExpressions=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(fieldsSize);
      for (int i=0; i < fieldsSize; i++) {
        if (valueObject instanceof ConstantExpression) {
          valueObject=((ConstantExpression)valueObject).getValue();
        }
        if (valueObject == null) {
          values.add(null);
        }
 else {
          DatabaseField targetField=((OneToOneMapping)mapping).getSourceToTargetKeyFields().get(fields.get(i));
          if (valueObject instanceof Expression) {
            Expression exp=((Expression)((Expression)valueObject).clone()).getField(targetField);
            if (exp.isParameterExpression()) {
              ((ParameterExpression)exp).setType(targetField.getType());
            }
            values.add(exp);
          }
 else {
            values.add(((OneToOneMapping)mapping).getReferenceDescriptor().getObjectBuilder().extractValueFromObjectForField(valueObject,targetField,getSession()));
          }
        }
        baseExpressions.add(new FieldExpression((DatabaseField)fields.elementAt(i),((QueryKeyExpression)baseExpression).getBaseExpression()));
      }
    }
 else {
      fields=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(1);
      fields.add(field);
      values=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(1);
      values.add(valueObject);
      baseExpressions=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(1);
      baseExpressions.add(baseExpression);
    }
    int fieldsSize=fields.size();
    for (int i=0; i < fieldsSize; i++) {
      field=(DatabaseField)fields.elementAt(i);
      DatabaseTable table=field.getTable();
      if (!getDescriptor().getTables().contains(table)) {
        if (attributeName != null) {
          throw QueryException.updateAllQueryAddUpdateDefinesWrongField(getDescriptor(),getQuery(),attributeName,field.getQualifiedName());
        }
 else {
          throw QueryException.updateAllQueryAddUpdateDefinesWrongField(getDescriptor(),getQuery(),fieldExpression.toString(),field.getQualifiedName());
        }
      }
      HashMap databaseFieldsToValues=(HashMap)tables_databaseFieldsToValues.get(table);
      if (databaseFieldsToValues == null) {
        databaseFieldsToValues=new HashMap();
        tables_databaseFieldsToValues.put(table,databaseFieldsToValues);
        tablesToPrimaryKeyFields.put(table,getPrimaryKeyFieldsForTable(table));
      }
      Object value=values.elementAt(i);
      Expression valueExpression;
      if (valueObject instanceof Expression) {
        valueExpression=(Expression)value;
      }
 else {
        valueExpression=builder.value(value);
      }
      if (valueExpression.isValueExpression()) {
        valueExpression.setLocalBase((Expression)baseExpressions.elementAt(i));
      }
      databaseFieldsToValues.put(field,valueExpression);
    }
  }
  SQLCall selectCallForExist=null;
  SQLSelectStatement selectStatementForExist=createSQLSelectStatementForModifyAll(getSelectionCriteria());
  boolean isMainCase=selectStatementForExist.requiresAliases();
  if (isMainCase) {
    if (getExecutionSession().getPlatform().shouldAlwaysUseTempStorageForModifyAll()) {
      prepareUpdateAllUsingTempStorage(tables_databaseFieldsToValues,tablesToPrimaryKeyFields);
      return;
    }
  }
  selectCallForExist=(SQLCall)selectStatementForExist.buildCall(getSession());
  ExpressionIterator expRequiresSelectIterator=new ExpressionIterator(){
    @Override public void iterate(    Expression each){
      if (getResult() == null) {
        return;
      }
      if (each instanceof DataExpression) {
        DataExpression dataExpression=(DataExpression)each;
        Expression baseExpression=dataExpression.getBaseExpression();
        if (baseExpression != null && !(baseExpression instanceof ExpressionBuilder)) {
          boolean stop=true;
          if (baseExpression instanceof DataExpression) {
            DataExpression baseDataExpression=(DataExpression)baseExpression;
            if (baseDataExpression.getMapping() != null && baseDataExpression.getMapping().isAggregateObjectMapping()) {
              stop=false;
            }
          }
          if (stop) {
            setResult(null);
            return;
          }
        }
        DatabaseField field=dataExpression.getField();
        if (field != null) {
          if (!field.getTable().equals((DatabaseTable)getResult())) {
            setResult(null);
            return;
          }
        }
      }
    }
    @Override public boolean shouldIterateOverSubSelects(){
      return true;
    }
  }
;
  HashMap tables_databaseFieldsToValuesCopy=new HashMap();
  it=tables_databaseFieldsToValues.entrySet().iterator();
  while (it.hasNext()) {
    Map.Entry entry=(Map.Entry)it.next();
    DatabaseTable table=(DatabaseTable)entry.getKey();
    HashMap databaseFieldsToValues=(HashMap)entry.getValue();
    HashMap databaseFieldsToValuesCopy=new HashMap();
    tables_databaseFieldsToValuesCopy.put(table,databaseFieldsToValuesCopy);
    Iterator itFieldsToValues=databaseFieldsToValues.entrySet().iterator();
    while (itFieldsToValues.hasNext()) {
      Map.Entry entry2=(Map.Entry)itFieldsToValues.next();
      DatabaseField field=(DatabaseField)entry2.getKey();
      Expression value=(Expression)entry2.getValue();
      expRequiresSelectIterator.setResult(table);
      Expression valueClone=(Expression)value.clone();
      valueClone.getBuilder().setSession(getSession());
      valueClone.getBuilder().setQueryClass(getQuery().getReferenceClass());
      expRequiresSelectIterator.iterateOn(valueClone);
      if (expRequiresSelectIterator.getResult() == null) {
        if (getExecutionSession().getPlatform().shouldAlwaysUseTempStorageForModifyAll()) {
          prepareUpdateAllUsingTempStorage(tables_databaseFieldsToValues,tablesToPrimaryKeyFields);
          return;
        }
        SQLSelectStatement selStatement=createSQLSelectStatementForAssignedExpressionForUpdateAll(value);
        databaseFieldsToValuesCopy.put(field,selStatement);
      }
 else {
        databaseFieldsToValuesCopy.put(field,valueClone);
      }
    }
  }
  HashMap tables_databaseFieldsToValuesOriginal=tables_databaseFieldsToValues;
  tables_databaseFieldsToValues=tables_databaseFieldsToValuesCopy;
  if (tables_databaseFieldsToValues.size() == 1) {
    Map.Entry entry=(Map.Entry)tables_databaseFieldsToValues.entrySet().iterator().next();
    DatabaseTable table=(DatabaseTable)entry.getKey();
    HashMap databaseFieldsToValues=(HashMap)entry.getValue();
    Collection primaryKeyFields=tablesToPrimaryKeyFields.values().iterator().next();
    setSQLStatement(buildUpdateAllStatement(table,databaseFieldsToValues,selectCallForExist,selectStatementForExist,primaryKeyFields));
  }
 else {
    ExpressionIterator expIterator=new ExpressionIterator(){
      @Override public void iterate(      Expression each){
        if (each instanceof DataExpression) {
          DataExpression dataExpression=(DataExpression)each;
          DatabaseField field=dataExpression.getField();
          if (field != null) {
            ((Collection)getResult()).add(field);
          }
        }
      }
      @Override public boolean shouldIterateOverSubSelects(){
        return true;
      }
    }
;
    HashSet selectCallForExistFields=new HashSet();
    if (selectCallForExist != null) {
      expIterator.setResult(selectCallForExistFields);
      expIterator.iterateOn(selectStatementForExist.getWhereClause());
    }
    HashMap tablesToLeftFields=new HashMap();
    HashMap tablesToRightFields=new HashMap();
    Vector beforeTables=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance();
    Vector afterTables=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance();
    HashMap simpleConflicts=new HashMap();
    it=tables_databaseFieldsToValues.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry entry=(Map.Entry)it.next();
      DatabaseTable table=(DatabaseTable)entry.getKey();
      HashMap databaseFieldsToValues=(HashMap)entry.getValue();
      HashSet leftFields=new HashSet(databaseFieldsToValues.size());
      HashSet rightFields=(HashSet)selectCallForExistFields.clone();
      expIterator.setResult(rightFields);
      Iterator itDatabaseFieldsToValues=databaseFieldsToValues.entrySet().iterator();
      while (itDatabaseFieldsToValues.hasNext()) {
        Map.Entry databaseFieldValueEntry=(Map.Entry)itDatabaseFieldsToValues.next();
        DatabaseField field=(DatabaseField)databaseFieldValueEntry.getKey();
        leftFields.add(field);
        Object value=databaseFieldValueEntry.getValue();
        if (value instanceof Expression) {
          Expression valueExpression=(Expression)value;
          expIterator.iterateOn(valueExpression);
        }
 else {
          SQLSelectStatement selStatement=(SQLSelectStatement)value;
          expIterator.iterateOn((Expression)selStatement.getFields().get(0));
          expIterator.iterateOn(selStatement.getWhereClause());
        }
      }
      Iterator itProcessedTables=tablesToLeftFields.keySet().iterator();
      while (itProcessedTables.hasNext()) {
        DatabaseTable processedTable=(DatabaseTable)itProcessedTables.next();
        HashSet processedTableLeftFields=(HashSet)tablesToLeftFields.get(processedTable);
        HashSet processedTableRightFields=(HashSet)tablesToRightFields.get(processedTable);
        boolean tableBeforeProcessedTable=false;
        Iterator itProcessedTableLeftField=processedTableLeftFields.iterator();
        while (itProcessedTableLeftField.hasNext()) {
          if (rightFields.contains(itProcessedTableLeftField.next())) {
            tableBeforeProcessedTable=true;
            break;
          }
        }
        boolean processedTableBeforeTable=false;
        Iterator itLeftField=leftFields.iterator();
        while (itLeftField.hasNext()) {
          if (processedTableRightFields.contains(itLeftField.next())) {
            processedTableBeforeTable=true;
            break;
          }
        }
        if (tableBeforeProcessedTable && !processedTableBeforeTable) {
          beforeTables.add(table);
          afterTables.add(processedTable);
        }
 else         if (!tableBeforeProcessedTable && processedTableBeforeTable) {
          beforeTables.add(processedTable);
          afterTables.add(table);
        }
 else         if (tableBeforeProcessedTable && processedTableBeforeTable) {
          simpleConflicts.put(processedTable,table);
        }
      }
      tablesToLeftFields.put(table,leftFields);
      tablesToRightFields.put(table,rightFields);
    }
    if (!simpleConflicts.isEmpty()) {
      prepareUpdateAllUsingTempStorage(tables_databaseFieldsToValuesOriginal,tablesToPrimaryKeyFields);
      return;
    }
    Vector orderedTables=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(tables_databaseFieldsToValues.size());
    while (!beforeTables.isEmpty()) {
      DatabaseTable firstTable=null;
      for (int i=0; i < beforeTables.size(); i++) {
        DatabaseTable beforeTable=(DatabaseTable)beforeTables.elementAt(i);
        if (!afterTables.contains(beforeTable)) {
          firstTable=beforeTable;
          break;
        }
      }
      if (firstTable == null) {
        prepareUpdateAllUsingTempStorage(tables_databaseFieldsToValuesOriginal,tablesToPrimaryKeyFields);
        return;
      }
 else {
        for (int i=beforeTables.size() - 1; i >= 0; i--) {
          if (beforeTables.elementAt(i).equals(firstTable)) {
            beforeTables.remove(i);
            afterTables.remove(i);
          }
        }
        orderedTables.addElement(firstTable);
      }
    }
    Iterator itTables=tables_databaseFieldsToValues.keySet().iterator();
    while (itTables.hasNext()) {
      DatabaseTable table=(DatabaseTable)itTables.next();
      if (!orderedTables.contains(table)) {
        orderedTables.add(table);
      }
    }
    for (int i=0; i < orderedTables.size(); i++) {
      DatabaseTable table=(DatabaseTable)orderedTables.elementAt(i);
      HashMap databaseFieldsToValues=(HashMap)tables_databaseFieldsToValues.get(table);
      Collection primaryKeyFields=tablesToPrimaryKeyFields.get(table);
      getSQLStatements().addElement(buildUpdateAllStatement(table,databaseFieldsToValues,selectCallForExist,selectStatementForExist,primaryKeyFields));
    }
  }
  ((UpdateAllQuery)getQuery()).setIsPreparedUsingTempStorage(false);
  super.prepareUpdateAll();
}

Fixed version (ground truth):
/** 
 * Pre-build the SQL statement from the expressions.
 */
@Override public void prepareUpdateAll(){
  ExpressionBuilder builder=((UpdateAllQuery)getQuery()).getExpressionBuilder();
  HashMap updateClauses=((UpdateAllQuery)getQuery()).getUpdateClauses();
  boolean updateClausesHasBeenCloned=false;
  OptimisticLockingPolicy policy=getDescriptor().getOptimisticLockingPolicy();
  if (policy != null) {
    if (policy.getWriteLockField() != null) {
      Expression writeLock=builder.getField(policy.getWriteLockField());
      if (!isFieldInUpdate(writeLock,updateClauses)) {
        Expression writeLockUpdateExpression=policy.getWriteLockUpdateExpression(builder,getQuery().getSession());
        if (writeLockUpdateExpression != null) {
          updateClauses=(HashMap)updateClauses.clone();
          updateClausesHasBeenCloned=true;
          updateClauses.put(writeLock,writeLockUpdateExpression);
        }
      }
    }
  }
  if (getDescriptor().hasSerializedObjectPolicy()) {
    if (!updateClausesHasBeenCloned) {
      updateClauses=(HashMap)updateClauses.clone();
      updateClausesHasBeenCloned=true;
    }
    Expression sopFieldExpression=builder.getField(getDescriptor().getSerializedObjectPolicy().getField());
    updateClauses.put(sopFieldExpression,new ConstantExpression(null,sopFieldExpression));
  }
  HashMap tables_databaseFieldsToValues=new HashMap();
  HashMap<DatabaseTable,List<DatabaseField>> tablesToPrimaryKeyFields=new HashMap();
  Iterator it=updateClauses.entrySet().iterator();
  while (it.hasNext()) {
    Map.Entry entry=(Map.Entry)it.next();
    Object fieldObject=entry.getKey();
    DataExpression fieldExpression=null;
    Expression baseExpression=null;
    String attributeName=null;
    if (fieldObject instanceof String) {
      attributeName=(String)fieldObject;
    }
 else {
      fieldExpression=(DataExpression)fieldObject;
    }
    DatabaseField field=null;
    DatabaseMapping mapping=null;
    if (attributeName != null) {
      mapping=getDescriptor().getObjectBuilder().getMappingForAttributeName(attributeName);
      if (mapping != null && !mapping.getFields().isEmpty()) {
        field=mapping.getFields().get(0);
      }
      if (field == null) {
        throw QueryException.updateAllQueryAddUpdateDoesNotDefineField(getDescriptor(),getQuery(),attributeName);
      }
      baseExpression=((UpdateAllQuery)getQuery()).getExpressionBuilder().get(attributeName);
    }
 else     if (fieldExpression != null) {
      if (fieldExpression.getBaseExpression() instanceof ExpressionBuilder) {
        field=getDescriptor().getObjectBuilder().getFieldForQueryKeyName(fieldExpression.getName());
      }
      if (field == null) {
        DataExpression fieldExpressionClone=(DataExpression)fieldExpression.clone();
        fieldExpressionClone.getBuilder().setQueryClass(getQuery().getReferenceClass());
        fieldExpressionClone.getBuilder().setSession(getSession().getRootSession(null));
        field=fieldExpressionClone.getField();
        if (field == null) {
          throw QueryException.updateAllQueryAddUpdateDoesNotDefineField(getDescriptor(),getQuery(),fieldExpression.toString());
        }
      }
      mapping=getDescriptor().getObjectBuilder().getMappingForField(field);
      baseExpression=fieldExpression;
    }
    Object valueObject=entry.getValue();
    Vector fields;
    Vector values;
    Vector baseExpressions;
    if (mapping != null && mapping.isOneToOneMapping()) {
      fields=mapping.getFields();
      int fieldsSize=fields.size();
      values=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(fieldsSize);
      baseExpressions=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(fieldsSize);
      for (int i=0; i < fieldsSize; i++) {
        if (valueObject instanceof ConstantExpression) {
          valueObject=((ConstantExpression)valueObject).getValue();
        }
        if (valueObject == null) {
          values.add(null);
        }
 else {
          DatabaseField targetField=((OneToOneMapping)mapping).getSourceToTargetKeyFields().get(fields.get(i));
          if (valueObject instanceof Expression) {
            Expression exp=((Expression)((Expression)valueObject).clone()).getField(targetField);
            if (exp.isParameterExpression()) {
              ((ParameterExpression)exp).setType(targetField.getType());
            }
            values.add(exp);
          }
 else {
            values.add(((OneToOneMapping)mapping).getReferenceDescriptor().getObjectBuilder().extractValueFromObjectForField(valueObject,targetField,getSession()));
          }
        }
        baseExpressions.add(new FieldExpression((DatabaseField)fields.elementAt(i),((QueryKeyExpression)baseExpression).getBaseExpression()));
      }
    }
 else {
      fields=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(1);
      fields.add(field);
      values=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(1);
      values.add(valueObject);
      baseExpressions=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(1);
      baseExpressions.add(baseExpression);
    }
    int fieldsSize=fields.size();
    for (int i=0; i < fieldsSize; i++) {
      field=(DatabaseField)fields.elementAt(i);
      DatabaseTable table=field.getTable();
      if (!getDescriptor().getTables().contains(table)) {
        if (attributeName != null) {
          throw QueryException.updateAllQueryAddUpdateDefinesWrongField(getDescriptor(),getQuery(),attributeName,field.getQualifiedName());
        }
 else {
          throw QueryException.updateAllQueryAddUpdateDefinesWrongField(getDescriptor(),getQuery(),fieldExpression.toString(),field.getQualifiedName());
        }
      }
      HashMap databaseFieldsToValues=(HashMap)tables_databaseFieldsToValues.get(table);
      if (databaseFieldsToValues == null) {
        databaseFieldsToValues=new HashMap();
        tables_databaseFieldsToValues.put(table,databaseFieldsToValues);
        tablesToPrimaryKeyFields.put(table,getPrimaryKeyFieldsForTable(table));
      }
      Object value=values.elementAt(i);
      Expression valueExpression;
      if (valueObject instanceof Expression) {
        valueExpression=(Expression)value;
      }
 else {
        valueExpression=builder.value(value);
      }
      if (valueExpression.isValueExpression()) {
        valueExpression.setLocalBase((Expression)baseExpressions.elementAt(i));
      }
      databaseFieldsToValues.put(field,valueExpression);
    }
  }
  SQLCall selectCallForExist=null;
  SQLSelectStatement selectStatementForExist=createSQLSelectStatementForModifyAll(getSelectionCriteria());
  boolean isMainCase=selectStatementForExist.requiresAliases();
  if (isMainCase) {
    if (getExecutionSession().getPlatform().shouldAlwaysUseTempStorageForModifyAll()) {
      prepareUpdateAllUsingTempStorage(tables_databaseFieldsToValues,tablesToPrimaryKeyFields);
      return;
    }
  }
  selectCallForExist=(SQLCall)selectStatementForExist.buildCall(getSession());
  ExpressionIterator expRequiresSelectIterator=new ExpressionIterator(){
    @Override public void iterate(    Expression each){
      if (getResult() == null) {
        return;
      }
      if (each instanceof DataExpression) {
        DataExpression dataExpression=(DataExpression)each;
        Expression baseExpression=dataExpression.getBaseExpression();
        if (baseExpression != null && !(baseExpression instanceof ExpressionBuilder)) {
          boolean stop=true;
          if (baseExpression instanceof DataExpression) {
            DataExpression baseDataExpression=(DataExpression)baseExpression;
            if (baseDataExpression.getMapping() != null && baseDataExpression.getMapping().isAggregateObjectMapping()) {
              stop=false;
            }
          }
          if (stop) {
            setResult(null);
            return;
          }
        }
        DatabaseField field=dataExpression.getField();
        if (field != null) {
          if (!field.getTable().equals((DatabaseTable)getResult())) {
            setResult(null);
            return;
          }
        }
      }
    }
    @Override public boolean shouldIterateOverSubSelects(){
      return true;
    }
  }
;
  HashMap tables_databaseFieldsToValuesCopy=new HashMap();
  it=tables_databaseFieldsToValues.entrySet().iterator();
  while (it.hasNext()) {
    Map.Entry entry=(Map.Entry)it.next();
    DatabaseTable table=(DatabaseTable)entry.getKey();
    HashMap databaseFieldsToValues=(HashMap)entry.getValue();
    HashMap databaseFieldsToValuesCopy=new HashMap();
    tables_databaseFieldsToValuesCopy.put(table,databaseFieldsToValuesCopy);
    Iterator itFieldsToValues=databaseFieldsToValues.entrySet().iterator();
    while (itFieldsToValues.hasNext()) {
      Map.Entry entry2=(Map.Entry)itFieldsToValues.next();
      DatabaseField field=(DatabaseField)entry2.getKey();
      Expression value=(Expression)entry2.getValue();
      expRequiresSelectIterator.setResult(table);
      Expression valueClone=(Expression)value.clone();
      valueClone.getBuilder().setSession(getSession());
      valueClone.getBuilder().setQueryClass(getQuery().getReferenceClass());
      expRequiresSelectIterator.iterateOn(valueClone);
      if (expRequiresSelectIterator.getResult() == null) {
        if (getExecutionSession().getPlatform().shouldAlwaysUseTempStorageForModifyAll()) {
          prepareUpdateAllUsingTempStorage(tables_databaseFieldsToValues,tablesToPrimaryKeyFields);
          return;
        }
        SQLSelectStatement selStatement=createSQLSelectStatementForAssignedExpressionForUpdateAll(value);
        databaseFieldsToValuesCopy.put(field,selStatement);
      }
 else {
        databaseFieldsToValuesCopy.put(field,valueClone);
      }
    }
  }
  HashMap tables_databaseFieldsToValuesOriginal=tables_databaseFieldsToValues;
  tables_databaseFieldsToValues=tables_databaseFieldsToValuesCopy;
  if (tables_databaseFieldsToValues.size() == 1) {
    Map.Entry entry=(Map.Entry)tables_databaseFieldsToValues.entrySet().iterator().next();
    DatabaseTable table=(DatabaseTable)entry.getKey();
    HashMap databaseFieldsToValues=(HashMap)entry.getValue();
    Collection primaryKeyFields=tablesToPrimaryKeyFields.values().iterator().next();
    setSQLStatement(buildUpdateAllStatement(table,databaseFieldsToValues,selectCallForExist,selectStatementForExist,primaryKeyFields));
  }
 else {
    ExpressionIterator expIterator=new ExpressionIterator(){
      @Override public void iterate(      Expression each){
        if (each instanceof DataExpression) {
          DataExpression dataExpression=(DataExpression)each;
          DatabaseField field=dataExpression.getField();
          if (field != null) {
            ((Collection)getResult()).add(field);
          }
        }
      }
      @Override public boolean shouldIterateOverSubSelects(){
        return true;
      }
    }
;
    HashSet selectCallForExistFields=new HashSet();
    if (selectCallForExist != null) {
      expIterator.setResult(selectCallForExistFields);
      expIterator.iterateOn(selectStatementForExist.getWhereClause());
    }
    HashMap tablesToLeftFields=new HashMap();
    HashMap tablesToRightFields=new HashMap();
    Vector beforeTables=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance();
    Vector afterTables=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance();
    HashMap simpleConflicts=new HashMap();
    it=tables_databaseFieldsToValues.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry entry=(Map.Entry)it.next();
      DatabaseTable table=(DatabaseTable)entry.getKey();
      HashMap databaseFieldsToValues=(HashMap)entry.getValue();
      HashSet leftFields=new HashSet(databaseFieldsToValues.size());
      HashSet rightFields=(HashSet)selectCallForExistFields.clone();
      expIterator.setResult(rightFields);
      Iterator itDatabaseFieldsToValues=databaseFieldsToValues.entrySet().iterator();
      while (itDatabaseFieldsToValues.hasNext()) {
        Map.Entry databaseFieldValueEntry=(Map.Entry)itDatabaseFieldsToValues.next();
        DatabaseField field=(DatabaseField)databaseFieldValueEntry.getKey();
        leftFields.add(field);
        Object value=databaseFieldValueEntry.getValue();
        if (value instanceof Expression) {
          Expression valueExpression=(Expression)value;
          expIterator.iterateOn(valueExpression);
        }
 else {
          SQLSelectStatement selStatement=(SQLSelectStatement)value;
          expIterator.iterateOn((Expression)selStatement.getFields().get(0));
          expIterator.iterateOn(selStatement.getWhereClause());
        }
      }
      Iterator itProcessedTables=tablesToLeftFields.keySet().iterator();
      while (itProcessedTables.hasNext()) {
        DatabaseTable processedTable=(DatabaseTable)itProcessedTables.next();
        HashSet processedTableLeftFields=(HashSet)tablesToLeftFields.get(processedTable);
        HashSet processedTableRightFields=(HashSet)tablesToRightFields.get(processedTable);
        boolean tableBeforeProcessedTable=false;
        Iterator itProcessedTableLeftField=processedTableLeftFields.iterator();
        while (itProcessedTableLeftField.hasNext()) {
          if (rightFields.contains(itProcessedTableLeftField.next())) {
            tableBeforeProcessedTable=true;
            break;
          }
        }
        boolean processedTableBeforeTable=false;
        Iterator itLeftField=leftFields.iterator();
        while (itLeftField.hasNext()) {
          if (processedTableRightFields.contains(itLeftField.next())) {
            processedTableBeforeTable=true;
            break;
          }
        }
        if (tableBeforeProcessedTable && !processedTableBeforeTable) {
          beforeTables.add(table);
          afterTables.add(processedTable);
        }
 else         if (!tableBeforeProcessedTable && processedTableBeforeTable) {
          beforeTables.add(processedTable);
          afterTables.add(table);
        }
 else         if (tableBeforeProcessedTable && processedTableBeforeTable) {
          simpleConflicts.put(processedTable,table);
        }
      }
      tablesToLeftFields.put(table,leftFields);
      tablesToRightFields.put(table,rightFields);
    }
    if (!simpleConflicts.isEmpty()) {
      prepareUpdateAllUsingTempStorage(tables_databaseFieldsToValuesOriginal,tablesToPrimaryKeyFields);
      return;
    }
    Vector orderedTables=org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(tables_databaseFieldsToValues.size());
    while (!beforeTables.isEmpty()) {
      DatabaseTable firstTable=null;
      for (int i=0; i < beforeTables.size(); i++) {
        DatabaseTable beforeTable=(DatabaseTable)beforeTables.elementAt(i);
        if (!afterTables.contains(beforeTable)) {
          firstTable=beforeTable;
          break;
        }
      }
      if (firstTable == null) {
        prepareUpdateAllUsingTempStorage(tables_databaseFieldsToValuesOriginal,tablesToPrimaryKeyFields);
        return;
      }
 else {
        for (int i=beforeTables.size() - 1; i >= 0; i--) {
          if (beforeTables.elementAt(i).equals(firstTable)) {
            beforeTables.remove(i);
            afterTables.remove(i);
          }
        }
        orderedTables.addElement(firstTable);
      }
    }
    Iterator itTables=tables_databaseFieldsToValues.keySet().iterator();
    while (itTables.hasNext()) {
      DatabaseTable table=(DatabaseTable)itTables.next();
      if (!orderedTables.contains(table)) {
        orderedTables.add(table);
      }
    }
    for (int i=0; i < orderedTables.size(); i++) {
      DatabaseTable table=(DatabaseTable)orderedTables.elementAt(i);
      HashMap databaseFieldsToValues=(HashMap)tables_databaseFieldsToValues.get(table);
      Collection primaryKeyFields=tablesToPrimaryKeyFields.get(table);
      getSQLStatements().addElement(buildUpdateAllStatement(table,databaseFieldsToValues,selectCallForExist,selectStatementForExist,primaryKeyFields));
    }
  }
  ((UpdateAllQuery)getQuery()).setIsPreparedUsingTempStorage(false);
  super.prepareUpdateAll();
}