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 "Peter S. Hamlen" <ph...@mail.com> on 2003/02/19 16:05:39 UTC

Re: AW: how to tell the bo that a foreign collection must bereloaded?

Marc,

Good questions.  

1)  Specifically,  "addObject" adds the object to the parent object's
collection AND sets the foreign keys appropriately.  It does not save
the data to the database.

2)  The data will be saved to the database when you call save on the
original object.  There are two ways this can happen:
1)  You call save on the actual object.
2)  You call save on the parent object - in this case, the save routine
will walk through all objects in the collObjects list and call save on
each on those as well.

3)  Sample AddObject code. I usually do the following:

// retrieve a person (person 114 in this case)
Person p = PersonPeer.retrieveByPK(114);

// add an address for that person.
PersonAddress addr = new PersonAddress("52 Strong Pl", "Brooklyn",
"NY");
p.addPersonAddress(addr);

// Call save on the person and it will also call save on addr.
p.save();


The last line (p.save()) will save the new address to the database.


4)  Deleting objects is a little trickier.  Essentially, you need to
remove the object from collObjects, and THEN delete the object.  (The
save() function on the parent object will NOT delete removed objects.)

So the code fragment might look like:

Person p = PersonPeer.retrieveByPK(114);
PersonAddresss addrList = p.getPersonAddresss();
PersonAddress addr1 = addrList.get(0);
addr1.remove(0);
PersonAddressPeer.doDelete(addr1);

(NB:  Looking over the classes, it turns out that there isn't a
"removeObject" method that's generated.  We basically do the step above
when we want to delete.)

Hope this helps...
-Peter

On Tue, 2003-02-18 at 15:41, Marc Lustig wrote:
> 
> 
> > -----Ursprüngliche Nachricht-----
> > Von: Peter S. Hamlen [mailto:phamlen@mail.com]
> > Gesendet: Dienstag, 18. Februar 2003 19:39
> > An: Turbine Torque Users List
> > Betreff: Re: how to tell the bo that a foreign collection must be
> > reloaded?
> >
> >
> > Well, depending on what you're doing, there are several answers:
> >
> > 1)  If you get the foreign object from the collObjects list, then you
> > don't need to do anything (because the object is still in the list and
> > has been changed in place.)
> >
> > 2)  If you're adding or removing foreign objects, then you may need to
> > reload the list.
> >   a)   The easiest way is to use the addObject() and removeObject()
> > methods in the user object.  This automatically adds/removes the foreign
> > object from the collObjects list and no reload is necessary.
> 
> Thanks Peter. Do these methods actually add / remove a row from the table
> immediately, or do they only affect the collection-object? If only the
> collObjects is altered, how do I invoke the actual change to the database?
> 
> Marc
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org
> 



AW: AW: how to tell the bo that a foreign collection must bereloaded?

Posted by Marc Lustig <ml...@marclustig.com>.
> 4)  Deleting objects is a little trickier.  Essentially, you need to
> remove the object from collObjects, and THEN delete the object.  (The
> save() function on the parent object will NOT delete removed objects.)
> 
> So the code fragment might look like:
> 
> Person p = PersonPeer.retrieveByPK(114);
- PersonAddresss addrList = p.getPersonAddresss();
+ List addrList = p.getPersonAddresss();
> PersonAddress addr1 = addrList.get(0);
                                     ^
- addr1.remove(0);
+ addrList.remove(0);
> PersonAddressPeer.doDelete(addr1);

Many thanks. Your code would compile only with the above changes.
But still it would not work.
Am I right that this is the way you are doing the removal?

PersonAddress addr1 = getObjectToRemove
Person p = PersonPeer.retrieveByPK(114);
List addrList = p.getPersonAddresss();
addrList.remove(addrList.indexOf(addr1));
PersonAddressPeer.doDelete(addr1);