Trying not to think in terms of tables

0 views
Skip to first unread message

jqdu...@gmail.com

unread,
Nov 2, 2009, 2:19:42 PM11/2/09
to transf...@googlegroups.com
In a previous question to this list, I mentioned that I'm working on an Address object that is composed into both a Company object and a User object. I'm currently working with the Company>Address relationship and building a couple of forms to manage Companies and Addresses.

I have a form intended to modify Addresses associated with a single Company (one company can have multiple addresses). It contains both a CompanyID and AddressID. My first exercise in saving an Address, did so without regard of the CompanyID. Essentially, the handler took the AddressID, located the correct Transfer Object, cloned it, populated, validated and saved the Address without any knowledge of a Company. This seemed to be working fine.

However, now I'm wondering if I should have started by retrieving the Company and accessing the Address via the generated o2m methods. And if so, I'll probably have a couple of follow up questions. :)

TIA
Jason

Tom McNeer

unread,
Nov 2, 2009, 2:46:54 PM11/2/09
to transf...@googlegroups.com
You're headed in the right direction, looking to use the composed relationships. But ...

On Mon, Nov 2, 2009 at 2:19 PM, <jqdu...@gmail.com> wrote:
My first exercise in saving an Address, did so without regard of the CompanyID. Essentially, the handler took the AddressID, located the correct Transfer Object, cloned it, populated, validated and saved the Address without any knowledge of a Company. This seemed to be working fine.

... if you are working with a database record that currently exists and has the Company relationship attached, you don't need to deal with the Company at all. The Address object knows what its parent Company is, and if you don't need to change it, then you don't have to mess with it at all.

For a new record, you'd need to use the CompanyID to retrieve the Company object, then do Address.setParentCompany(myCompany), or something like that, before saving the Address record.

But if the Address already has a Company, most times you can just ignore it. (Unless of course, you actually need the Company for another reason.)

But why did you need to clone the object?


Thanks,

Tom

Tom McNeer
MediumCool
http://www.mediumcool.com
1735 Johnson Road NE
Atlanta, GA 30306
404.589.0560

Jason Durham

unread,
Nov 2, 2009, 3:10:00 PM11/2/09
to transf...@googlegroups.com
Ah... I hadn't considered that the Address was already aware of its parent.
Dealing with the Address object directly was less painful. :)

With regard to cloning...  isn't that the only way to do it? :o)   My
validation mechanism occurs once the Transfer Object has been populated.  I
don't want the (potentially) invalid form data to dirty the cache until it
has been validated.  If the object doesn't validate, I pass the entire
object back to the form to output errors.

Jason


On Mon, Nov 2, 2009 at 2:06 PM, Jason Durham <ja...@durhamcons.com> wrote:
Ah... I hadn't considered that the Address was already aware of its parent.  Dealing with the Address object directly was less painful. :)

With regard to cloning...  isn't that the only way to do it? :o)   My validation mechanism occurs once the Transfer Object has been populated.  I don't want the (potentially) invalid form data to dirty the cache until it has been validated.  If the object doesn't validate, I pass the entire object back to the form to output errors.

Jason
--
Jason Durham



--
Jason Durham

Jason Durham

unread,
Nov 2, 2009, 4:17:03 PM11/2/09
to transf...@googlegroups.com
Ah nuts.... I have a m2m between Company and Address (with a Join table).  Thus, Address isn't directly aware of Company.  My Address gets inserted but the join table doesn't get updated.  What's the recommended way of handling this?  Should my Decorator override the save() method in order to update the Join table... or will Transfer maintain the Join table if access the Address through the Company?

TIA
Jason

Tom McNeer

unread,
Nov 2, 2009, 4:28:26 PM11/2/09
to transf...@googlegroups.com
On Mon, Nov 2, 2009 at 3:10 PM, Jason Durham <jqdu...@gmail.com> wrote:
 My
validation mechanism occurs once the Transfer Object has been populated.  I
don't want the (potentially) invalid form data to dirty the cache until it
has been validated.

Perfectly reasonable. I just didn't follow your logic before.

Ah nuts.... I have a m2m between Company and Address (with a Join table).  Thus, Address isn't directly aware of Company.  My Address gets inserted but the join table doesn't get updated.  What's the recommended way of handling this?  Should my Decorator override the save() method in order to update the Join table... or will Transfer maintain the Join table if access the Address through the Company?

Depends on your configuration, but probably, yes, if the many-to-many is set on the Company. I think if you go through the docs on Managing Relationships, you'll see how to set this up.

--

Mark Mandel

unread,
Nov 2, 2009, 4:44:33 PM11/2/09
to transf...@googlegroups.com
Transfer will update the join table if you modify the relationship b/w Company and Address and save the Company.

Mark


On Tue, Nov 3, 2009 at 8:28 AM, Tom McNeer <tmc...@gmail.com> wrote:
or will Transfer maintain the Join table if access the Address through the Company?



--
E: mark....@gmail.com
T: http://www.twitter.com/neurotic
W: www.compoundtheory.com

Jason Durham

unread,
Nov 2, 2009, 6:02:20 PM11/2/09
to transf...@googlegroups.com
These are the sequence of events in my handler...

Form is submitted....
Create new Address object
Populate Address object
Validate Address object
Retrieve Company object
Verify Address exists in Company and copy the new Address over existing [ oTempAddress.copyValuesTo(oCompany.getAddress(addressIndex) ]
Save Company

If I dump oCompany.getAddress(addressIndex).getMemento() just before the save of the company, I see the new data from the form.  However, oCompany.save() doesn't seem to save the Address I just modified.  I'm gonna RTFM again just in case I missed something. :)

Jason

Mark Mandel

unread,
Nov 2, 2009, 6:04:36 PM11/2/09
to transf...@googlegroups.com
What does your company.save() do?

Mark

Jason Durham

unread,
Nov 3, 2009, 10:00:00 AM11/3/09
to transf...@googlegroups.com
Thanks for [subtly] pointing out the problem. cascadeSave() did the trick. :)

I have the inserts and updates working well. I'm dealing with an
event to remove an address. Is my logic correct here?

//Get Address and Company
oAddress = AddressService.get( addressID );
oCompany = CompanyService.get( rc.companyID );

//Remove address from Company
oCompany.removeAddress( oAddress );

//Save company
oCompany.cascadeSave();

This is removing the address from the Join table but not removing the
address from the database completely. Do I need to call
Transfer.delete() on the address after save of the Company?

Thanks again.

On Mon, Nov 2, 2009 at 5:07 PM, Jason Durham <ja...@durhamcons.com> wrote:
> I'm just using Transfer.save().
>
> Jason

Mark Mandel

unread,
Nov 3, 2009, 4:05:32 PM11/3/09
to transf...@googlegroups.com
Yes, to delete, you must call delete()

Mark
Reply all
Reply to author
Forward
0 new messages