You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-user@db.apache.org by "Vitzethum, Daniel" <Da...@gigatronik.com> on 2004/09/14 18:23:18 UTC

save an object tree / bug concerning the MODIFIED-flag

Hello,

my name is Daniel. I have just joined the list to ask one
or two related questions:

I would appreciate if Torque saved an object tree for me -
e.g. I have a Book with two Authors and want to call

 book.addAuthor(a1);
 book.addAuthor(a2);
 BookPeer.doSave(book);    // should insert or update the book and both
authors

For this purpose, and as it seems that this feature is not
implemented in Torque, I wrote the doSave(Book) method in BookPeer,
like this:

 public static void doSave(Book book) throws TorqueException {
    if (matchEinzel.isNew()) {
        BookPeer.doInsert(book);
    } else {
        if (book.isModified()) {
            BookPeer.doUpdate(matchEinzel);
        }
    }
    ObjectKey key = book.getPrimaryKey();
    List authors = book.collAuthors;
    
    // do the same for a List of author objects...
    doSaveAuthors(key, authors);
 }

That works quite well, but when I read a simple object from
DB and call the doSave() on it immediately, the object has
"true" as it's modified flag, what results in an (unwanted)
update in the above doSave()-method:

 Book oldBook = BookPeer.retrieveByPK(123);
 BookPeer.doSave(oldBook);    // performs an Update!


This can be fixed by overriding a BasePeer's method
row2Object(Record, int, Class) like this - what I don't want
to do for any generated class, as you may have guessed... ;-)

 public static Book row2Object(Record row, int offset, Class cls)
 throws TorqueException
 {
    Book obj = BaseBookPeer.row2Object(row, offset, cls);
    obj.setModified(false);
    return obj;
 }

So, finally my 2 questions:
- Did I miss a Torque feature that saves an object tree for me?
- Isn't it just wrong that a row being read freshly from DB has
  the modified-flag set to true?


Greetings from Munich, Germany,

Daniel

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Re: save an object tree / bug concerning the MODIFIED-flag

Posted by Howard Lin <xu...@gmail.com>.
Hi, if you specify relations in the schema files, (assumimg Author
table has a foreign key to Book table), like this:
<table name="AUTHOR">
     ....
      <foreign-key foreignTable="BOOK">
            <reference foreign="BOOK_ID" local="BOOK_ID"/>
        </foreign-key>
</table>
Torque will generate save methods in the BaseBook to save authers as well.

For your second question, I don't think it's a bug. I think Torque
assumes you are going to do something with the object, otherwise you
can always call setModified(false) before call save.

Howard 

On Tue, 14 Sep 2004 18:23:18 +0200, Vitzethum, Daniel
<da...@gigatronik.com> wrote:
> Hello,
> 
> my name is Daniel. I have just joined the list to ask one
> or two related questions:
> 
> I would appreciate if Torque saved an object tree for me -
> e.g. I have a Book with two Authors and want to call
> 
>  book.addAuthor(a1);
>  book.addAuthor(a2);
>  BookPeer.doSave(book);    // should insert or update the book and both
> authors
> 
> For this purpose, and as it seems that this feature is not
> implemented in Torque, I wrote the doSave(Book) method in BookPeer,
> like this:
> 
>  public static void doSave(Book book) throws TorqueException {
>     if (matchEinzel.isNew()) {
>         BookPeer.doInsert(book);
>     } else {
>         if (book.isModified()) {
>             BookPeer.doUpdate(matchEinzel);
>         }
>     }
>     ObjectKey key = book.getPrimaryKey();
>     List authors = book.collAuthors;
> 
>     // do the same for a List of author objects...
>     doSaveAuthors(key, authors);
>  }
> 
> That works quite well, but when I read a simple object from
> DB and call the doSave() on it immediately, the object has
> "true" as it's modified flag, what results in an (unwanted)
> update in the above doSave()-method:
> 
>  Book oldBook = BookPeer.retrieveByPK(123);
>  BookPeer.doSave(oldBook);    // performs an Update!
> 
> This can be fixed by overriding a BasePeer's method
> row2Object(Record, int, Class) like this - what I don't want
> to do for any generated class, as you may have guessed... ;-)
> 
>  public static Book row2Object(Record row, int offset, Class cls)
>  throws TorqueException
>  {
>     Book obj = BaseBookPeer.row2Object(row, offset, cls);
>     obj.setModified(false);
>     return obj;
>  }
> 
> So, finally my 2 questions:
> - Did I miss a Torque feature that saves an object tree for me?
> - Isn't it just wrong that a row being read freshly from DB has
>   the modified-flag set to true?
> 
> Greetings from Munich, Germany,
> 
> Daniel
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Re: save an object tree / bug concerning the MODIFIED-flag

Posted by "Henning P. Schmiedehausen" <hp...@intermeta.de>.
"Henning P. Schmiedehausen" <hp...@intermeta.de> writes:

>>I would appreciate if Torque saved an object tree for me -
>>e.g. I have a Book with two Authors and want to call

>> book.addAuthor(a1);
>> book.addAuthor(a2);
>> BookPeer.doSave(book);    // should insert or update the book and both
>>authors

>Well, Torque does not work this way. The foreign key getters are just shortcuts
>for 

>Object.getForeigns() is equivalent to

>Criteria crit = new Criteria();
>crit.add(ForeignPeer.OBJECT_ID, Object.OBJECT_ID);
>List foreigns = ForeignPeer.doSelect(crit);

>There are no setters for this (or add<xxx> methods. The objects do not
>cache intermediate collections of objects.

[...]

>>So, finally my 2 questions:
>>- Did I miss a Torque feature that saves an object tree for me?

>No. There is no such thing in current Torque (at least 3.1). If you
>want to save an object tree, it might be possible to write such a
>bugger but you would end up with either having to retrieve existing
>objects for your foreign keys (expensive) or some sort of caching (as
>Hibernate does).

Ok. I put my foot in my mouth a little (I shouldn't post past 1am):
There actually _is_ such a thing. Sorry.

There are two Object flavours. The classes that are created with

torque.objectIsCaching = false  

in project.properties behave just like I wrote. If you put

torque.objectIsCaching = true

in your project.properties before generating your peers/objects then
you will have add<ForeignKeyObject>s methods in your object classes.

I do remember now that I've tried these once. As you are a german,
this is verbatim from my application project.properties file after
trying this out:

--- cut ---
# Unsere Objekte duerfen nicht cachen, sonst geschehen schlimme
# Dinge!
torque.objectIsCaching = false
--- cut ---

	Regards
		Henning

P.S.: Rough translation is "Our object must not cache or bad things
will happen!" I my case, the bad things were that my application ate
my database. There were some issues with the id generation of the
connected objects. I can't recall the exact problems anymore and it is
quite possible that they are gone from the 3.1.1 (and 3.2) tree. YMMV.

P.P.S.: I'm pretty sure that this simply needs more debugging. Patches
welcome!

-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/

RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for hire
   Linux, Java, perl, Solaris -- Consulting, Training, Development

"Fighting for one's political stand is an honorable action, but re-
 fusing to acknowledge that there might be weaknesses in one's
 position - in order to identify them so that they can be remedied -
 is a large enough problem with the Open Source movement that it
 deserves to be on this list of the top five problems."
                       -- Michelle Levesque, "Fundamental Issues with
                                    Open Source Software Development"

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Re: save an object tree / bug concerning the MODIFIED-flag

Posted by "Henning P. Schmiedehausen" <hp...@intermeta.de>.
"Vitzethum, Daniel" <Da...@gigatronik.com> writes:

>Hello,

>my name is Daniel. I have just joined the list to ask one
>or two related questions:

>I would appreciate if Torque saved an object tree for me -
>e.g. I have a Book with two Authors and want to call

> book.addAuthor(a1);
> book.addAuthor(a2);
> BookPeer.doSave(book);    // should insert or update the book and both
>authors

Well, Torque does not work this way. The foreign key getters are just shortcuts
for 

Object.getForeigns() is equivalent to

Criteria crit = new Criteria();
crit.add(ForeignPeer.OBJECT_ID, Object.OBJECT_ID);
List foreigns = ForeignPeer.doSelect(crit);

There are no setters for this (or add<xxx> methods. The objects do not
cache intermediate collections of objects.


>For this purpose, and as it seems that this feature is not
>implemented in Torque, I wrote the doSave(Book) method in BookPeer,
>like this:

> public static void doSave(Book book) throws TorqueException {
>    if (matchEinzel.isNew()) {

what is "matchEinzel"? 

>        BookPeer.doInsert(book);
>    } else {
>        if (book.isModified()) {
>            BookPeer.doUpdate(matchEinzel);
>        }
>    }
>    ObjectKey key = book.getPrimaryKey();
>    List authors = book.collAuthors;

What is this? An additional collection or just book.getAutors() ?

>    
>    // do the same for a List of author objects...
>    doSaveAuthors(key, authors);
> }

>That works quite well, but when I read a simple object from
>DB and call the doSave() on it immediately, the object has
>"true" as it's modified flag, what results in an (unwanted)
>update in the above doSave()-method:

> Book oldBook = BookPeer.retrieveByPK(123);
> BookPeer.doSave(oldBook);    // performs an Update!

Yeah, sure. The oldBook is a representation of a row in a table in the
database. If you save() on it and it does not do an update, you would
get a pk violation. Or is the fact that an update happens a problem
(as compared to "do nothing")? Consider your table having a
timestamp. Silently ignoring an update would mean, that such a
timestamp would not get updated. 

>This can be fixed by overriding a BasePeer's method
>row2Object(Record, int, Class) like this - what I don't want
>to do for any generated class, as you may have guessed... ;-)

This would be introducing a bug, not a fix. 

> public static Book row2Object(Record row, int offset, Class cls)
> throws TorqueException
> {
>    Book obj = BaseBookPeer.row2Object(row, offset, cls);
>    obj.setModified(false);
>    return obj;
> }

>So, finally my 2 questions:
>- Did I miss a Torque feature that saves an object tree for me?

No. There is no such thing in current Torque (at least 3.1). If you
want to save an object tree, it might be possible to write such a
bugger but you would end up with either having to retrieve existing
objects for your foreign keys (expensive) or some sort of caching (as
Hibernate does).

>- Isn't it just wrong that a row being read freshly from DB has
>  the modified-flag set to true?

Probably not. Consider the case of database generated time stamps. If
this is a problem for you, you can simply call setModfied(false);

	Regards
		Henning

-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/

RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for hire
   Linux, Java, perl, Solaris -- Consulting, Training, Development

"Fighting for one's political stand is an honorable action, but re-
 fusing to acknowledge that there might be weaknesses in one's
 position - in order to identify them so that they can be remedied -
 is a large enough problem with the Open Source movement that it
 deserves to be on this list of the top five problems."
                       -- Michelle Levesque, "Fundamental Issues with
                                    Open Source Software Development"

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org