You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Scott Anderson <sa...@airvana.com> on 2008/05/06 00:07:30 UTC

RE: Specifying PK when creating a new object in DB generated PK mode

I was looking through my old emails, and I realized that I still haven't
found a resolution to this. Andrus vaguely mentioned that there might be
a good reason why you aren't allowed to, but I wasn't able to find that
reason.

So, to recap, here's my scenario:

I am normalizing the schema for a request ticket database. I wrote an
importer using Cayenne, but I wasn't able to determine how to specify
the PK for an object when that type is set to have the PK assigned by
the database. 

The PK is not meaningful (in that its value has no inherent meaning),
but it is used as the unique token to identify each REQUEST - after all,
it is the PK - and I therefore must keep this value consistent from one
schema to the next.

I need to have the DB generate PKs for the production code when creating
new requests to avoid PK collision, as we have two replicated MASTER
servers, one for each country we are located in; the anti-collision
strategy is to have PKs generated by the database where PK MOD 10 is a
different value on each MASTER.

RE: Specifying PK when creating a new object in DB generated PK mode

Posted by Scott Anderson <sa...@airvana.com>.
Works great; thanks Andrus.

-----Original Message-----
From: Andrus Adamchik [mailto:andrus@objectstyle.org] 
Sent: Tuesday, May 06, 2008 4:30 AM
To: user@cayenne.apache.org
Subject: Re: Specifying PK when creating a new object in DB generated PK
mode

How about using a brute force approach. You have a DataMap that  
contains DbEntities with "Database Generated" PK. Fine... but for  
import purposes you need to suppress this behavior, so do something  
like that on startup:

DataDomain domain = Configuration.getSharedConfiguration().getDomain();
for(DbEntity e : domain.getEntityResolver().getDbEntities()) {
    for(DbAttribute a : e.getPrimaryKeys()) {
       a.setGenerated(false);
    }
}


Andrus

Re: Specifying PK when creating a new object in DB generated PK mode

Posted by Andrus Adamchik <an...@objectstyle.org>.
On May 6, 2008, at 1:07 AM, Scott Anderson wrote:

> I was looking through my old emails, and I realized that I still  
> haven't
> found a resolution to this. Andrus vaguely mentioned that there  
> might be
> a good reason why you aren't allowed to, but I wasn't able to find  
> that
> reason.
>
> So, to recap, here's my scenario:
>
> I am normalizing the schema for a request ticket database. I wrote an
> importer using Cayenne, but I wasn't able to determine how to specify
> the PK for an object when that type is set to have the PK assigned by
> the database.
>
> The PK is not meaningful (in that its value has no inherent meaning),
> but it is used as the unique token to identify each REQUEST - after  
> all,
> it is the PK - and I therefore must keep this value consistent from  
> one
> schema to the next.
>
> I need to have the DB generate PKs for the production code when  
> creating
> new requests to avoid PK collision, as we have two replicated MASTER
> servers, one for each country we are located in; the anti-collision
> strategy is to have PKs generated by the database where PK MOD 10 is a
> different value on each MASTER.

How about using a brute force approach. You have a DataMap that  
contains DbEntities with "Database Generated" PK. Fine... but for  
import purposes you need to suppress this behavior, so do something  
like that on startup:

DataDomain domain = Configuration.getSharedConfiguration().getDomain();
for(DbEntity e : domain.getEntityResolver().getDbEntities()) {
    for(DbAttribute a : e.getPrimaryKeys()) {
       a.setGenerated(false);
    }
}


Andrus