On Wednesday, January 23, 2002, at 11:01  AM, Fabian Moerchen wrote:

> i'm using the latest torque with mysql and defaultIdMethod="native".
>
> the next problem is creating objects connected with foreign keys. e.g.
> if i create a new object Artist and call save() it does not know it's
> own primary key yet, since mysql decides about the value, right? because
> when trying to tie it to another Website (they are connected via a m:n
> relationship) and saving this i get
>
> java.sql.SQLException: General error: Column 'ARTIST_ID' cannot be null
>
> if i create artist and website first, retrieve them into new objects,
> create the relationship and save then it works, but this is not very
> convenient. especially since i search for them by one of the other
> attributes (name) which is _not unique_, so it works in my small test
> setup, but:
>
> isn't there a bulletproof way to get the pk of the object just saved?

NumberKey key = artist.save();

But I think what you really want to do is something like this:

Artist artist = new Artist();
Website site = new WebSite();
//set various properties of artist and site;
artist.setWebsite(site);
artist.save();

The save() method should connect the two records for you.  And
as an added bonus, if you use a database that supports
transactions it'll add (or update) those records within a
transaction.

-Eric


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to