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

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Tue May 06 2008 - 04:30:07 EDT

  • Next message: Scott Anderson: "RE: Specifying PK when creating a new object in DB generated PK mode"

    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



    This archive was generated by hypermail 2.0.0 : Tue May 06 2008 - 04:30:39 EDT