You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Joseph Schmidt <jo...@yahoo.com> on 2009/04/08 00:26:52 UTC

"Can't get primary key from temporary id" :(

I'm getting the following error:
------------
Can't get primary key from temporary id
------------

while trying to access the 'id' of a newly created object entity A.
I need this 'id' to write the value together with some other fields in some other object entity B inside the same transaction. Between A and B there's no relationship (I'm using MySQL as a DB and CayenneM5).

Is there's some API I'm missing or using wrongly for this scenario? 
I used DataObjectUtils.pkForObject() to get the 'id'.

Or how to solve this scenario - it is pretty common to need inside the sam transaction values from newly created entities - e.g. for audit/logging or other purposes.

thanks,
Joseph.


      


Re: "Can't get primary key from temporary id" :(

Posted by Aristedes Maniatis <ar...@ish.com.au>.
On 08/04/2009, at 8:26 AM, Joseph Schmidt wrote:

> Can't get primary key from temporary id
> ------------
>
> while trying to access the 'id' of a newly created object entity A.
> I need this 'id' to write the value together with some other fields  
> in some other object entity B inside the same transaction. Between A  
> and B there's no relationship (I'm using MySQL as a DB and CayenneM5).


The primary key doesn't exist until the record is written to the  
database, so you just can't do this. You have two options:

* write the record to database, fetch it back again and then you'll  
have the primary key
* create a relationship between A and B

If you find yourself using the primary keys directly very often, you  
are probably not using Cayenne in the manner it was intended. The idea  
is that it removes you from thinking about database-centric issues  
like primary keys and just think about objects and how they relate to  
each other. Mostly.


Ari




-------------------------->
ish
http://www.ish.com.au
Level 1, 30 Wilson Street Newtown 2042 Australia
phone +61 2 9550 5001   fax +61 2 9550 4001
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A