Return-Path: Delivered-To: apmail-cayenne-user-archive@www.apache.org Received: (qmail 77044 invoked from network); 20 Apr 2009 10:00:27 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 20 Apr 2009 10:00:27 -0000 Received: (qmail 52066 invoked by uid 500); 20 Apr 2009 10:00:26 -0000 Delivered-To: apmail-cayenne-user-archive@cayenne.apache.org Received: (qmail 52014 invoked by uid 500); 20 Apr 2009 10:00:25 -0000 Mailing-List: contact user-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@cayenne.apache.org Delivered-To: mailing list user@cayenne.apache.org Received: (qmail 52004 invoked by uid 99); 20 Apr 2009 10:00:25 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 20 Apr 2009 10:00:25 +0000 X-ASF-Spam-Status: No, hits=0.4 required=10.0 tests=FORGED_YAHOO_RCVD,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of gcjcu-cayenne-user-moved1@m.gmane.org designates 80.91.229.2 as permitted sender) Received: from [80.91.229.2] (HELO ciao.gmane.org) (80.91.229.2) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 20 Apr 2009 10:00:18 +0000 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1LvqI4-0003GK-TK for user@cayenne.apache.org; Mon, 20 Apr 2009 09:59:53 +0000 Received: from p54a7effa.dip.t-dialin.net ([84.167.239.250]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 20 Apr 2009 09:59:52 +0000 Received: from joseph_schmidt71 by p54a7effa.dip.t-dialin.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 20 Apr 2009 09:59:52 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: user@cayenne.apache.org From: Joseph Schmidt Subject: Re: "Can't get primary key from temporary id" :( Date: Mon, 20 Apr 2009 11:59:30 +0200 Lines: 44 Message-ID: References: <315763.11044.qm@web111104.mail.gq1.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: p54a7effa.dip.t-dialin.net User-Agent: Thunderbird 2.0.0.21 (Windows/20090302) In-Reply-To: Sender: news X-Virus-Checked: Checked by ClamAV on apache.org >>> * write the record to database, fetch it back again and >>> then you'll have the primary key >> Than this is not the same transaction :(. > > Correct. This is a limitation of how databases work, not Cayenne. You > can't have a primary key until you write the record, unless you do > something tricky like using a nanosecond timestamp hashed with the MAC > address of your machine and the number you first thought of. Sorry but this is complete *bullshit*. There's no such database limitation. I can write in the same transaction (i.e. before I do commit), the values received from the previous inserts - even those generated by the DB. E.g. for Mysql, for the following tables: t_1(id integer autoincrement, name varchar40) t_2(id integer autoincrement, name varchar40, t_1_id integer) (see that t_1_id field is not a foreign key here) No if one is doing with DB directly or with JDBC, the following will work: -- start transaction INSERT INTO t_1 (name) VALUES ('name1'); INSERT INTO t_1 (name) VALUES ('name2'); INSERT INTO t_2 (name,t_1_id) select 'name1' , t_1.id from t_1 where t_1.name='name1'; commit; -- end transaction so the correct primary key value (generated by the DB for t_1) will be insert at the correct position of t_2.t_1_id, and this inside the same transaction - so just like using FK would. Now, my trivial question again: How to do it with Cayenne, how to get that ID to write again inside the same transaction? (I mentioned in the previous posts that I don't have for t_2.t_1_id a relationship, nor FK, because there are too many tables like that). This is an incredible common scenario in most DBs, so I suppose that it should be possible with an ORM like Cayenne too(since with SQL or JDBC is dead easy). I red the entire documentation several times but couldn't find anything how to do it in Cayenne :(. thanks in advance, Joseph.