Index: JDBCStartCommand.java
===================================================================
--- JDBCStartCommand.java	(revision 63207)
+++ JDBCStartCommand.java	(working copy)
@@ -249,13 +249,12 @@
       {
          JDBCAbstractCMRFieldBridge cmrField = cmrFields[i];
          JDBCRelationMetaData relationMetaData = cmrField.getMetaData().getRelationMetaData();
+         DataSource dataSource = relationMetaData.getDataSource();
 
          // if the table for the related entity has been created
          final EntityBridge relatedEntity = cmrField.getRelatedEntity();
          if(relationMetaData.isTableMappingStyle() && createdTables.contains(relatedEntity.getEntityName()))
          {
-            DataSource dataSource = relationMetaData.getDataSource();
-
             boolean relTableExisted = SQLUtil.tableExists(cmrField.getQualifiedTableName(), entity.getDataSource());
 
             if(relTableExisted)
@@ -323,8 +322,6 @@
                {
                   log.debug("Relation table not created as requested: " + cmrField.getQualifiedTableName());
                }
-               // create Indices if needed
-               createCMRIndex(dataSource, cmrField);
 
                if(relationMetaData.getCreateTable())
                {
@@ -334,32 +331,32 @@
                }
             }
          }
+         if(createdTables.contains(relatedEntity.getEntityName()) || relationMetaData.getAlterTable())
+         {
+            // create Indices if needed
+            createCMRIndex(dataSource, cmrField);
+         }
       }
    }
 
    public void addForeignKeyConstraints() throws DeploymentException
    {
-      // Create table if necessary
       Set createdTables = getCreatedTables(manager);
 
       JDBCAbstractCMRFieldBridge[] cmrFields = entity.getCMRFields();
       for(int i = 0; i < cmrFields.length; ++i)
       {
          JDBCAbstractCMRFieldBridge cmrField = cmrFields[i];
+         EntityBridge relatedEntity = cmrField.getRelatedEntity();
          JDBCRelationMetaData relationMetaData = cmrField.getMetaData().getRelationMetaData();
 
-         // if the table for the related entity has been created
-         final EntityBridge relatedEntity = cmrField.getRelatedEntity();
-
-         // Only generate indices on foreign key columns if
-         // the table was freshly created. If not, we risk
-         // creating an index twice and get an exception from the DB
-         if(relationMetaData.isForeignKeyMappingStyle() && createdTables.contains(relatedEntity.getEntityName()))
+         if(relationMetaData.isForeignKeyMappingStyle() && (createdTables.contains(relatedEntity.getEntityName()) ||
+                                                            relationMetaData.getAlterTable()))
          {
-            createCMRIndex(((JDBCAbstractEntityBridge)relatedEntity).getDataSource(), cmrField);
+            createCMRIndex(((JDBCAbstractEntityBridge)cmrField.getRelatedEntity()).getDataSource(), cmrField);
          }
 
-         // Create my fk constraint
+         // Create fk constraint
          addForeignKeyConstraint(cmrField);
       }
    }
@@ -424,58 +421,25 @@
       }
 
       return true;
-/*
-previous implementation
-      ArrayList idxAscDesc = oldIndexes.getColumnAscDesc();
+   }
 
-      // search for for column in index
-      if(idxAscDesc != null)
-      {
-         for(int i = 0; i < idxColumns.size(); i++)
-         {
-            // only match ascending columns
-            if("A".equals(idxAscDesc.get(i)))
-            {
-               String name = columns[0];
-               String testCol = (String) idxColumns.get(i);
-               if(testCol.charAt(0) == '\"' &&
-                  testCol.charAt(testCol.charAt(testCol.length() - 1)) == '\"')
-               {
-                  testCol = testCol.substring(1, testCol.length() - 1);
-               }
+   /**
+    * Check whether a required index already exists on a table
+    *
+    * @param oldIndexes list of existing indexes
+    * @param column     column we test the existence of an index for
+    * @return True if the column has an index; otherwise false
+    */
+   private boolean hasIndex(SQLUtil.OldIndexes oldIndexes, String column)
+   {
+      ArrayList idxColumns = oldIndexes.getColumnNames();
 
-               if(testCol.equalsIgnoreCase(name))
-               {
-                  // first column matches, now check the others
-                  String idxName = (String) idxNames.get(i);
-                  int j = 1;
-                  for(; j < columns.length; j++)
-                  {
-                     name = columns[j];
-                     testCol = (String) idxColumns.get(i + j);
-                     if(testCol.charAt(0) == '\"' &&
-                        testCol.charAt(testCol.charAt(testCol.length() - 1)) == '\"')
-                     {
-                        testCol = testCol.substring(1, testCol.length() - 1);
-                     }
-
-                     String testName = (String) idxNames.get(i + j);
-                     if(!(testName.equals(idxName)
-                        &&
-                        testCol.equalsIgnoreCase(name)
-                        && idxAscDesc.get(i + j).equals("A")))
-                     {
-                        break;
-                     }
-                  }
-                  // if they all matched -> found
-                  if(j == columns.length) return true;
-               }
-            }
-         }
+      if(columnIndex(idxColumns, column) == -1)
+      {
+         return false;
       }
-      return false;
-*/
+
+      return true;
    }
 
    private int columnIndex(ArrayList idxColumns, String column)
@@ -484,11 +448,14 @@
       {
          String idxColumn = (String)idxColumns.get(j);
          idxColumn = idxColumn.trim();         
-         while(idxColumn.charAt(0) == '\"' &&
-            idxColumn.charAt(idxColumn.charAt(idxColumn.length() - 1)) == '\"')
+         while(idxColumn.startsWith("\""))
          {
-            idxColumn = idxColumn.substring(1, idxColumn.length() - 1);
+            idxColumn = idxColumn.substring(1);
          }
+         while(idxColumn.endsWith("\""))
+         {
+            idxColumn = idxColumn.substring(0, idxColumn.length() - 1);
+         }
 
          if(idxColumn.equalsIgnoreCase(column))
          {
@@ -952,6 +919,8 @@
    private void createFKIndex(JDBCRelationshipRoleMetaData metadata, DataSource dataSource, String tableName)
       throws DeploymentException
    {
+      SQLUtil.OldIndexes oldIndexes = SQLUtil.getOldIndexes(tableName, dataSource);
+
       Collection kfl = metadata.getKeyFields();
       Iterator it = kfl.iterator();
       while(it.hasNext())
@@ -959,8 +928,11 @@
          JDBCCMPFieldMetaData fi = (JDBCCMPFieldMetaData) it.next();
          if(metadata.isIndexed())
          {
-            createIndex(dataSource, tableName, fi.getFieldName(), createIndexSQL(fi, tableName));
-            idxCount++;
+            if(!hasIndex(oldIndexes, fi.getColumnName()))
+            {
+               createIndex(dataSource, tableName, fi.getFieldName(), createIndexSQL(fi, tableName));
+               idxCount++;
+            }
          }
       }
    }
