On Thu, 2002-01-24 at 06:04, Eric Dobbs wrote:
>
> 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 save() returns void
>
> 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.
but that's exactly what doesn't work. maybe it's because of n:m?
what i really do is the following.
this doesn't work:
Artist artist1 = new Artist();
artist1.setName("Primus");
artist1.save();
Website website1 = new Website();
website1.setName("Primus Online");
website1.setUrl("http://www.primussucks.com");
website1.save();
ArtistHasWebsite link = new ArtistHasWebsite();
link.setArtist(artist1);
link.setWebsite(website1);
link.save(); //Column 'ARTIST_ID' cannot be null
this does work:
Artist artist1 = new Artist();
artist1.setName("Primus");
artist1.save();
Website website1 = new Website();
website1.setName("Primus Online");
website1.setUrl("http://www.primussucks.com");
website1.save();
Artist artist2 = ArtistFactory.findByName(artist1.getName());
Website website2 = WebsiteFactory.findByUrl(website1.getUrl());
ArtistHasWebsite link = new ArtistHasWebsite();
link.setArtist(artist2);
link.setWebsite(website2);
link.save();
tried defaultIdMethod native and idBroker by now.
bye
fabian
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>