cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrus Adamchik <and...@objectstyle.org>
Subject Re: Specifying PK when creating a new object in DB generated PK mode
Date Tue, 06 May 2008 08:30:07 GMT

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

Mime
View raw message