Here is a patch that saves a Torque OM object passed as a parameter 
to the set method of  another Torque OM object.  Of course the second 
object's schema table definition contains a foreign key reference to the 
first OM object.

 Here is a snippet of code that it creates:

    public void save(DBConnection dbCon) throws Exception
    {
            doPreSave();
        if (!alreadyInSave)
      {
        alreadyInSave = true;
               
        if (aQuestionType != null)
        {   
            aQuestionType.save(dbCon);
            setQuestionType(aQuestionType);
        }  
               
        if (aAccount != null)
        {   
            aAccount.save(dbCon);
            setAccount(aAccount);
        }  
                 
        if (isModified())
        {
            if (isNew())
            {
                SectionPeer.doInsert((Section)this, dbCon);
                setNew(false);
            }
            else
            {
                SectionPeer.doUpdate((Section)this, dbCon);
            }
        }


     So, we first test if a given "set object" exists, if so save it, 
then update the foreign key id reference property with a possible new Id 
created by the save.  The set object is saved before the containing 
object is saved since we need to get a foreign key id from a possibly 
new set object.  When the containing object is saved we want to insure 
that it contains a foreign key id for the set object.  If these object 
are not modified, then nothing will be save because the set object id 
will be compared to the current set object id and they will be found to 
match.  In this case the modified flag is left as is, and no action is 
taken.  The alreadyInSave flag prevents any infinite looping.

     We've been running with this patch for about a week and everything 
works well.  This is slick since through a combination of set and/or add 
methods a complicated relation of objects can be saved within the same 
transaction by a single method call.

Thanks,
Byron



John McNally wrote:

>I am interested in seeing your solution.  I implemented the automatic
>transaction based on the add methods.  I just was not sure about
>creating an infinite loop if I coded the other direction as well.  Maybe
>it is not a problem.
>
>john mcnally
>
>Byron Foster wrote:
>
>>Hello,
>>
>>   If I have tables Foo and Bar defined in the torque database schema
>>and Bar is linked to Foo by a foreign key reference then Torque will
>>create a Foo Java object with an addBar(Bar bar) method and will also
>>create a Bar  Java object with a setFoo(Foo foo) method.  Now in the
>>case of the Foo object the addBar(Bar) method is very cool since I can
>>simply add Bar objects to Foo using this method and when calling the
>>save method on the Foo object all Bar objects are inserted/updated and
>>the linkage is handled.  However, if i call the setFoo (Foo foo) method
>>on the Bar object, the linkage is handled, but foo will not be saved
>>when calling the save method on Bar.  This is unfortunate and
>>inconsistent given the handling of the addXXX methods.  Ok,  so unless
>>I'm missing something I can add this functionality.
>>
>>Thanks,
>>Byron
>>
>>


Index: src/templates/om/Object.vm
===================================================================
RCS file: /home/cvspublic/jakarta-turbine-torque/src/templates/om/Object.vm,v
retrieving revision 1.9
diff -u -U5 -r1.9 Object.vm
--- src/templates/om/Object.vm  2001/10/24 19:44:38     1.9
+++ src/templates/om/Object.vm  2001/10/25 14:15:30
@@ -192,10 +192,12 @@
 #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 ( $tmp = $fk.ForeignTableName )
    #set ( $tblFK = $table.Database.getTable($tmp) )
    #set ( $className = $tblFK.JavaName )
@@ -214,11 +216,12 @@
        #set ( $relCol = $strings.concat(["RelatedBy", $relCol]) )
    #end
 
    #set ( $pVarName = $strings.concat([$className, $relCol]) )
    #set ( $varName = $strings.concat(["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
      *
@@ -892,11 +895,36 @@
         doPreSave($secInfo);
   #if ($complexObjectModel)
       if (!alreadyInSave)
       {
         alreadyInSave = true;
-  #end      
+       #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.
+        #end
+        #set($aindx = 0) 
+        #foreach ($pVarName in $pVars)
+        #set($aVarName = $aVars.get($aindx))
+
+        if ($aVarName != null)
+        {   
+            ${aVarName}.save(dbCon);
+            set${pVarName}($aVarName);
+        }   
+        #set($aindx = $aindx+1)
+        #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, dbCon);

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

Reply via email to