db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Saravana Krishnan Kannan" <saravkr...@uky.edu>
Subject Re: save an object tree / bug concerning the MODIFIED-flag
Date Tue, 14 Sep 2004 16:55:05 GMT
Hi Daniel,

  Lets say there are two tables A and B

A:
id number
name varchar(10)

B:
id number
parent fk references A.id
data varchar(10)

If you do-

A a = new A();
a.setName("Hmm");

B b1 = new B();
b1.setData("123");
a.addB(b1);

B b2 = new B();
b2.setData("456");
a.addB(b2);

a.save();

a, b1 and b2 get saved. I have not written any extra code. You just need to change the way
you make some calls. If you want  "a" to get saved when "b1" is modified, then you will have
to write extra code I guess.

~Sarav

-----Original Message-----
From: "Vitzethum, Daniel" <Daniel.Vitzethum@gigatronik.com>
To: <torque-user@db.apache.org>
Date: Tue, 14 Sep 2004 18:23:18 +0200
Subject: 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





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