Re: Primary Keys in Cayenne

From: Dave Slusher (dslushe..uarded.net)
Date: Wed Dec 04 2002 - 12:17:51 EST

  • Next message: Andrus Adamchik: "Re: Primary Keys in Cayenne"

    Andrei,

    Thanks for the help. I thought I understood this, but when I went to
    implement it I am still getting the autoincrement sequences and stuff after
    I added an ObjAttribute. I'll paste in some examples from the schema:

    Originally created from reverse engineering a mysql database:

    ...
    <db-entity name="hosts">
    <db-attribute name="domain_ref" type="INTEGER" isMandatory="true" length="10"/>
    <db-attribute name="dst_cw" type="TINYINT" isMandatory="true" length="3"/>
    <db-attribute name="entity" type="SMALLINT" isMandatory="true" length="5"/>
    <db-attribute name="host_class" type="SMALLINT" isMandatory="true" length="5"/>
    <db-attribute name="id" type="BIGINT" isPrimaryKey="true"
    isMandatory="true" length="20"/>
    <db-attribute name="spoofagentid" type="INTEGER" isMandatory="true"
    length="11"/>
    <db-attribute name="src_tw" type="TINYINT" isMandatory="true" length="3"/>
    <db-attribute name="tl_mult" type="SMALLINT" isMandatory="true" length="5"/>
    <db-attribute name="userdef" type="TINYINT" isMandatory="true" length="3"/>
    <db-attribute name="utime" type="INTEGER" length="10"/>
    <db-attribute name="watchlist" type="TINYINT" isMandatory="true" length="3"/>
    <db-attribute name="xlateip" type="INTEGER" length="10"/>
    </db-entity>

    ...
    <obj-entity name="Hosts" className="Hosts" dbEntityName="hosts">
    <obj-attribute name="domainRef" type="java.lang.Integer"
    db-attribute-name="domain_ref"/>
    <obj-attribute name="dstCw" type="java.lang.Byte" db-attribute-name="dst_cw"/>
    <obj-attribute name="entity" type="java.lang.Short"
    db-attribute-name="entity"/>
    <obj-attribute name="hostClass" type="java.lang.Short"
    db-attribute-name="host_class"/>
    <obj-attribute name="spoofagentid" type="java.lang.Integer"
    db-attribute-name="spoofagentid"/>
    <obj-attribute name="srcTw" type="java.lang.Byte" db-attribute-name="src_tw"/>
    <obj-attribute name="tlMult" type="java.lang.Short"
    db-attribute-name="tl_mult"/>
    <obj-attribute name="userdef" type="java.lang.Byte"
    db-attribute-name="userdef"/>
    <obj-attribute name="utime" type="java.lang.Integer"
    db-attribute-name="utime"/>
    <obj-attribute name="watchlist" type="java.lang.Byte"
    db-attribute-name="watchlist"/>
    <obj-attribute name="xlateip" type="java.lang.Integer"
    db-attribute-name="xlateip"/>
    </obj-entity>

    The primary key is "id", which did not have a ObjAttribute automatically
    created. Out of the box, it was getting autoincrement support, sequences
    and such created for Oracle. After seeing your e-mail, I added this line to
    the ObjEntity tag in the map:

    <obj-attribute name="id" type="java.lang.Integer" db-attribute-name="id"/>

    This is what you were saying, right? If there exists an ObjAttribute mapped
    to the DbAttribute, it shouldn't generate the support. However, after
    adding in that line I'm not seeing an effect. I get the identical SQL out
    of it before and after adding that line. Is there more I need to do in the
    data map to suppress this?

    Thanks for the help. Cayenne really rocks, shortening an odious cross-db
    support issue we have by days if not weeks. Kick ass!

    d

    At 03:49 PM 12/4/2002 +0000, Andrus Adamchik wrote:
    >Hi Dave,
    >Basically, autogeneration (autoincrement) of PKs will be used when all
    >other options are exhausted. Options are listed here:
    >http://objectstyle.org/cayenne/userguide/design/autopk.html#pk
    >and are basically:
    >1. Meaningful Primary Key
    >2. Primary Key Derived From Realtionship
    >3. Automatic
    >So to switch a certain key from type (3) to type (1), you can manually add
    >an ObjAttribute in ObjEntity that maps to a PK DbAttribute. If such
    >ObjAttribute is present, then autogeneration will not occur.
    >Andrus



    This archive was generated by hypermail 2.0.0 : Wed Dec 04 2002 - 12:20:59 EST