You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2011/07/19 16:14:00 UTC

svn commit: r1148345 - in /db/torque/torque4/trunk: torque-runtime/src/main/java/org/apache/torque/ torque-runtime/src/main/java/org/apache/torque/map/ torque-runtime/src/test/java/org/apache/torque/ torque-templates/src/main/resources/org/apache/torqu...

Author: tfischer
Date: Tue Jul 19 14:13:58 2011
New Revision: 1148345

URL: http://svn.apache.org/viewvc?rev=1148345&view=rev
Log:
TORQUE-164: Fix foreign key storage in database map

Added:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ForeignKeyMap.java
Modified:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ColumnMap.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/doBuild.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/imports.vm

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java?rev=1148345&r1=1148344&r2=1148345&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java Tue Jul 19 14:13:58 2011
@@ -25,6 +25,7 @@ import java.util.Map;
 import org.apache.torque.adapter.DB;
 import org.apache.torque.adapter.IDMethod;
 import org.apache.torque.dsfactory.DataSourceFactory;
+import org.apache.torque.map.ColumnMap;
 import org.apache.torque.map.DatabaseMap;
 import org.apache.torque.map.TableMap;
 import org.apache.torque.oid.IDBroker;
@@ -212,10 +213,19 @@ public class Database
         }
         setIdTable("ID_TABLE");
         TableMap tMap = getIdTable();
-        tMap.addPrimaryKey("ID_TABLE_ID", new Integer(0));
-        tMap.addColumn("TABLE_NAME", "");
-        tMap.addColumn("NEXT_ID", new Integer(0));
-        tMap.addColumn("QUANTITY", new Integer(0));
+        ColumnMap idTableId = new ColumnMap("QUANTITY", tMap);
+        idTableId.setType(Integer.valueOf(0));
+        idTableId.setPrimaryKey(true);
+        tMap.addColumn(idTableId);
+        ColumnMap tableName = new ColumnMap("TABLE_NAME", tMap);
+        tableName.setType("");
+        tMap.addColumn(tableName);
+        ColumnMap nextId = new ColumnMap("NEXT_ID", tMap);
+        nextId.setType(Integer.valueOf(0));
+        tMap.addColumn(nextId);
+        ColumnMap quantity = new ColumnMap("QUANTITY", tMap);
+        quantity.setType(Integer.valueOf(0));
+        tMap.addColumn(quantity);
         idBroker = new IDBroker(idTable);
         addIdGenerator(IDMethod.ID_BROKER, idBroker);
         return true;

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ColumnMap.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ColumnMap.java?rev=1148345&r1=1148344&r2=1148345&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ColumnMap.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ColumnMap.java Tue Jul 19 14:13:58 2011
@@ -61,12 +61,6 @@ public class ColumnMap implements java.i
     /** Is null value allowed ? */
     private boolean notNull = false;
 
-    /** Name of the table that this column is related to. */
-    private String relatedTableName = "";
-
-    /** Name of the column that this column is related to. */
-    private String relatedColumnName = "";
-
     /** The TableMap for this column. */
     private TableMap table;
 
@@ -230,48 +224,6 @@ public class ColumnMap implements java.i
         this.notNull = nn;
     }
 
-    /**
-     * Set the foreign key for this column.
-     *
-     * @param fullyQualifiedName The name of the table.column that is
-     * foreign.
-     */
-    public void setForeignKey(String fullyQualifiedName)
-    {
-        if (fullyQualifiedName != null && fullyQualifiedName.length() > 0)
-        {
-            relatedTableName = fullyQualifiedName.substring(
-                    0, fullyQualifiedName.indexOf('.'));
-            relatedColumnName = fullyQualifiedName.substring(
-                    fullyQualifiedName.indexOf('.') + 1);
-        }
-        else
-        {
-            relatedTableName = "";
-            relatedColumnName = "";
-        }
-    }
-
-    /**
-     * Set the foreign key for this column.
-     *
-     * @param tableName The name of the table that is foreign.
-     * @param columnName The name of the column that is foreign.
-     */
-    public void setForeignKey(String tableName, String columnName)
-    {
-        if (tableName != null && tableName.length() > 0 && columnName != null
-                && columnName.length() > 0)
-        {
-            relatedTableName = tableName;
-            relatedColumnName = normalizeName(columnName);
-        }
-        else
-        {
-            relatedTableName = "";
-            relatedColumnName = "";
-        }
-    }
 
     /**
      * Get the type of this column. Note that if usePrimitive is true, this may
@@ -333,46 +285,6 @@ public class ColumnMap implements java.i
     }
 
     /**
-     * Is this column a foreign key?
-     *
-     * @return True if column is a foreign key.
-     */
-    public boolean isForeignKey()
-    {
-        return (relatedTableName != null && relatedTableName.length() > 0);
-    }
-
-    /**
-     * Get the table.column that this column is related to.
-     *
-     * @return A String with the full name for the related column.
-     */
-    public String getRelatedName()
-    {
-        return relatedTableName + "." + relatedColumnName;
-    }
-
-    /**
-     * Get the table name that this column is related to.
-     *
-     * @return A String with the name for the related table.
-     */
-    public String getRelatedTableName()
-    {
-        return relatedTableName;
-    }
-
-    /**
-     * Get the column name that this column is related to.
-     *
-     * @return A String with the name for the related column.
-     */
-    public String getRelatedColumnName()
-    {
-        return relatedColumnName;
-    }
-
-    /**
      * Gets the scale set for this column (if any) as set in the XML database
      * definition. E.g., the value of the scale attribute or the scale portion
      * of a size="P,S" attribute. (Note: size="P,S" format is being

Added: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ForeignKeyMap.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ForeignKeyMap.java?rev=1148345&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ForeignKeyMap.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ForeignKeyMap.java Tue Jul 19 14:13:58 2011
@@ -0,0 +1,313 @@
+package org.apache.torque.map;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * ForeignKeyMap is used to model a foreign key in a database.
+ *
+ * @version $Id: ColumnMap.java 1003834 2010-10-02 16:51:32Z tfischer $
+ */
+public class ForeignKeyMap implements Serializable
+{
+    /** The serialVersionUID for this class. */
+    private static final long serialVersionUID = -5971184507395399165L;
+
+    /** The table to which the foreign key belongs, not null. */
+    private TableMap table;
+
+    /** 
+     * The table which is referenced by the foreign key, can be null if
+     * the foreign table is not yet in the database map.
+     */
+    private TableMap foreignTable;
+
+    /** 
+     * The name of the foreign table, in the case that the foreign table
+     * map builder was not yet built when this foreign key was initialized.
+     */
+    private String foreignTableName;
+
+    /** The column pairs for the foreign key, not null, not empty, */
+    private List<ColumnPair> columns = new ArrayList<ColumnPair>();
+
+    /**
+     * Constructor.
+     *
+     * @param table the local table, not null.
+     * @param foreignTable the foreign table, not null.
+     *
+     * @throws NullPointerException if an argument is null.
+     */
+    public ForeignKeyMap(TableMap table, TableMap foreignTable)
+    {
+        if (table == null)
+        {
+            throw new NullPointerException("table is null");
+        }
+        if (foreignTable == null)
+        {
+            throw new NullPointerException("foreignTable is null");
+        }
+        this.table = table;
+        this.foreignTable = foreignTable;
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param table the local table, not null.
+     * @param foreignTableName the name of the foreign table, not null.
+     *
+     * @throws NullPointerException if an argument is null.
+     */
+    public ForeignKeyMap(TableMap table, String foreignTableName)
+    {
+        if (table == null)
+        {
+            throw new NullPointerException("table is null");
+        }
+        if (foreignTableName == null)
+        {
+            throw new NullPointerException("foreignTableName is null");
+        }
+        this.table = table;
+        this.foreignTableName = foreignTableName;
+    }
+
+    /**
+     * Adds a column pair to the foreign key.
+     * 
+     * @param columnPair the column pair to add, not null.
+     */
+    public void addColumns(ColumnPair columnPair)
+    {
+        if (columnPair == null)
+        {
+            throw new NullPointerException("columnPair is null");
+        }
+        columns.add(columnPair);
+    }
+
+    /**
+     * Returns the local table of the foreign key.
+     * 
+     * @return the referencing table, not null.
+     */
+    public TableMap getTable()
+    {
+        return table;
+    }
+
+    /**
+     * Returns the foreign table of the foreign key.
+     *
+     * @return the referenced table, not null.
+     * 
+     * @throws IllegalStateException if the foreign table map builder
+     *         was not yet built.
+     */
+    public TableMap getForeignTable()
+    {
+        if (foreignTable == null)
+        {
+            foreignTable = table.getDatabaseMap().getTable(foreignTableName);
+            if (foreignTable == null)
+            {
+                throw new IllegalStateException(
+                    "Map builder for " 
+                        + foreignTableName
+                        + " was not yet built.");
+            }
+            foreignTableName = null;
+        }
+        return foreignTable;
+    }
+
+    /**
+     * Returns name of the foreign table of the foreign key.
+     *
+     * @return the name of the referenced table, not null.
+     */
+    public String getForeignTableName()
+    {
+        if (foreignTable == null)
+        {
+            return foreignTableName;
+        }
+        return foreignTable.getName();
+    }
+    
+
+    /**
+     * Returns the column pairs.
+     *
+     * @return the column pairs, not null, as unmodifiable list.
+     */
+    public List<ColumnPair> getColumns()
+    {
+        return Collections.unmodifiableList(columns);
+    }
+
+    /**
+     * A pair of local and Foreign column.
+     * This class is immutable.
+     */
+    public static class ColumnPair 
+    {
+        /**
+         * The foreign key map this ColumnPair belongs to.
+         */
+        private ForeignKeyMap foreignKeyMap;
+
+        /** 
+         * The foreign column of the pair, not null. * 
+         */
+        private ColumnMap local;
+
+        /** 
+         * The foreign column of the pair, can be null if the 
+         * foreign table was not yet in the database.
+         */
+        private ColumnMap foreign;
+
+        /** 
+         * The name of foreign column of the pair,
+         * or null if the foreign column is already filled.
+         */
+        private String foreignName;
+
+        /**
+         * Constructor.
+         *
+         * @param foreignKeyMap the foreign key map this columnPait belongs to.
+         * @param local the local column, not null.
+         * @param foreign the foreign column, not null.
+         *
+         * @throws NullPointerException if local or doreign are null.
+         */
+        public ColumnPair(
+                ForeignKeyMap foreignKeyMap,
+                ColumnMap local,
+                ColumnMap foreign)
+        {
+            if (foreignKeyMap == null)
+            {
+                throw new NullPointerException("foreignKeyMap is null");
+            }
+            if (local == null)
+            {
+                throw new NullPointerException("local is null");
+            }
+            if (foreign == null)
+            {
+                throw new NullPointerException("foreign is null");
+            }
+            this.foreignKeyMap = foreignKeyMap;
+            this.local = local;
+            this.foreign = foreign;
+        }
+
+        /**
+         * Constructor.
+         *
+         * @param foreignKeyMap the foreign key map this columnPait belongs to.
+         * @param local the local column, not null.
+         * @param foreign the foreign column, not null.
+         *
+         * @throws NullPointerException if local or doreign are null.
+         */
+        public ColumnPair(
+                ForeignKeyMap foreignKeyMap,
+                ColumnMap local,
+                String foreignName)
+        {
+            if (foreignKeyMap == null)
+            {
+                throw new NullPointerException("foreignKeyMap is null");
+            }
+            if (local == null)
+            {
+                throw new NullPointerException("local is null");
+            }
+            if (foreignName == null)
+            {
+                throw new NullPointerException("foreignName is null");
+            }
+            this.foreignKeyMap = foreignKeyMap;
+            this.local = local;
+            this.foreignName = foreignName;
+        }
+
+        /** 
+         * Returns the associated foreign key map.
+         * 
+         * @return the associated foreign key map, not null.
+         */
+        public ForeignKeyMap getForeignKeyMap()
+        {
+            return foreignKeyMap;
+        }
+
+        /** 
+         * Returns the local column of the pair.
+         * 
+         * @return the local column of the pair, not null.
+         */
+        public ColumnMap getLocal()
+        {
+            return local;
+        }
+
+        /** 
+         * Returns the foreign column of the pair.
+         * 
+         * @return the foreign column of the pair, not null.
+         * 
+         * @throws IllegalStateException if the foreign table map builder
+         *         was not yet built.
+         */
+        public ColumnMap getForeign()
+        {
+            if (foreign == null)
+            {
+                TableMap foreignTable =  foreignKeyMap.getForeignTable();
+                if (foreignTable == null)
+                {
+                    throw new IllegalStateException(
+                            "Table " + foreignKeyMap.getForeignTableName()
+                                + " is not yet initialized");
+                }
+                foreign = foreignTable.getColumn(foreignName);
+                if (foreign == null)
+                {
+                    throw new IllegalStateException(
+                            "Table " + foreignKeyMap.getForeignTableName()
+                                + " has no column named " + foreignName);
+                }
+            }
+            return foreign;
+        }
+    }
+}

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java?rev=1148345&r1=1148344&r2=1148345&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java Tue Jul 19 14:13:58 2011
@@ -19,10 +19,12 @@ package org.apache.torque.map;
  * under the License.
  */
 
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.StringTokenizer;
@@ -55,6 +57,9 @@ public class TableMap implements IDMetho
             = Collections.synchronizedMap(
                 new LinkedHashMap<String, ColumnMap>());
 
+    /** The foreign keys in the table. XML Order is preserved. */
+    private List<ForeignKeyMap> foreignKeys = new ArrayList<ForeignKeyMap>();
+
     /** The database this table belongs to. */
     private DatabaseMap dbMap;
 
@@ -302,6 +307,16 @@ public class TableMap implements IDMetho
     }
 
     /**
+     * Get all foreign keys in the table..
+     *
+     * @return All foreign keys, not null.
+     */
+    public List<ForeignKeyMap> getForeignKeys()
+    {
+        return Collections.unmodifiableList(foreignKeys);
+    }
+
+    /**
      * Get a ColumnMap for the named table.
      *
      * @param name A String with the name of the table.
@@ -331,226 +346,13 @@ public class TableMap implements IDMetho
     }
 
     /**
-     * Add a column to this table of a certain type.
-     *
-     * @param columnName A String with the column name.
-     * @param type An Object specifying the type.
-     * @deprecated Associated Column maps should be populated using it's
-     *             set methods, then added to table via addColumn(ColumnMap).
-     *             This method will be removed in a future version of Torque.
-     */
-    public void addColumn(String columnName, Object type)
-    {
-        addColumn(columnName, type, false, null, null, 0);
-    }
-
-    /**
-     * Add a column to this table of a certain type, size, and scale.
-     *
-     * @param columnName A String with the column name.
-     * @param type An Object specifying the type.
-     * @param size An int specifying the size.
-     * @param scale An int specifying the scale.
-     * @deprecated Associated Column maps should be populated using it's set
-     *             methods, then added to table via addColumn(ColumnMap).
-     *             This method will be removed in a future version of Torque.
-     */
-    public void addColumn(String columnName, Object type, int size, int scale)
-    {
-        addColumn(columnName, type, false, null, null, size, scale);
-    }
-
-    /**
-     * Add a column to this table of a certain type and size.
-     *
-     * @param columnName A String with the column name.
-     * @param type An Object specifying the type.
-     * @param size An int specifying the size.
-     * @deprecated Associated Column maps should be populated using it's set
-     *             methods, then added to table via addColumn(ColumnMap).
-     *             This method will be removed in a future version of Torque.
-     */
-    public void addColumn(String columnName, Object type, int size)
-    {
-        addColumn(columnName, type, false, null, null, size);
-    }
-
-    /**
-     * Add a primary key column to this Table.
-     *
-     * @param columnName A String with the column name.
-     * @param type An Object specifying the type.
-     * @deprecated Associated Column maps should be populated using it's set
-     *             methods, then added to table via addColumn(ColumnMap).
-     *             This method will be removed in a future version of Torque.
-     */
-    public void addPrimaryKey(String columnName, Object type)
-    {
-        addColumn(columnName, type, true, null, null, 0);
-    }
-
-    /**
-     * Add a primary key column to this Table.
-     *
-     * @param columnName A String with the column name.
-     * @param type An Object specifying the type.
-     * @param size An int specifying the size.
-     * @deprecated Associated Column maps should be populated using it's set
-     *             methods, then added to table via addColumn(ColumnMap).
-     *             This method will be removed in a future version of Torque.
-     */
-    public void addPrimaryKey(String columnName, Object type, int size)
-    {
-        addColumn(columnName, type, true, null, null, size);
-    }
-
-    /**
-     * Add a foreign key column to the table.
+     * Add a foreign key to this table.
      *
-     * @param columnName A String with the column name.
-     * @param type An Object specifying the type.
-     * @param fkTable A String with the foreign key table name.
-     * @param fkColumn A String with the foreign key column name.
-     * @deprecated Associated Column maps should be populated using it's set
-     *             methods, then added to table via addColumn(ColumnMap).
-     *             This method will be removed in a future version of Torque.
+     * @param foreignKey the foreign key map, not null
      */
-    public void addForeignKey(String columnName,
-                              Object type,
-                              String fkTable,
-                              String fkColumn)
+    public void addForeignKey(ForeignKeyMap fk)
     {
-        addColumn(columnName, type, false, fkTable, fkColumn, 0);
-    }
-
-    /**
-     * Add a foreign key column to the table.
-     *
-     * @param columnName A String with the column name.
-     * @param type An Object specifying the type.
-     * @param fkTable A String with the foreign key table name.
-     * @param fkColumn A String with the foreign key column name.
-     * @param size An int specifying the size.
-     * @deprecated Associated Column maps should be populated using it's set
-     *             methods, then added to table via addColumn(ColumnMap).
-     *             This method will be removed in a future version of Torque.
-     */
-    public void addForeignKey(String columnName,
-                              Object type,
-                              String fkTable,
-                               String fkColumn,
-                               int size)
-    {
-        addColumn(columnName, type, false, fkTable, fkColumn, size);
-    }
-
-    /**
-     * Add a foreign primary key column to the table.
-     *
-     * @param columnName A String with the column name.
-     * @param type An Object specifying the type.
-     * @param fkTable A String with the foreign key table name.
-     * @param fkColumn A String with the foreign key column name.
-     * @deprecated Associated Column maps should be populated using it's set
-     *             methods, then added to table via addColumn(ColumnMap).
-     *             This method will be removed in a future version of Torque.
-     */
-    public void addForeignPrimaryKey(String columnName,
-                                     Object type,
-                                     String fkTable,
-                                     String fkColumn)
-    {
-        addColumn(columnName, type, true, fkTable, fkColumn, 0);
-    }
-
-    /**
-     * Add a foreign primary key column to the table.
-     *
-     * @param columnName A String with the column name.
-     * @param type An Object specifying the type.
-     * @param fkTable A String with the foreign key table name.
-     * @param fkColumn A String with the foreign key column name.
-     * @param size An int specifying the size.
-     * @deprecated Associated Column maps should be populated using it's set
-     *             methods, then added to table via addColumn(ColumnMap).
-     *             This method will be removed in a future version of Torque.
-     */
-    public void addForeignPrimaryKey(String columnName,
-                                     Object type,
-                                     String fkTable,
-                                     String fkColumn,
-                                     int size)
-    {
-        addColumn(columnName, type, true, fkTable, fkColumn, size);
-    }
-
-    /**
-     * Add a column to the table.
-     *
-     * @param name A String with the column name.
-     * @param type An Object specifying the type.
-     * @param pk True if column is a primary key.
-     * @param fkTable A String with the foreign key table name.
-     * @param fkColumn A String with the foreign key column name.
-     * @param size An int specifying the size.
-     * @deprecated Associated Column maps should be populated using it's set
-     *             methods, then added to table via addColumn(ColumnMap).
-     *             This method will be removed in a future version of Torque.
-     */
-    private void addColumn(String name,
-                           Object type,
-                           boolean pk,
-                           String fkTable,
-                           String fkColumn,
-                           int size)
-    {
-        addColumn(name, type, pk, fkTable, fkColumn, size, 0);
-    }
-
-    /**
-     * Add a column to the table.
-     *
-     * @param name A String with the column name.
-     * @param type An Object specifying the type.
-     * @param pk True if column is a primary key.
-     * @param fkTable A String with the foreign key table name.
-     * @param fkColumn A String with the foreign key column name.
-     * @param size An int specifying the size.
-     * @param scale An int specifying the scale.
-     * @deprecated Associated Column maps should be populated using it's set
-     *             methods, then added to table via addColumn(ColumnMap).
-     *             This method will be removed in a future version of Torque.
-     */
-    private void addColumn(String name,
-                           Object type,
-                           boolean pk,
-                           String fkTable,
-                           String fkColumn,
-                           int size,
-                           int scale)
-    {
-        // If the tablename is prefixed with the name of the column,
-        // remove it ie: SCARAB_PROJECT.PROJECT_ID remove the
-        // SCARAB_PROJECT.
-        if (name.indexOf('.') > 0 && name.indexOf(getName()) != -1)
-        {
-            name = name.substring(getName().length() + 1);
-        }
-        if (fkTable != null && fkTable.length() > 0 && fkColumn != null
-                && fkColumn.length() > 0)
-        {
-            if (fkColumn.indexOf('.') > 0 && fkColumn.indexOf(fkTable) != -1)
-            {
-                fkColumn = fkColumn.substring(fkTable.length() + 1);
-            }
-        }
-        ColumnMap col = new ColumnMap(name, this);
-        col.setType(type);
-        col.setPrimaryKey(pk);
-        col.setForeignKey(fkTable, fkColumn);
-        col.setSize(size);
-        col.setScale(scale);
-        columns.put(name, col);
+        foreignKeys.add(fk);
     }
 
     /**

Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java?rev=1148345&r1=1148344&r2=1148345&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java Tue Jul 19 14:13:58 2011
@@ -9,6 +9,7 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.torque.adapter.DB;
 import org.apache.torque.dsfactory.DataSourceFactory;
+import org.apache.torque.map.ColumnMap;
 import org.apache.torque.map.DatabaseMap;
 import org.apache.torque.map.MapBuilder;
 import org.apache.torque.map.TableMap;
@@ -225,8 +226,14 @@ public class TorqueInstanceTest extends 
 
             tMap.setPrimaryKeyMethodInfo(tableName);
 
-            tMap.addPrimaryKey(tableName + "ID", new Integer(0));
-            tMap.addColumn(tableName + "NAME", "", 50 );
+            ColumnMap idColumn = new ColumnMap(tableName + "ID", tMap);
+            idColumn.setType(new Integer(0));
+            idColumn.setPrimaryKey(true);
+            tMap.addColumn(idColumn);
+            ColumnMap nameColumn = new ColumnMap(tableName + "ID", tMap);
+            nameColumn.setType("");
+            nameColumn.setSize(50);
+            tMap.addColumn(nameColumn);
         }
     }
 

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/doBuild.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/doBuild.vm?rev=1148345&r1=1148344&r2=1148345&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/doBuild.vm (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/doBuild.vm Tue Jul 19 14:13:58 2011
@@ -118,7 +118,6 @@
   #set ( $inheritance = $columnElement.getAttribute("inheritance") )
   #set ( $size = $columnElement.getAttribute("size") )
   #set ( $scale = $columnElement.getAttribute("scale") )
-  #set ( $referencedColumnElements = $columnElement.getChildren("referenced-column"))
     // ------------- Column: $columnName --------------------
         cMap = new ColumnMap("$columnName", tMap);
         cMap.setType($sampleObject);
@@ -147,12 +146,6 @@
         cMap.setScale($scale);
     #end
   #end
-  #if (!$referencedColumnElements.isEmpty())
-    #set ( $referencedColumnElement = $referencedColumnElements.get(0).getChild("column"))
-        cMap.setForeignKey(
-            "$referencedColumnElement.getParent().getAttribute("name")",
-            "$referencedColumnElement.getAttribute("name")");
-  #end
   #set ( $columnOptionElements = $columnElement.getChildren("option") )
   #foreach ( $optionElement in $columnOptionElements )
     #set ( $key = $optionElement.getAttribute("key") )
@@ -177,4 +170,43 @@
         tMap.addColumn(cMap);
 #end
         tMap.setUseInheritance($tableUseInheritance);
+#set ( $foreignKeyElements = $tableElement.getChildren("foreign-key") )
+#if (!$foreignKeyElements.isEmpty())
+
+    // ------------- Foreign keys --------------------
+        ForeignKeyMap foreignKeyMap;
+        TableMap foreignTable;
+        String foreignTableName; 
+  #foreach ($foreignKeyElement in $foreignKeyElements)
+    #set($foreignTableName = $foreignKeyElement.getAttribute("foreignTable"))
+        foreignTableName = "${foreignTableName}"; 
+        foreignTable = dbMap.getTable(foreignTableName);
+        if (foreignTable == null)
+        {
+            foreignKeyMap = new ForeignKeyMap(tMap, foreignTableName);
+    #set ( $referenceElements = $foreignKeyElement.getChildren("reference") )
+    #foreach ($referenceElement in $referenceElements)
+      #set($localColumn = $referenceElement.getAttribute("local"))
+      #set($foreignColumn = $referenceElement.getAttribute("foreign"))
+            foreignKeyMap.addColumns(new ForeignKeyMap.ColumnPair(
+                foreignKeyMap,
+                tMap.getColumn("${localColumn}"),
+                "${foreignColumn}"));
+    #end
+        }
+        else
+        {
+            foreignKeyMap = new ForeignKeyMap(tMap, foreignTable);
+    #foreach ($referenceElement in $referenceElements)
+      #set($localColumn = $referenceElement.getAttribute("local"))
+      #set($foreignColumn = $referenceElement.getAttribute("foreign"))
+            foreignKeyMap.addColumns(new ForeignKeyMap.ColumnPair(
+                foreignKeyMap,
+                tMap.getColumn("${localColumn}"),
+                foreignTable.getColumn("${foreignColumn}")));
+    #end
+        }
+        tMap.addForeignKey(foreignKeyMap);
+  #end
+#end
     }

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/imports.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/imports.vm?rev=1148345&r1=1148344&r2=1148345&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/imports.vm (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/imports.vm Tue Jul 19 14:13:58 2011
@@ -28,6 +28,8 @@
 ## as velocity variables.  
 ##
 import java.util.Date;
+import java.util.List;
+import java.util.ArrayList;
 
 import org.apache.torque.Torque;
 import org.apache.torque.TorqueException;
@@ -38,5 +40,6 @@ import org.apache.torque.map.MapBuilder;
 import org.apache.torque.map.DatabaseMap;
 import org.apache.torque.map.TableMap;
 import org.apache.torque.map.ColumnMap;
+import org.apache.torque.map.ForeignKeyMap;
 import org.apache.torque.map.InheritanceMap;
 



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org