db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henning P. Schmiedehausen" <...@intermeta.de>
Subject Re: save an object tree / bug concerning the MODIFIED-flag
Date Tue, 14 Sep 2004 23:24:50 GMT
"Vitzethum, Daniel" <Daniel.Vitzethum@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


Mime
View raw message