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 01:40:35 UTC

Initial modified state of torque OM Java objects

I was wonder what the repercussions would be if the modified field of 
 org.apache.torque.om.BaseObject was intialized to true instead of false.

Case for setting it to true:

Given the following snipit of schema:

<table name="foo" idMethod="native">
    <column name="id" primaryKey="true" required="true" type="INTEGER" 
autoIncrement="true"/>
    <column name="val" type="INTEGER" />
</table>

<table name="bar">
    <column name="id" primaryKey="true" required="true"  type="INTEGER" 
autoIncrement="true" />
    <column name="val" type="INTEGER" />
    <column name="fooId"  type="INTEGER"/>

    <foreign-key foreignTable="foo">
      <reference local="fooId" foreign="id"/>
    </foreign-key>
</table>

The above schema will create java object Foo and Bar

now in code I can write

Bar bar1 = new Bar();
bar1.setVal(1);
bar2 = new Bar();
bar2.setVal(2);

Foo foo = new Foo();
foo.setVal(1);
foo.addBar(bar1);
foo.addBar(bar2);
foo.save();

And this all works great.  However, if I Don't call the setVal of the 
foo object before saving, this will not work, and I will get an 
SQLException stating That the foriegn key field of the bar objects are 
not intialized.  This occurs becase when Foo is created its modified 
flag is false so isModified on BaseObject returns false.  The modified 
flag will not be set to true unless a property is set.  This causes the 
new foo object not to be saved, and so incorrectly sets the foriegn key 
field fooId in bar.

A simpler example:

Foo foo = new Foo();
foo.save();

The following piece of code does nothing, becase the modified flag is 
false, so no insert is performed  even though no fields have been 
initialized I would think that foo should still be saved out.

I could simply call setModified(true) after constrution, but I think 
this is obscure.

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] Initial modified state of torque OM Java objects

Posted by Daniel Rall <dl...@finemaltcoding.com>.
Applied to jakarta-turbine-torque CVS, thanks Byron.  (I'll get to
your last patch eventually.  ;)

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

> Jon Stevens wrote:
>
>>on 10/14/01 3:58 PM, "John McNally" <jm...@collab.net> wrote:
>>
>>>+1.  I have thought about it and did not see any good reason not to make
>>>this change.  Does anyone else see a problem?
>>>
>>>john mcnally
>>>
>>
>>+1
>>
>>-jon
>>
>>
> Here is a patch that simply changes the initial value for the modified
> field in the torque org.apache.torque.om.BaseObject to true.
>
> Thanks,
> Byron
>
>
> Index: src/java/org/apache/torque/om/BaseObject.java
> ===================================================================
> RCS file: /home/cvspublic/jakarta-turbine-torque/src/java/org/apache/torque/om/BaseObject.java,v
> retrieving revision 1.4
> diff -u -r1.4 BaseObject.java
> --- src/java/org/apache/torque/om/BaseObject.java	2001/09/10 21:14:02	1.4
> +++ src/java/org/apache/torque/om/BaseObject.java	2001/10/16 21:23:34
> @@ -90,11 +90,13 @@
>      private ObjectKey primaryKey = null;
>  
>      /**
> -     * A flag which can be set to indicate that an object has been
> -     * modified, since it was last retrieved from the persistence
> -     * mechanism.
> +     * A flag that indicates an object has been modified since it was
> +     * last retrieved from the persistence mechanism.  This flag is
> +     * used to determine if this object should be saved to the
> +     * database.  We initialize it to true to force new objects to be
> +     * saved.
>       */
> -    private boolean modified = false;
> +    private boolean modified = true;
>  
>      /**
>       * getter for the object primaryKey.

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


[PATCH] Initial modified state of torque OM Java objects

Posted by Byron Foster <bf...@base2.cc>.
Jon Stevens wrote:

>on 10/14/01 3:58 PM, "John McNally" <jm...@collab.net> wrote:
>
>>+1.  I have thought about it and did not see any good reason not to make
>>this change.  Does anyone else see a problem?
>>
>>john mcnally
>>
>
>+1
>
>-jon
>
>
Here is a patch that simply changes the initial value for the modified 
field in the torque org.apache.torque.om.BaseObject to true.

Thanks,
Byron



Re: Initial modified state of torque OM Java objects

Posted by Jon Stevens <jo...@latchkey.com>.
on 10/14/01 3:58 PM, "John McNally" <jm...@collab.net> wrote:

> +1.  I have thought about it and did not see any good reason not to make
> this change.  Does anyone else see a problem?
> 
> john mcnally

+1

-jon


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


Re: Initial modified state of torque OM Java objects

Posted by John McNally <jm...@collab.net>.
+1.  I have thought about it and did not see any good reason not to make
this change.  Does anyone else see a problem?

john mcnally

Byron Foster wrote:
> 
> I was wonder what the repercussions would be if the modified field of
>  org.apache.torque.om.BaseObject was intialized to true instead of false.
> 
> Case for setting it to true:
> 
> Given the following snipit of schema:
> 
> <table name="foo" idMethod="native">
>     <column name="id" primaryKey="true" required="true" type="INTEGER"
> autoIncrement="true"/>
>     <column name="val" type="INTEGER" />
> </table>
> 
> <table name="bar">
>     <column name="id" primaryKey="true" required="true"  type="INTEGER"
> autoIncrement="true" />
>     <column name="val" type="INTEGER" />
>     <column name="fooId"  type="INTEGER"/>
> 
>     <foreign-key foreignTable="foo">
>       <reference local="fooId" foreign="id"/>
>     </foreign-key>
> </table>
> 
> The above schema will create java object Foo and Bar
> 
> now in code I can write
> 
> Bar bar1 = new Bar();
> bar1.setVal(1);
> bar2 = new Bar();
> bar2.setVal(2);
> 
> Foo foo = new Foo();
> foo.setVal(1);
> foo.addBar(bar1);
> foo.addBar(bar2);
> foo.save();
> 
> And this all works great.  However, if I Don't call the setVal of the
> foo object before saving, this will not work, and I will get an
> SQLException stating That the foriegn key field of the bar objects are
> not intialized.  This occurs becase when Foo is created its modified
> flag is false so isModified on BaseObject returns false.  The modified
> flag will not be set to true unless a property is set.  This causes the
> new foo object not to be saved, and so incorrectly sets the foriegn key
> field fooId in bar.
> 
> A simpler example:
> 
> Foo foo = new Foo();
> foo.save();
> 
> The following piece of code does nothing, becase the modified flag is
> false, so no insert is performed  even though no fields have been
> initialized I would think that foo should still be saved out.
> 
> I could simply call setModified(true) after constrution, but I think
> this is obscure.
> 
> 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