I just used the Torque Gen 3.1 to reverse engineer a complex database and found a some of inconsistencies when  the "complexObjectModel" is used.

variable names for the associated collections where inconsistently derived/generated. I fixed it in my installation and have attached a fixed Object.vm.

Has this been reported and is there a better one out there ?

Jerry H
package ${package};

#if ($table.BaseClass == "")
  #set ($extendsBaseClass = "" )
#else
  #set ($extendsBaseClass = "extends $table.BaseClass" )
#end

import java.math.BigDecimal;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.Collections;
import java.util.List;

#if ($addSaveMethod)
import org.apache.commons.lang.ObjectUtils;
#end
#if ($addIntakeRetrievable)
import $retrievableInterface;
#end
#if (!$complexObjectModel)
import org.apache.torque.Torque;
#end
import org.apache.torque.TorqueException;
import org.apache.torque.om.BaseObject;
import org.apache.torque.om.ComboKey;
import org.apache.torque.om.DateKey;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.ObjectKey;
import org.apache.torque.om.SimpleKey;
import org.apache.torque.om.StringKey;
import org.apache.torque.om.Persistent;
import org.apache.torque.util.Criteria;
import org.apache.torque.util.Transaction;

#foreach ($fk in $table.ForeignKeys)
  #set ( $tblFK = $table.Database.getTable($fk.ForeignTableName) )
  #set ( $className = $tblFK.JavaName )
  #if ($tblFK.Interface)
    #set ($className = $tblFK.Interface)
  #end

  #if ($tblFK.Package != $package)
import ${tblFK.Package}.${className};
import ${tblFK.Package}.${tblFK.JavaName}Peer;
  #end
#end

/**
#if ($addTimeStamp)
 * This class was autogenerated by Torque on:
 *
 * [$now]
 *
#end
 * You should not use this class directly.  It should not even be
 * extended all references should be to $table.JavaName
 */
public abstract class $basePrefix$table.JavaName $extendsBaseClass
#if ($addIntakeRetrievable)
    implements $retrievableInterface
#end
{
    /** The Peer class */
    private static final ${table.JavaName}Peer peer =
        new ${table.JavaName}Peer();

#if (!$table.isAlias())
  ## member variables
  #foreach ($col in $table.Columns)
    #set ( $cjtype = $col.JavaNative )
    #set ( $clo=$col.UncapitalisedJavaName )
    #set ($defVal = "")
    #if ($col.DefaultValue && !$col.DefaultValue.equalsIgnoreCase("NULL") )
      #set ( $quote = '' )
      #if ( $cjtype == "String" )
        #set ( $quote = '"' )
      #end
      #set ( $defaultValue = $col.DefaultValue )
      #if ( $cjtype == "boolean" || $cjtype == "Boolean" )
        #if ( $defaultValue == "1" || $defaultValue == "Y" )
          #set ( $defaultValue = "true" )
        #elseif ( $defaultValue == "0" || $defaultValue == "N" )
          #set ( $defaultValue = "false" )
        #end
      #end
      #if ($cjtype == "BigDecimal")
        #set ($defVal = "= new BigDecimal($defaultValue)")
      #elseif ($cjtype == "NumberKey")
        #set ( $quote = '"' )
        #set ($defVal = "= new NumberKey($quote$defaultValue$quote)")
      #elseif ($cjtype == "StringKey")
        #set ( $quote = '"' )
        #set ($defVal = "= new StringKey($quote$defaultValue$quote)")
      #else
        #if (!$col.isPrimitive() && $cjtype != "String")
          #set ( $defaultValue = "new ${cjtype}($defaultValue)" )
        #end
        #set ($defVal = " = $quote$defaultValue$quote")
      #end
    #end

    /** The value for the $clo field */
    private $cjtype $clo$defVal;
  #end
// batman is good

  ## getter and setter methods
  #foreach ($col in $table.Columns)
    #set ( $cfc=$col.JavaName )
    #set ( $clo=$col.UncapitalisedJavaName )
    #set ( $cjtype = $col.JavaNative )

    /**
     * Get the $cfc
     *
     * @return $cjtype
     */
    public $cjtype get${cfc}()
    {
        return $clo;
    }

    #set ( $throwsClause = "" )
    #if ($complexObjectModel)
      #if ($col.isForeignKey())
        #set ( $throwsClause = "throws TorqueException" )
      #end
      #if ( $col.Referrers.size() > 0 )
        #if ($throwsClause == "")
          #set ( $throwsClause = "throws TorqueException" )
        #end
      #end
    #end

    /**
     * Set the value of $cfc
     *
     * @param v new value
     */
    public void set${cfc}($cjtype v) $throwsClause
    {
    #if (($cjtype == "NumberKey") || ($cjtype == "StringKey") || ($cjtype == 
"DateKey"))
        if (v != null && v.getValue() == null)
        {
            // If this is an Objectkey than this set method is
            // probably storing the id of this object or some
            // associated object.  If the objectKey value is null
            // then we convert the parameter to null so that this
            // property is consistently null to indicate that no
            // object is associated or defined.
            v = null;
        }
    #end

    #if ($addSaveMethod)
      #if ($col.isPrimitive())
        if (this.$clo != v)
      #else
        if (!ObjectUtils.equals(this.$clo, v))
      #end
        {
            this.$clo = v;
            setModified(true);
        }
    #else
        this.$clo = v;
    #end

    #if ($complexObjectModel)
      #if ($col.isForeignKey())
        #set ( $tblFK = $table.Database.getTable($col.RelatedTableName) )
        #set ( $colFK = $tblFK.getColumn($col.RelatedColumnName) )
        #set ( $relCol = "" )
        #foreach ($columnName in $col.foreignKey.LocalColumns)
          #set ( $column = $table.getColumn($columnName) )
          #if ($column.isMultipleFK() )
            #set ( $relCol = "$relCol$column.JavaName" )
          #end
        #end
        #if ($relCol != "")
          #set ( $relCol = "RelatedBy$relCol" )
          #set ( $varName = "a${tblFK.JavaName}$relCol" )
        #else
          #set ( $varName = "a$tblFK.JavaName" )
        #end
        #if($colFK.Primitive)
        if ($varName != null && !(${varName}.get${colFK.JavaName}() == v))
        #else
        if ($varName != null && !ObjectUtils.equals(${varName}.get${colFK.JavaName}(), 
v))
        #end
        {
            $varName = null;
        }
      #end

      #foreach ($fk in $col.Referrers)
        #set ( $fkColName = $fk.ForeignLocalMapping.get($col.Name) )
        #set ( $tblFK = $fk.Table )
        #if ( !($tblFK.Name.equals($table.Name)) )
        #set ( $colFK = $tblFK.getColumn($fkColName) )
          
       #set ( $relatedByCol = "" )

        #foreach ($columnName in $fk.LocalColumns)
        #set ( $column = $tblFK.getColumn($columnName) )
        #if ($column.isMultipleFK())
          #set ($relatedByCol= "$relatedByCol$column.JavaName")
        #end
      #end

        #if ($relatedByCol == "")
          #set ( $relCol = "${tblFK.JavaName}s" )
        #else
           #set ( $relCol= "${tblFK.JavaName}sRelatedBy$relatedByCol" )
        #end
        #set ( $collName = "coll$relCol" )

        // update associated $tblFK.JavaName 
        if ($collName != null)
        {
            for (int i = 0; i < ${collName}.size(); i++)
            {
                ((${tblFK.JavaName}) ${collName}.get(i))
                    .set${colFK.JavaName}(v);
            }
        }
        #end
      #end
    #end
    }
  #end
#end

##association code
#if ($complexObjectModel)
  #set($pVars = [])  ## Array of object set method names for later reference.
  #set($aVars = [])  ## Array of object field names for later reference.
  #foreach ($fk in $table.ForeignKeys)

    #set ( $tblFK = $table.Database.getTable($fk.ForeignTableName) )
    #set ( $className = $tblFK.JavaName )

    #set ( $relCol = "" )
    #foreach ($columnName in $fk.LocalColumns)
      #set ( $column = $table.getColumn($columnName) )
      #if ($column.isMultipleFK())
        #set ( $relCol = "$relCol$column.JavaName" )
      #end
    #end

    #if ($relCol != "")
      #set ( $relCol = "RelatedBy$relCol" )
    #end

    #set ( $pVarName = "$className$relCol" )
    #set ( $varName = "a$pVarName" )
    #set ( $retVal = $pVars.add($pVarName) )
    #set ( $retVal = $aVars.add($varName) )
    private $className $varName;

    /**
     * Declares an association between this object and a $className object
     *
     * @param v $className
     * @throws TorqueException
     */
    public void set${pVarName}($className v) throws TorqueException
    {
    #foreach ($columnName in $fk.LocalColumns)
      #set ( $column = $table.getColumn($columnName) )
      #set ( $colFKName = $fk.LocalForeignMapping.get($columnName) )
      #set ( $colFK = $tblFK.getColumn($colFKName) )
        if (v == null)
        {
        #if($colFK.Primitive)
          #set ($coldefval = "0")
          #set ($coldefval = $column.DefaultValue)
            set${column.JavaName}($coldefval);
          #else
            set${column.JavaName}(($column.JavaNative) null);
          #end
        }
        else
        {
            set${column.JavaName}(v.get${colFK.JavaName}());
        }
        #end
        $varName = v;
    }

        #set ( $and = "" )
        #set ( $comma = "" )
        #set ( $conditional = "" )
        #set ( $arglist = "" )
        #set ( $argsize = 0 )
        #foreach ($columnName in $fk.LocalColumns)
          #set ( $column = $table.getColumn($columnName) )
          #set ( $cjtype = $column.JavaNative )
          #set ( $clo=$column.UncapitalisedJavaName )
          #if ($cjtype == "short" || $cjtype == "int" || $cjtype == "long" || $cjtype 
== "byte" || $cjtype == "float" || $cjtype == "double")
            #set ( $conditional = "$conditional${and}this.${clo} > 0" )
          #else
            #set ( $conditional = "$conditional${and}!ObjectUtils.equals(this.${clo}, 
null)" )
          #end
          #set ( $arglist = "$arglist${comma}this.$clo" )
          #set ( $and = " && " )
          #set ( $comma = ", " )
          #set ( $argsize = $argsize + 1 )
        #end
      #set ( $pCollName = "${table.JavaName}s$relCol" )

    /**
     * Get the associated $className object
     *
     * @return the associated $className object
     * @throws TorqueException
     */
    public $className get${pVarName}() throws TorqueException
    {
        if ($varName == null && ($conditional))
        {
      #if ($tblFK.isAlias())
        #if ($argsize > 1)
            $varName = ${className}Peer.retrieve${className}ByPK($arglist);
        #else
            $varName = 
${className}Peer.retrieve${className}ByPK(SimpleKey.keyFor($arglist));
        #end
      #else
        #if ($argsize > 1)
            $varName = ${className}Peer.retrieveByPK($arglist);
        #else
            $varName = ${className}Peer.retrieveByPK(SimpleKey.keyFor($arglist));
        #end
      #end

            /* The following can be used instead of the line above to
               guarantee the related object contains a reference
               to this object, but this level of coupling
               may be undesirable in many circumstances.
               As it can lead to a db query with many results that may
               never be used.
               $className obj = ${className}Peer.retrieveByPK($arglist);
               obj.add${pCollName}(this);
            */
        }
        return $varName;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey.  e.g.
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
      #if ($fk.LocalColumns.size() > 1)
     * Note: It is important that the xml schema used to create this class
     * maintains consistency in the order of related columns between
     * $table.Name and ${tblFK.Name}.
     * If for some reason this is impossible, this method should be
     * overridden in <code>$table.JavaName</code>.
      #end
     */
    public void set${pVarName}Key(ObjectKey key) throws TorqueException
    {
      #if ($fk.LocalColumns.size() > 1)
        SimpleKey[] keys = (SimpleKey[]) key.getValue();
        #set ($i = 0)
        #foreach ($colName in $fk.LocalColumns)
          #set ($col = $table.getColumn($colName) )

          #set ($fktype = $col.JavaNative)
          #if ($fktype == "short")
        set${col.JavaName}(((NumberKey) keys[$i]).shortValue());
          #elseif($fktype == "int")
        set${col.JavaName}(((NumberKey) keys[$i]).intValue());
          #elseif($fktype == "long")
        set${col.JavaName}(((NumberKey) keys[$i]).longValue());
          #elseif($fktype == "BigDecimal")
        set${col.JavaName}(((NumberKey) keys[$i]).getBigDecimal());
          #elseif($fktype == "byte") )
        set${col.JavaName}(((NumberKey) keys[$i]).byteValue());
          #elseif($fktype == "float")
        set${col.JavaName}(((NumberKey) keys[$i]).floatValue());
          #elseif($fktype == "double")
        set${col.JavaName}(((NumberKey) keys[$i]).doubleValue());
          #elseif($fktype == "Short")
        set${col.JavaName}(new Short(((NumberKey) keys[$i]).shortValue()));
          #elseif($fktype == "Integer")
        set${col.JavaName}(new Integer(((NumberKey) keys[$i]).intValue()));
          #elseif($fktype == "Long")
        set${col.JavaName}(new Long(((NumberKey) keys[$i]).longValue()));
          #elseif($fktype == "Byte") )
        set${col.JavaName}(new Byte(((NumberKey) keys[$i]).byteValue()));
          #elseif($fktype == "Float")
        set${col.JavaName}(new Float(((NumberKey) keys[$i]).floatValue()));
          #elseif($fktype == "Double")
        set${col.JavaName}(new Double(((NumberKey) keys[$i]).doubleValue()));
          #elseif($fktype == "String")
        set${col.JavaName}(keys[$i].toString());
          #elseif($fktype == "Date")
        set${col.JavaName}(((DateKey)keys[$i]).getDate());
          #end
          #set ( $i = $i + 1 )
        #end
      #else
        #set ($colName = $fk.LocalColumns.get(0))
        #set ($col = $table.getColumn($colName) )

        #set ($fktype = $col.JavaNative)
        #if ($fktype == "short")
        set${col.JavaName}(((NumberKey) key).shortValue());
        #elseif($fktype == "int")
        set${col.JavaName}(((NumberKey) key).intValue());
        #elseif($fktype == "long")
        set${col.JavaName}(((NumberKey) key).longValue());
        #elseif($fktype == "BigDecimal")
        set${col.JavaName}(((NumberKey) key).getBigDecimal());
        #elseif($fktype == "byte") )
        set${col.JavaName}(((NumberKey) key).byteValue());
        #elseif($fktype == "float")
        set${col.JavaName}(((NumberKey) key).floatValue());
        #elseif($fktype == "double")
        set${col.JavaName}(((NumberKey) key).doubleValue());
        #elseif($fktype == "Short")
        set${col.JavaName}(new Short(((NumberKey) key).shortValue()));
        #elseif($fktype == "Integer")
        set${col.JavaName}(new Integer(((NumberKey) key).intValue()));
        #elseif($fktype == "Long")
        set${col.JavaName}(new Long(((NumberKey) key).longValue()));
        #elseif($fktype == "Byte") )
        set${col.JavaName}(new Byte(((NumberKey) key).byteValue()));
        #elseif($fktype == "Float")
        set${col.JavaName}(new Float(((NumberKey) key).floatValue()));
        #elseif($fktype == "Double")
        set${col.JavaName}(new Double(((NumberKey) key).doubleValue()));
        #elseif($fktype == "String")
        set${col.JavaName}(key.toString());
        #elseif($fktype == "Date")
        set${col.JavaName}(((DateKey)key).getDate());
        #end
      #end
    }
    #end   ## end of foreach loop over foreign keys

  ##
  ## setup foreign key associations
  ##
  #foreach ($fk in $table.Referrers)
    #set ( $tblFK = $fk.Table )
    #if ( !($tblFK.Name.equals($table.Name)) )
      #set ( $className = $tblFK.JavaName )
      #set ( $relatedByCol = "" )
      #foreach ($columnName in $fk.LocalColumns)
        #set ( $column = $tblFK.getColumn($columnName) )
        #if ($column.isMultipleFK())
          #set ($relatedByCol= "$relatedByCol$column.JavaName")
        #end
      #end

      #if ($relatedByCol == "")
        #set ( $suffix = "" )
        #set ( $relCol = "${className}s" )
        #set ( $relColMs = $className )
      #else
        #set ( $suffix = "RelatedBy$relatedByCol" )
        #set ( $relCol= "${className}sRelatedBy$relatedByCol" )
        #set ( $relColMs= "${className}RelatedBy$relatedByCol" )
      #end
      #set ( $collName = "coll$relCol" )

    /**
     * Collection to store aggregation of $collName
     */
    protected List $collName;

    /**
     * Temporary storage of $collName to save a possible db hit in
     * the event objects are add to the collection, but the
     * complete collection is never requested.
     */
    protected void init${relCol}()
    {
        if ($collName == null)
        {
            $collName = new ArrayList();
        }
    }

    /**
     * Method called to associate a $tblFK.JavaName object to this object
     * through the $className foreign key attribute
     *
     * @param l $className
     * @throws TorqueException
     */
    public void add${relColMs}($className l) throws TorqueException
    {
        get${relCol}().add(l);
        l.set${table.JavaName}${suffix}(($table.JavaName) this);
    }

    /**
     * The criteria used to select the current contents of $collName
     */
    private Criteria last${relCol}Criteria = null;

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * get${relCol}(new Criteria())
     *
     * @throws TorqueException
     */
    public List get${relCol}() throws TorqueException
    {
        if ($collName == null)
        {
            $collName = get${relCol}(new Criteria(10));
        }
        return $collName;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this $table.JavaName has previously
     * been saved, it will retrieve related ${relCol} from storage.
     * If this $table.JavaName is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     *
     * @throws TorqueException
     */
    public List get${relCol}(Criteria criteria) throws TorqueException
    {
        if ($collName == null)
        {
            if (isNew())
            {
               $collName = new ArrayList();
            }
            else
            {
      #foreach ($columnName in $fk.ForeignColumns)
        #set ( $column = $table.getColumn($columnName) )
        #set ( $colFKName = $fk.ForeignLocalMapping.get($columnName) )
        #set ( $colFK = $tblFK.getColumn($colFKName) )
                criteria.add(${className}Peer.${colFK.Name.toUpperCase()}, 
get${column.JavaName}() );
      #end
                $collName = ${className}Peer.doSelect(criteria);
            }
        }
        else
        {
            // criteria has no effect for a new object
            if (!isNew())
            {
                // the following code is to determine if a new query is
                // called for.  If the criteria is the same as the last
                // one, just return the collection.
      #foreach ($columnName in $fk.ForeignColumns)
        #set ( $column = $table.getColumn($columnName) )
        #set ( $colFKName = $fk.ForeignLocalMapping.get($columnName) )
        #set ( $colFK = $tblFK.getColumn($colFKName) )
                criteria.add(${className}Peer.${colFK.Name.toUpperCase()}, 
get${column.JavaName}());
      #end
                if (!last${relCol}Criteria.equals(criteria))
                {
                    $collName = ${className}Peer.doSelect(criteria);
                }
            }
        }
        last${relCol}Criteria = criteria;

        return $collName;
    }

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * get${relCol}(new Criteria(),Connection)
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List get${relCol}(Connection con) throws TorqueException
    {
        if ($collName == null)
        {
            $collName = get${relCol}(new Criteria(10), con);
        }
        return $collName;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this $table.JavaName has previously
     * been saved, it will retrieve related ${relCol} from storage.
     * If this $table.JavaName is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List get${relCol}(Criteria criteria, Connection con)
            throws TorqueException
    {
        if ($collName == null)
        {
            if (isNew())
            {
               $collName = new ArrayList();
            }
            else
            {
      #foreach ($columnName in $fk.ForeignColumns)
        #set ( $column = $table.getColumn($columnName) )
        #set ( $colFKName = $fk.ForeignLocalMapping.get($columnName) )
        #set ( $colFK = $tblFK.getColumn($colFKName) )
                 criteria.add(${className}Peer.${colFK.Name.toUpperCase()}, 
get${column.JavaName}());
      #end
                 $collName = ${className}Peer.doSelect(criteria, con);
             }
         }
         else
         {
             // criteria has no effect for a new object
             if (!isNew())
             {
                 // the following code is to determine if a new query is
                 // called for.  If the criteria is the same as the last
                 // one, just return the collection.
      #foreach ($columnName in $fk.ForeignColumns)
        #set ( $column = $table.getColumn($columnName) )
        #set ( $colFKName = $fk.ForeignLocalMapping.get($columnName) )
        #set ( $colFK = $tblFK.getColumn($colFKName) )
                 criteria.add(${className}Peer.${colFK.Name.toUpperCase()}, 
get${column.JavaName}());
      #end
                 if (!last${relCol}Criteria.equals(criteria))
                 {
                     $collName = ${className}Peer.doSelect(criteria, con);
                 }
             }
         }
         last${relCol}Criteria = criteria;

         return $collName;
     }

      #set ( $countFK = 0 )
      #foreach ($dummyFK in $tblFK.ForeignKeys)
        #set ( $countFK = $countFK + 1 )
      #end

## ------------------------------------------------------------
##
      #if ($countFK >= 1)
        #set ( $lastTable = "" )
        #foreach ($fk2 in $tblFK.ForeignKeys)

          ## Add join methods if the fk2 table is not this table or
          ## the fk2 table references this table multiple times.

          #set ( $doJoinGet = true )
          #if ( $fk2.ForeignTableName.equals($table.Name) )
            #set ( $doJoinGet = false )
          #end

          #foreach ($columnName in $fk2.LocalColumns)
            #set ( $column = $tblFK.getColumn($columnName) )
            #if ($column.isMultipleFK())
              #set ( $doJoinGet = true )
            #end
          #end

          #set ( $tblFK2 = $table.Database.getTable($fk2.ForeignTableName) )
          #if ($tblFK2.isForReferenceOnly())
            #set ($doJoinGet = false)
          #else
            #set ($doJoinGet = true)
          #end
          #set ( $relatedByCol2 = "" )
          #foreach ($columnName in $fk2.LocalColumns)
            #set ( $column = $tblFK.getColumn($columnName) )
            #if ($column.isMultipleFK())
              #set($relatedByCol2 = "$relatedByCol2$column.JavaName")
            #end
          #end

          #set ( $fkClassName = $tblFK2.JavaName )
          ## do not generate code for self-referencing fk's, it would be
          ## good to do, but it is just not implemented yet.
          #if ($className == $fkClassName)
            #set ( $doJoinGet = false )
          #end

          #if ($relatedByCol2 == "")
            #set ( $relCol2 = $fkClassName )
          #else
            #set ($relCol2 = "${fkClassName}RelatedBy$relatedByCol2")
          #end

          #if ( $relatedByCol == "")
          #else
            #if ( $relatedByCol.equals($relatedByCol2) )
              #set ( $doJoinGet = false )
            #end
          #end

          #if ($doJoinGet)

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this $table.JavaName is new, it will return
     * an empty collection; or if this $table.JavaName has previously
     * been saved, it will retrieve related ${relCol} from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in ${table.JavaName}.
     */
    protected List get${relCol}Join${relCol2}(Criteria criteria)
        throws TorqueException
    {
        if ($collName == null)
        {
            if (isNew())
            {
               $collName = new ArrayList();
            }
            else
            {
            #foreach ($columnName in $fk.ForeignColumns)
              #set ( $column = $table.getColumn($columnName) )
              #set ( $colFKName = $fk.ForeignLocalMapping.get($columnName) )
              #set ( $colFK = $tblFK.getColumn($colFKName) )
                criteria.add(${className}Peer.${colFK.Name.toUpperCase()}, 
get${column.JavaName}());
            #end
                $collName = ${className}Peer.doSelectJoin${relCol2}(criteria);
            }
        }
        else
        {
            // the following code is to determine if a new query is
            // called for.  If the criteria is the same as the last
            // one, just return the collection.
            boolean newCriteria = true;
            #foreach ($columnName in $fk.ForeignColumns)
              #set ( $column = $table.getColumn($columnName) )
              #set ( $colFKName = $fk.ForeignLocalMapping.get($columnName) )
              #set ( $colFK = $tblFK.getColumn($colFKName) )
            criteria.add(${className}Peer.${colFK.Name.toUpperCase()}, 
get${column.JavaName}());
            #end
            if (!last${relCol}Criteria.equals(criteria))
            {
                $collName = ${className}Peer.doSelectJoin${relCol2}(criteria);
            }
        }
        last${relCol}Criteria = criteria;

        return $collName;
    }
          #end 
        #end 
      #end 
    #end

## ===========================================================
#*
  #if ($countFK > 2)

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this $table.JavaName is new, it will return
     * an empty collection; or if this $table.JavaName has previously
     * been saved, it will retrieve related ${relCol} from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in ${table.JavaName}.
     */
    protected List get${relCol}JoinAllExcept${table.JavaName}(Criteria criteria)
        throws TorqueException
    {
        if ($collName == null)
        {
            if (isNew())
            {
               $collName = new ArrayList();
            }
            else
            {
   #foreach ($columnName in $fk.ForeignColumns)
       #set ( $column = $table.getColumn($columnName) )
       #set ( $colFKName = $fk.ForeignLocalMapping.get($columnName) )
       #set ( $colFK = $tblFK.getColumn($colFKName) )
                criteria.add(${className}Peer.${colFK.Name.toUpperCase()}, 
get${column.JavaName}());
   #end
                $collName = 
${className}Peer.doSelectJoinAllExcept${table.JavaName}${suffix}(criteria);
            }
        }
        else
        {
            // the following code is to determine if a new query is
            // called for.  If the criteria is the same as the last
            // one, just return the collection.
            boolean newCriteria = true;
   #foreach ($columnName in $fk.ForeignColumns)
       #set ( $column = $table.getColumn($columnName) )
       #set ( $colFKName = $fk.ForeignLocalMapping.get($columnName) )
       #set ( $colFK = $tblFK.getColumn($colFKName) )
                criteria.add(${className}Peer.${colFK.Name.toUpperCase()}, 
get${column.JavaName}());
   #end
            if (!last${relCol}Criteria.equals(criteria))
            {
                $collName = 
${className}Peer.doSelectJoinAllExcept${table.JavaName}${suffix}(criteria);
            }
        }
        last${relCol}Criteria = criteria;

        return $collName;
    }

  #end

*#
## ------------------------------------------------------------

  #end    ## ends foreach over table.Referrers
#end    ##ends the if(complexObjectModel)

##
## getByName code
##
#if (!$table.isAlias() && $addGetByNameMethod)
    private static List fieldNames = null;

    /**
     * Generate a list of field names.
     *
     * @return a list of field names
     */
    public static synchronized List getFieldNames()
    {
        if (fieldNames == null)
        {
            fieldNames = new ArrayList();
  #foreach ($col in $table.Columns)
            fieldNames.add("${col.JavaName}");
  #end
            fieldNames = Collections.unmodifiableList(fieldNames);
        }
        return fieldNames;
    }

    /**
     * Retrieves a field from the object by name passed in as a String.
     *
     * @param name field name
     * @return value
     */
    public Object getByName(String name)
    {
  #foreach ($col in $table.Columns)
    #set ( $cfc = $col.JavaName )
    #set ( $cjtype = $col.JavaNative )
        if (name.equals("${col.JavaName}"))
        {
    #if ($cjtype == "int")
            return new Integer(get${cfc}());
    #elseif ($cjtype == "long")
            return new Long(get${cfc}());
    #elseif ($cjtype == "float")
            return new Float(get${cfc}());
    #elseif ($cjtype == "double")
            return new Double(get${cfc}());
    #elseif ($cjtype == "boolean")
            return new Boolean(get${cfc}());
    #elseif ($cjtype == "short")
            return new Short(get${cfc}());
    #elseif ($cjtype == "byte")
            return new Byte(get${cfc}());
    #elseif ($cjtype == "char")
            return new Character(get${cfc}());
    #else
            return get${cfc}();
    #end
        }
  #end
        return null;
    }
    
    /**
     * Retrieves a field from the object by name passed in
     * as a String.  The String must be one of the static
     * Strings defined in this Class' Peer.
     *
     * @param name peer name
     * @return value
     */
    public Object getByPeerName(String name)
    {
  #foreach ($col in $table.Columns)
    #set ( $cfc = $col.JavaName )
    #set ( $cup=$col.Name.toUpperCase() )
    #set ( $cjtype = $col.JavaNative )
        if (name.equals(${table.JavaName}Peer.$cup))
        {
    #if ($cjtype == "int")
            return new Integer(get${cfc}());
    #elseif ($cjtype == "long")
            return new Long(get${cfc}());
    #elseif ($cjtype == "float")
            return new Float(get${cfc}());
    #elseif ($cjtype == "double")
            return new Double(get${cfc}());
    #elseif ($cjtype == "boolean")
            return new Boolean(get${cfc}());
    #elseif ($cjtype == "short")
            return new Short(get${cfc}());
    #elseif ($cjtype == "byte")
            return new Byte(get${cfc}());
    #elseif ($cjtype == "char")
            return new Character(get${cfc}());
    #else
            return get${cfc}();
    #end
        }
  #end
        return null;
    }

    /**
     * Retrieves a field from the object by Position as specified
     * in the xml schema.  Zero-based.
     *
     * @param pos position in xml schema
     * @return value
     */
    public Object getByPosition(int pos)
    {
  #set ($i = 0)
  #foreach ($col in $table.Columns)
    #set ( $cfc = $col.JavaName )
    #set ( $cjtype = $col.JavaNative )
        if (pos == $i)
        {
    #if ($cjtype == "int")
            return new Integer(get${cfc}());
    #elseif ($cjtype == "long")
            return new Long(get${cfc}());
    #elseif ($cjtype == "float")
            return new Float(get${cfc}());
    #elseif ($cjtype == "double")
            return new Double(get${cfc}());
    #elseif ($cjtype == "boolean")
            return new Boolean(get${cfc}());
    #elseif ($cjtype == "short")
            return new Short(get${cfc}());
    #elseif ($cjtype == "byte")
            return new Byte(get${cfc}());
    #elseif ($cjtype == "char")
            return new Character(get${cfc}());
    #else
            return get${cfc}();
    #end
        }
    #set ($i = $i + 1)
  #end
        return null;
    }
#end     ## ends the if(addGetByNameMethod)

#if (!$table.isAlias() && $addSaveMethod)
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     *
     * @throws $saveException
     */
    public void save() throws $saveException
    {
  #if ($complexObjectModel)
        save(${table.JavaName}Peer.getMapBuilder()
                .getDatabaseMap().getName());
  #else
        if (isModified())
        {
            if (isNew())
            {
                ${table.JavaName}Peer.doInsert(($table.JavaName) this);
                setNew(false);
            }
            else
            {
                ${table.JavaName}Peer.doUpdate(($table.JavaName) this);
            }
        }
  #end
    }

    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
  #if ($complexObjectModel)
     * Note: this code is here because the method body is
     * auto-generated conditionally and therefore needs to be
     * in this file instead of in the super class, BaseObject.
  #end
     *
     * @param dbName
     * @throws TorqueException
     */
    public void save(String dbName) throws TorqueException
    {
        Connection con = null;
  #if ($complexObjectModel)
        try
        {
            con = Transaction.begin(dbName);
            save(con);
            Transaction.commit(con);
        }
        catch(TorqueException e)
        {
            Transaction.safeRollback(con);
            throw e;
        }
  #else
        if (isModified())
        {
            try
            {
                con = Torque.getConnection(dbName);
                if (isNew())
                {
                    ${table.JavaName}Peer
                        .doInsert(($table.JavaName) this, con);
                    setNew(false);
                }
                else
                {
                    ${table.JavaName}Peer
                        .doUpdate(($table.JavaName) this, con);
                }
            }
            finally
            {
                Torque.closeConnection(con);
            }
        }
  #end
    }

  #if ($complexObjectModel)
    /** flag to prevent endless save loop, if this object is referenced
        by another object which falls in this transaction. */
    private boolean alreadyInSave = false;
  #end
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.  This method
     * is meant to be used as part of a transaction, otherwise use
     * the save() method and the connection details will be handled
     * internally
     *
     * @param con
     * @throws TorqueException
     */
    public void save(Connection con) throws TorqueException
    {
  #if ($complexObjectModel)
        if (!alreadyInSave)
        {
            alreadyInSave = true;

#* FIXME! the following code can cause an infinite loop, needs more thought
shows the infinite loop: System.out.println("Entering save for " + this);
    #if ($pVars.size() != 0)

        // We call the save method on the following object(s) if they
        // were passed to this object by their coresponding set
        // method.  This object relates to these object(s) by a
        // foreign key reference.  If the object(s) being saved were
        // new to the database, an insert was performed, then they may
        // have a new PrimaryKey.  We call the coresponding set method
        // for the given object(s) to set this object's Id reference
        // to this new Primary key so that it will be saved.
      #foreach ($aVarName in $aVars)
        #set($i = $velocityCount - 1)
        if ($aVarName != null)
        {
            ${aVarName}.save(con);
            set$pVars.get($i)($aVarName);
        }
      #end
    #end
*#
  #end

            // If this object has been modified, then save it to the database.
            if (isModified())
            {
                if (isNew())
                {
                    ${table.JavaName}Peer.doInsert(($table.JavaName) this, con);
                    setNew(false);
                }
                else
                {
                    ${table.JavaName}Peer.doUpdate(($table.JavaName) this, con);
                }
            }

  #if ($complexObjectModel)
    #foreach ($fk in $table.Referrers)
      #set ( $tblFK = $fk.Table )
      #if ( !($tblFK.Name.equals($table.Name)) )
        #set ( $className = $tblFK.JavaName )
        #set ( $relCol = "" )
        #foreach ($columnName in $fk.LocalColumns)
          #set ( $column = $tblFK.getColumn($columnName) )
          #if ($column.isMultipleFK())
            #set ( $relCol = "$relCol$column.JavaName" )
          #end
        #end

        #if ($relCol == "")
          #set ( $relCol = "${className}s" )
        #else
          #set ( $relCol = "${className}sRelatedBy$relCol" )
        #end
        #set ( $collName = "coll$relCol" )

            if ($collName != null)
            {
                for (int i = 0; i < ${collName}.size(); i++)
                {
                    ((${className}) ${collName}.get(i)).save(con);
                }
            }
      #end
    #end
  #end
  #if ($complexObjectModel)
            alreadyInSave = false;
        }
  #end
    }
#end

## PrimaryKey methods
#if (!$table.isAlias())

  #set ( $throwsClause = "" )
  #set ( $argList = "" )
  #set ( $argList2 = "" )
  #set ( $comma = "" )
  #foreach ($col in $table.PrimaryKey)
    #if ($complexObjectModel)
      #if ( $col.isForeignKey() || ($col.Referrers.size() > 0) )
        #set ( $throwsClause = "throws TorqueException" )
      #end
    #end
    #set ( $clo=$col.UncapitalisedJavaName )
    #set ( $cjtype = $col.JavaNative )
    #set ($argList = "${argList}$comma $cjtype $clo")
    #set ($argList2 = "${argList2}$comma String $clo")
    #set ( $comma = "," )
  #end

  #if ($table.PrimaryKey.size() == 1)
    #set ($col = $table.PrimaryKey.get(0) )
    #set ( $clo=$col.UncapitalisedJavaName )
    #set ( $cjtype= $col.JavaNative )
    /**
     * Set the PrimaryKey using ObjectKey.
     *
     * @param  $clo ObjectKey
     */
    public void setPrimaryKey(ObjectKey key)
        $throwsClause
    {
    #if ($cjtype == "short")
        set${col.JavaName}(((NumberKey) key).shortValue());
    #elseif($cjtype == "int")
        set${col.JavaName}(((NumberKey) key).intValue());
    #elseif($cjtype == "long")
        set${col.JavaName}(((NumberKey) key).longValue());
    #elseif($cjtype == "BigDecimal")
        set${col.JavaName}(((NumberKey) key).getBigDecimal());
    #elseif($cjtype == "byte") )
        set${col.JavaName}(((NumberKey) key).byteValue());
    #elseif($cjtype == "float")
        set${col.JavaName}(((NumberKey) key).floatValue());
    #elseif($cjtype == "double")
        set${col.JavaName}(((NumberKey) key).doubleValue());
    #elseif($cjtype == "Short")
        set${col.JavaName}(new Short(((NumberKey) key).shortValue()));
    #elseif($cjtype == "Integer")
        set${col.JavaName}(new Integer(((NumberKey) key).intValue()));
    #elseif($cjtype == "Long")
        set${col.JavaName}(new Long(((NumberKey) key).longValue()));
    #elseif($cjtype == "Byte") )
        set${col.JavaName}(new Byte(((NumberKey) key).byteValue()));
    #elseif($cjtype == "Float")
        set${col.JavaName}(new Float(((NumberKey) key).floatValue()));
    #elseif($cjtype == "Double")
        set${col.JavaName}(new Double(((NumberKey) key).doubleValue()));
    #elseif($cjtype == "String")
        set${col.JavaName}(key.toString());
    #elseif($cjtype == "Date")
        set${col.JavaName}(((DateKey)key).getDate());
    #else
        set${col.JavaName}(($cjtype)key);
    #end
    }

    /**
     * Set the PrimaryKey using a String.
     *
     * @param key
     */
    public void setPrimaryKey(String key) $throwsClause
    {
    #if ($cjtype == "short")
        set${col.JavaName}(Short.parseShort(key));
    #elseif($cjtype == "int")
        set${col.JavaName}(Integer.parseInt(key));
    #elseif($cjtype == "long")
        set${col.JavaName}(Long.parseLong(key));
    #elseif($cjtype == "BigDecimal")
        set${col.JavaName}(new BigDecimal(key));
    #elseif($cjtype == "byte") )
        set${col.JavaName}(Byte.parseByte(key));
    #elseif($cjtype == "float")
        set${col.JavaName}(Float.parseFloat(key));
    #elseif($cjtype == "double")
        set${col.JavaName}(Double.parseDouble(key));
    #elseif($cjtype == "Short")
        set${col.JavaName}(new Short(key));
    #elseif($cjtype == "Integer")
        set${col.JavaName}(new Integer(key));
    #elseif($cjtype == "Long")
        set${col.JavaName}(new Long(key));
    #elseif($cjtype == "Byte") )
        set${col.JavaName}(new Byte(key));
    #elseif($cjtype == "Float")
        set${col.JavaName}(new Float(key));
    #elseif($cjtype == "Double")
        set${col.JavaName}(new Double(key));
    #elseif($cjtype == "String")
        set${col.JavaName}(key);
    #elseif($cjtype == "Date")
        set${col.JavaName}(new Date(key));
    #else
        set${col.JavaName}(new ${cjtype}(key));
    #end
    }

  #elseif ($table.PrimaryKey.size() > 1)

    private final SimpleKey[] pks = new SimpleKey[$table.PrimaryKey.size()];
    private final ComboKey comboPK = new ComboKey(pks);
    
    /**
     * Set the PrimaryKey with an ObjectKey
     *
     * @param key
     */
    public void setPrimaryKey(ObjectKey key) throws TorqueException
    {
        SimpleKey[] keys = (SimpleKey[]) key.getValue();
        SimpleKey tmpKey = null;
    #set ($i = 0)
    #foreach ($pk in $table.PrimaryKey)
      #set ($pktype = $pk.JavaNative)
      #if ($pktype == "short")
        set${pk.JavaName}(((NumberKey)keys[$i]).shortValue());
      #elseif($pktype == "int")
        set${pk.JavaName}(((NumberKey)keys[$i]).intValue());
      #elseif($pktype == "long")
        set${pk.JavaName}(((NumberKey)keys[$i]).longValue());
      #elseif($pktype == "BigDecimal")
        set${pk.JavaName}(((NumberKey)keys[$i]).getBigDecimal());
      #elseif($pktype == "byte") )
        set${pk.JavaName}(((NumberKey)keys[$i]).byteValue());
      #elseif($pktype == "float")
        set${pk.JavaName}(((NumberKey)keys[$i]).floatValue());
      #elseif($pktype == "double")
        set${pk.JavaName}(((NumberKey)keys[$i]).doubleValue());
      #elseif($pktype == "Short")
        set${pk.JavaName}(new Short(((NumberKey)keys[$i]).shortValue()));
      #elseif($pktype == "Integer")
        set${pk.JavaName}(new Integer(((NumberKey)keys[$i]).intValue()));
      #elseif($pktype == "Long")
        set${pk.JavaName}(new Long(((NumberKey)keys[$i]).longValue()));
      #elseif($pktype == "Byte") )
        set${pk.JavaName}(new Byte(((NumberKey)keys[$i]).byteValue()));
      #elseif($pktype == "Float")
        set${pk.JavaName}(new Float(((NumberKey)keys[$i]).floatValue()));
      #elseif($pktype == "Double")
        set${pk.JavaName}(new Double(((NumberKey)keys[$i]).doubleValue()));
      #elseif($pktype == "String")
        set${pk.JavaName}(keys[$i].toString());
      #elseif($pktype == "Date")
        set${pk.JavaName}(((DateKey)keys[$i]).getDate());
      #end
      #set ( $i = $i + 1 )
    #end
    }

    /**
     * Set the PrimaryKey using SimpleKeys.
     *
    #foreach ($col in $table.PrimaryKey)
      #set ( $clo=$col.UncapitalisedJavaName )
      #set ( $cjtype= $col.JavaNative )
     * @param $cjtype $clo
    #end
     */
    public void setPrimaryKey($argList)
        $throwsClause
    {
    #foreach ($col in $table.PrimaryKey)
        set${col.JavaName}($col.UncapitalisedJavaName);
    #end
    }

    /**
     * Set the PrimaryKey using a String.
     */
    public void setPrimaryKey(String key) throws TorqueException
    {
        setPrimaryKey(new ComboKey(key));
    }
  #end

    /**
     * returns an id that differentiates this object from others
     * of its class.
     */
    public ObjectKey getPrimaryKey()
    {
  #if ($table.PrimaryKey.size() == 1)
        return SimpleKey.keyFor(get${table.PrimaryKey.get(0).JavaName}());
  #elseif ($table.PrimaryKey.size() > 1)
    #set ($i = 0)
    #foreach ($pk in $table.PrimaryKey)
        pks[$i] = SimpleKey.keyFor(get${pk.JavaName}());
      #set ($i = $i +1)
    #end
        return comboPK;
  #else
        return null;
  #end
    }

#end ##ends if(!$table.isAlias())

#if ($addIntakeRetrievable)
    /**
     * get an id that differentiates this object from others
     * of its class.
     */
    public String getQueryKey()
    {
        if (getPrimaryKey() == null)
        {
            return "";
        }
        else
        {
            return getPrimaryKey().toString();
        }
    }

    /**
     * set an id that differentiates this object from others
     * of its class.
     */
    public void setQueryKey(String key)
        throws TorqueException
    {
        setPrimaryKey(key);
    }
#end

#if (!$table.isAlias())
    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
  #if ($complexObjectModel)
     * It then fills all the association collections and sets the
     * related objects to isNew=true.
  #end
     */
  #if ($table.isAbstract())
    public abstract $table.JavaName copy() throws TorqueException;
  #else
    public $table.JavaName copy() throws TorqueException
    {
        return copyInto(new ${table.JavaName}());
    }
  #end

    protected $table.JavaName copyInto($table.JavaName copyObj) throws TorqueException
    {
  #foreach ($col in $table.Columns)
        copyObj.set${col.JavaName}($col.UncapitalisedJavaName);
  #end

  #foreach ($col in $table.Columns)
    #if ($col.isPrimaryKey())
      #if($col.Primitive)
        #set ($coldefval = "0")
        #set ($coldefval = $col.DefaultValue)
        copyObj.set${col.JavaName}($coldefval);
      #else
        #set ( $pkid = "null" )
        #set ( $cjtype = $col.JavaNative )
        copyObj.set${col.JavaName}((${cjtype})${pkid});
      #end
    #end
  #end

  #if ($complexObjectModel)
    #set ( $list = "List " )
    #foreach ($fk in $table.Referrers)
      #set ( $tblFK = $fk.Table )
      #if ( !($tblFK.Name.equals($table.Name)) )
        #set ( $className = $tblFK.JavaName )
        #set ( $relCol = "" )
        #foreach ($columnName in $fk.LocalColumns)
          #set ( $column = $tblFK.getColumn($columnName) )
          #if ($column.isMultipleFK())
            #set ( $relCol = "$relCol$column.JavaName" )
          #end
        #end

        #if ($relCol == "")
          #set ( $pCollName = "${className}s" )
          #set ( $pCollNameNoS = "${className}" )
        #else
          #set ( $pCollName = "${className}sRelatedBy$relCol" )
          #set ( $pCollNameNoS = "${className}RelatedBy$relCol" )
        #end

        ${list}v = get${pCollName}();
        for (int i = 0; i < v.size(); i++)
        {
            $className obj = ($className) v.get(i);
            copyObj.add$pCollNameNoS(obj.copy());
        }
        #set ( $list = "" )
      #end
    #end
  #end

        return copyObj;
    }
#end

#if (!$table.isAlias())
    /**
     * returns a peer instance associated with this om.  Since Peer classes
     * are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     */
    public ${table.JavaName}Peer getPeer()
    {
        return peer;
    }
#end

    public String toString()
    {
        StringBuffer str = new StringBuffer();
        str.append("$table.JavaName:\n");
#foreach ($col in $table.Columns)
        str.append("$col.JavaName = ")
           .append(get${col.JavaName}())
           .append("\n");
#end
        return(str.toString());
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to