Re: Entities whose pk values come from a value in another mysql table

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Mon Apr 05 2004 - 22:10:04 EDT

  • Next message: Todd O'Bryan: "DataMap dependencies"

    Scott,

    You can roll out your own DbAdapter with a custom PkGenerator, and use
    it later in the Modeler with all your projects. Both DbAdapter and
    PkGenerator are interfaces in the org.objectstyle.cayenne.dba package.
    At some point in 1.1 or later we may simplify this procedure but for
    now follow these steps:

    1. Designate your own package name for your adapter.

    2. Copy "types.xml" file from org.objectstyle.cayenne.dba.mysql package
    to this package

    3. Implement a PkGenerator interface to follow your procedure. Likely
    you should subclass JdbcPkGenerator, and just change the queries.

    4. Implement an adapter similar to this:

    public class MyAdapter extends MySQLAdapter {
         // override PkGenerator factory method...
         protected PkGenerator createPkGenerator() {
             return new MyPkGenerator();
         }
    }

    Hope this helps.

    Andrus

    On Apr 5, 2004, at 9:48 PM, Scott Ellsworth wrote:

    > Hi, all.
    >
    > We have a schema that was designed before MySql supported autonumbers,
    > so we rolled our own uid pk generation. It is in use by a lot of
    > different apps, and cannot easily be changed, and further, the older
    > deployed apps must continue to work, so the uid sequence must be
    > updated too.
    >
    > There are several hundred tables created with sql like:
    >
    > CREATE TABLE Resource (
    > id INTEGER NOT NULL,
    > resource_type INTEGER,
    > max_reads INTEGER,
    > reads_reserved INTEGER,
    > is_reserved_for_modify CHAR(2),
    > state INTEGER,
    > barcode VARCHAR(255)
    > );
    > ALTER TABLE Resource ADD PRIMARY KEY
    > (id);
    > CREATE TABLE Resource_Seq (
    > nextval INTEGER NOT NULL
    > );
    > INSERT INTO Resource_Seq (nextval) values (1);
    >
    > Each new entry into Resource gets an id equal to Resource_Seq.nextval,
    > which is incremented as soon as it is read. (This thus emulates an
    > Oracle sequence, with a bit more effort because of the key
    > management.)
    >
    > Is there a way to tell Cayenne to do this kind of key management?
    > Pointers to examples like this would work. I am targeting Cayenne
    > 1.1.
    >
    > Scott



    This archive was generated by hypermail 2.0.0 : Mon Apr 05 2004 - 22:10:11 EDT