You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by Byron Foster <bf...@base2.cc> on 2001/10/14 02:00:49 UTC

Saving object linked by foreign keys in torque OM

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
 




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


Re: [PATCH] Saving object linked by foreign keys in Torque OM

Posted by Byron Foster <bf...@base2.cc>.
> >
> > I got scarab and I was able to reproduce the problem. It appears that
> > the line:  'System.out.println("Entering save for " + this)' causes the
> > infinite loop.  Did you experience any problems without this debug line?
>
> Yes, I was getting the loop without the debug line.  At least I was
> getting the same behavior with and without the println.  Try removing
> the +this, it should still loop.
>

Hmm,  I was gettting it to fail when I entered in the confirmation key sent
by email.  With the 'System.out' line scarab would loop, but without it
scarab would not.   Is this the same area you found it to fail?

Thanks
Byron



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] Saving object linked by foreign keys in Torque OM

Posted by John McNally <jm...@collab.net>.
Byron Foster wrote:
> 
> I got scarab and I was able to reproduce the problem. It appears that
> the line:  'System.out.println("Entering save for " + this)' causes the
> infinite loop.  Did you experience any problems without this debug line?

Yes, I was getting the loop without the debug line.  At least I was
getting the same behavior with and without the println.  Try removing
the +this, it should still loop.

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] Saving object linked by foreign keys in Torque OM

Posted by Byron Foster <bf...@base2.cc>.
I got scarab and I was able to reproduce the problem. It appears that
the line:  'System.out.println("Entering save for " + this)' causes the
infinite loop.  Did you experience any problems without this debug line?
What seems to be happening is that the toString() method, invoked by
refrencing 'this', of ScarabModule calls getName() which then calls
getAncestors(), after a few more method calls there is a call to the
parent module to get its parent module.. and so the recursion begins.
Here is a snapshot of the stack trace into a constructor I made for
BaseScarabModule while the program is failing:

  [1] org.tigris.scarab.om.BaseScarabModule.<init>
(BaseScarabModule.java:94)
  [2] org.tigris.scarab.om.ScarabModule.<init> (ScarabModule.java:102)
  [3] java.lang.Class.newInstance0 (native method)
  [4] java.lang.Class.newInstance (Class.java:237)
  [5] org.tigris.scarab.om.BaseScarabModulePeer.row2Object
(BaseScarabModulePeer.java:212)
  [6] org.tigris.scarab.om.BaseScarabModulePeer.populateObjects
(BaseScarabModulePeer.java:358)
  [7] org.tigris.scarab.om.BaseScarabModulePeer.doSelect
(BaseScarabModulePeer.java:262)
  [8] org.tigris.scarab.om.BaseScarabModulePeer.retrieveByPK
(BaseScarabModulePeer.java:652)
  [9] org.tigris.scarab.om.BaseScarabModulePeer.retrieveByPK
(BaseScarabModulePeer.java:630)
  [10]
org.tigris.scarab.om.BaseScarabModule.getScarabModuleRelatedByParentId
(BaseScarabModule.java:585)
  [11] org.tigris.scarab.om.ScarabModule.getModuleRelatedByParentIdCast
(ScarabModule.java:328)
  [12] org.tigris.scarab.om.ScarabModule.getAncestors
(ScarabModule.java:238)
  [13] org.tigris.scarab.om.ScarabModule.getName (ScarabModule.java:191)
  [14] org.tigris.scarab.om.ScarabModule.toString
(ScarabModule.java:1309)
  [15] java.lang.String.valueOf (String.java:1947)
  [16] java.lang.StringBuffer.append (StringBuffer.java:370)
  [17] org.tigris.scarab.om.BaseScarabModule.save
(BaseScarabModule.java:2733)
  [18] org.tigris.scarab.om.BaseScarabModule.save
(BaseScarabModule.java:2747)
  [19] org.tigris.scarab.om.BaseScarabModule.save
(BaseScarabModule.java:2706)
  [20] org.tigris.scarab.om.BaseScarabModule.save
(BaseScarabModule.java:2689)


The fix is to remove the debug line :) 

Thanks
Byron





On Wed, 2001-11-14 at 18:10, Byron Foster
> Yea, I'll get scarab out of CVS and check it out.
> 
> Byron
> 
> > I get an infinite loop in scarab which has a object called a Module that
> > has a parent Module, the top most Module has itself defined as its
> > parent.
> > 
> > MODULE
> > ------
> > MODULE_ID pk
> > PARENT_ID fk
> > NAME
> > ...
> > 
> > saving the module with MODULE_ID=0 and PARENT_ID=0, is looping.  It did
> > not seem obvious to me from the code that this should be the case, so
> > maybe there is something more complicated causing a problem in scarab. 
> > Can you verify?




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] Saving object linked by foreign keys in Torque OM

Posted by Byron Foster <bf...@base2.cc>.
Yea, I'll get scarab out of CVS and check it out.

Byron

> I get an infinite loop in scarab which has a object called a Module that
> has a parent Module, the top most Module has itself defined as its
> parent.
> 
> MODULE
> ------
> MODULE_ID pk
> PARENT_ID fk
> NAME
> ...
> 
> saving the module with MODULE_ID=0 and PARENT_ID=0, is looping.  It did
> not seem obvious to me from the code that this should be the case, so
> maybe there is something more complicated causing a problem in scarab. 
> Can you verify?
> 



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] Saving object linked by foreign keys in Torque OM

Posted by John McNally <jm...@collab.net>.
I get an infinite loop in scarab which has a object called a Module that
has a parent Module, the top most Module has itself defined as its
parent.

MODULE
------
MODULE_ID pk
PARENT_ID fk
NAME
...

saving the module with MODULE_ID=0 and PARENT_ID=0, is looping.  It did
not seem obvious to me from the code that this should be the case, so
maybe there is something more complicated causing a problem in scarab. 
Can you verify?

john mcnally

Byron Foster wrote:
> 
> Hello John,
> 
> I noticed the following line in Object.vm related to the patch I
> submitted:
> 
> #* FIXME! the following code can cause an infinite loop, needs more
> thought
> shows the infinite loop: System.out.println("Entering save for " +
> this);
> 
> I am unable to re-create a case that causes infinite looping, If you
> could provide some information it would be helpful, Maybe a short
> description on how the objects are related.
> 
> Thanks,
> Byron
> 
> On Fri, 2001-11-09 at 10:44, John McNally wrote:
> 
> > I will add it this weekend unless you do it today.
> >
> > john mcnally
> >
> > Daniel Rall wrote:
> > >
> > > Was this ever addressed?
> > >
> > > Byron Foster <bf...@base2.cc> writes:
> > >
> > > >     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
> > > >
> > > >
> > > >
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] Saving object linked by foreign keys in Torque OM

Posted by Byron Foster <bf...@base2.cc>.
Hello John,

I noticed the following line in Object.vm related to the patch I
submitted:

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

I am unable to re-create a case that causes infinite looping, If you
could provide some information it would be helpful, Maybe a short
description on how the objects are related.

Thanks,
Byron



On Fri, 2001-11-09 at 10:44, John McNally wrote:

> I will add it this weekend unless you do it today.
> 
> john mcnally
> 
> Daniel Rall wrote:
> > 
> > Was this ever addressed?
> > 
> > Byron Foster <bf...@base2.cc> writes:
> > 
> > >     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
> > >
> > >
> > >



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] Saving object linked by foreign keys in Torque OM

Posted by John McNally <jm...@collab.net>.
I will add it this weekend unless you do it today.

john mcnally

Daniel Rall wrote:
> 
> Was this ever addressed?
> 
> Byron Foster <bf...@base2.cc> writes:
> 
> >     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: turbine-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: turbine-dev-help@jakarta.apache.org
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] Saving object linked by foreign keys in Torque OM

Posted by Daniel Rall <dl...@finemaltcoding.com>.
Was this ever addressed?

Byron Foster <bf...@base2.cc> writes:

>     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: turbine-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: turbine-dev-help@jakarta.apache.org

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


[PATCH] Saving object linked by foreign keys in Torque OM

Posted by Byron Foster <bf...@base2.cc>.
    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
>>



[PATCH] Saving object linked by foreign keys in Torque OM

Posted by Byron Foster <bf...@base2.cc>.
     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
>>
>>



Re: Saving object linked by foreign keys in torque OM

Posted by Byron Foster <bf...@base2.cc>.
I should add that alreadyInSave prevents looping not only given a simple 
case of two linked objects, but also prevents looping in a more general 
interconnected graph of Torque OM objects.

Byron

Byron Foster wrote:

>
> I think its all good :)   Given the current logic to prevent infinite 
> looping.  Using the Foo and Bar objects described in my original 
> email, such that Bar is linked to Foo by foreign key, the following 
> suedo java code (for the sake of brevity) would be generated. 
> save method in BaseBar.java:
>
>    public void save(DBConnection dbCon)
>    {
>        doPreSave();
>        if (!alreadyInSave)
>        {
>            alreadyInSave = true;
>            if (isModified())
>            {
>               // If new property is true then insert, else update.
>            }
>
>            // Proposed location for inserting/updating aFoo
>            aFoo.save(dbCon);
>        }                                                       }         
> methods in BaseFoo.java:
>
>   public void addBar(Bar l)
>   {
>        getBars().add(l);
>        l.setFoo((Foo)this);
>   }  
>   public void save(DBConnection dbCon)
>    {
>        doPreSave();
>        if (!alreadyInSave)
>        {
>            alreadyInSave = true;
>            if (isModified())
>            {
>               // If new property is true then insert, else update.
>            }
>
>            if (collBar != null )
>            {
>               // Loop through added Bar object and call the save(DB..) 
> method on each
>            }        
> }                                                       }         
>
> alreadyInSave prevents infinite looping with the added aFoo.save call 
> in BaseBar.java,  Now  given that we have made the change when a Bar 
> object is added to Foo the add method calls Bar.setFoo(this).  So, 
> when the save method is called on the Foo object, Foo will iterate 
> through all the Bar objects calling their save method. This intern 
> would make a call back to the Foo.save method, but this will exit 
> because alreadyInSave will now be true.
>
> Too make a long story short, it appears to fall out due to the 
> existing alreadyInSave flag.
>
> 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
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: turbine-dev-help@jakarta.apache.org
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: turbine-dev-help@jakarta.apache.org
>>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: turbine-dev-help@jakarta.apache.org





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


Re: Saving object linked by foreign keys in torque OM

Posted by Byron Foster <bf...@base2.cc>.
I think its all good :)   Given the current logic to prevent infinite 
looping.  Using the Foo and Bar objects described in my original email, 
such that Bar is linked to Foo by foreign key, the following suedo java 
code (for the sake of brevity) would be generated.  

save method in BaseBar.java:

    public void save(DBConnection dbCon)
    {
        doPreSave();
        if (!alreadyInSave)
        {
            alreadyInSave = true;
            if (isModified())
            {
               // If new property is true then insert, else update.
            }

            // Proposed location for inserting/updating aFoo
            aFoo.save(dbCon);
        }                                                   
     }          

methods in BaseFoo.java:

   public void addBar(Bar l)
   {
        getBars().add(l);
        l.setFoo((Foo)this);
   }   

   public void save(DBConnection dbCon)
    {
        doPreSave();
        if (!alreadyInSave)
        {
            alreadyInSave = true;
            if (isModified())
            {
               // If new property is true then insert, else update.
            }

            if (collBar != null )
            {
               // Loop through added Bar object and call the save(DB..) 
method on each
            } 
        }                                                   
     }          


alreadyInSave prevents infinite looping with the added aFoo.save call in 
BaseBar.java,  Now  given that we have made the change when a Bar object 
is added to Foo the add method calls Bar.setFoo(this).  So, when the 
save method is called on the Foo object, Foo will iterate through all 
the Bar objects calling their save method. This intern would make a call 
back to the Foo.save method, but this will exit because alreadyInSave 
will now be true.

Too make a long story short, it appears to fall out due to the existing 
alreadyInSave flag.

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
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: turbine-dev-help@jakarta.apache.org
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: turbine-dev-help@jakarta.apache.org
>




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


Re: Saving object linked by foreign keys in torque OM

Posted by John McNally <jm...@collab.net>.
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
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: turbine-dev-help@jakarta.apache.org

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