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/25 16:41:00 UTC

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

     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
>>
>>