RE: [news] Re: Status of MySQL AUTO_INCREMENT Support?

From: David Norwood (david..irtuant.com)
Date: Thu Mar 31 2005 - 12:29:10 EST

  • Next message: Jamie: "Re: DataObject becomes hollow when passed through a tapestry DirectLink"

    Hi,

    I am using an Oracle DB that has auto-incremented (trigger-generated
    integer) primary keys for every table. In addition, each table has its own
    sequence. The trigger will check for a non-zero or null and use that value
    if it's passed in. In the past, I've had to modify the 2 Cayenne classes
    (below) to bypass the problem; now it seems that the PK generation is
    customizable per table.

    Question is, which is now best practice:

    1. Tell Cayenne to pass a NULL as the pk for all tables
    2. Oracle-supported auto-increment?
    3. Some other way?

    TIA,

    -david

    */ OraclePkGenerator
    public class OraclePkGenerator extends JdbcPkGenerator {
        
            private static final String _SEQUENCE_PREFIX = "pk_";
            
            // custom sequence naming - dsn 03.31.05
            private static final String _SEQUENCE_POSTFIX = "_SEQ";

    /** Returns expected primary key sequence name for a DbEntity. */
        protected String sequenceName(DbEntity entity) {

            // use custom generator if possible
            DbKeyGenerator keyGenerator = entity.getPrimaryKeyGenerator();
            if (keyGenerator != null
                &&
    DbKeyGenerator.ORACLE_TYPE.equals(keyGenerator.getGeneratorType())
                && keyGenerator.getGeneratorName() != null) {

                return keyGenerator.getGeneratorName().toLowerCase();
            }
            else {
                String entName = entity.getName();
                // String seqName = _SEQUENCE_PREFIX + entName.toLowerCase();
    -- custom sequence name 03.31.05 dsn
                String seqName = entName.toLowerCase() + _SEQUENCE_POSTFIX;

    */ JdbcPkGenerator
    public class JdbcPkGenerator implements PkGenerator {
            
        // public static final int DEFAULT_PK_CACHE_SIZE = 20; -- autogen PK's
    dsn 03.31.05
        public static final int DEFAULT_PK_CACHE_SIZE = 1;

    -----Original Message-----
    From: Andrus Adamchik [mailto:andru..bjectstyle.org]
    Sent: Wednesday, January 19, 2005 7:30 AM
    To: cayenne-use..bjectstyle.org
    Subject: [news] Re: Status of MySQL AUTO_INCREMENT Support?

    BTW, there seems to be a problem with the nightly builds in the last
    few days... I'll have to investigate what's going on...

    Andrus

    On Jan 18, 2005, at 4:03 PM, Gary Affonso wrote:

    > Thanks Andrus! I'll give it a try.
    >
    > - Gary
    >
    > On 1/18/05 12:37 PM, "Andrus Adamchik" <andru..bjectstyle.org> wrote:
    >
    >> Hi Gary,
    >>
    >> Current M1 already contains a "fake" support for autogenerated
    >> columns ...
    >> This is mostly workarounds for SQLServer to allow current Cayenne to
    >> work
    >> on top of auto-increment schema without throwing exceptions... With
    >> MySQL
    >> this worked even before M1.
    >>
    >> I promised to roll out the "real" support for a while, but it turned
    >> out
    >> to be a bigger change than originally envisioned. Mainly because of
    >> the
    >> issues with managing the object graph during commit when the some
    >> keys are
    >> not known before the commit....
    >>
    >> Now after two paragraphs of noise, here is the good news ;-)
    >> Auto-generated PKs mostly work in the last nightly build (01/18). You
    >> are
    >> more than welcome to try it out. No adapter extentions are needed.
    >> Just
    >> open your project in the Modeler, select MySQL adapter, and mark PK
    >> columns (DbAttributes) as "Auto" in the new Modeler.
    >>
    >> I say "mostly work" as there are a few things that still have to be
    >> cleaned up:
    >>
    >> (1) Modeler schema reverse/forward engineering ignores auto
    >> increment. So
    >> you have to check the checkbox manually, and also manually update the
    >> column property in the DB (very easy with phpMyAdmin)
    >>
    >> (2) One [rather uncommon] scenario is broken - If a dependent object
    >> obtains it key form another (master) object and master uses
    >> autincrement,
    >> you can't replace one master with another for dependent. This is
    >> uncommon
    >> as dependent objects are usually tied to their master for their life
    >> (e.g.
    >> UserDetail is never reassigned to a different User).
    >>
    >> There maybe other special cases that break .... I am doing some
    >> refactoring to make everything more consistent, but M2 may go out even
    >> before this is done, as IMO it is usable in its current state.
    >>
    >> Andrus
    >>
    >>
    >>> I'm evaluating Cayenne for an current project. It's a fairly large,
    >>> existing, database that uses MySQL AUTO_INCREMENT to handle the
    >>> primary
    >>> key fields. Switching to Cayenne's "pk table" just won't work,
    >>> there's
    >>> a LOT of legacy data there I just cannot change.
    >>>
    >>> But everything else in Cayenne looks great, and I'd vastly prefer an
    >>> alternative to Hibernate (which I'm using now) and it's constant
    >>> irritations (XML hell, code-generation difficulties, etc.). I swear,
    >>> using that product is like death-by-1000-irritants, particularly for
    >>> bottom-up efforts.
    >>>
    >>> I read back through the Cayenne archives and it looks like support
    >>> for
    >>> database-generated PK's (such as MySQL's AUTO_INCREMENT) is slated
    >>> for
    >>> 1.2, which is now at Milestone 1 with Milestone 2 being worked on.
    >>>
    >>> So...
    >>>
    >>> A few of questions:
    >>>
    >>> 1) What is the status for support of db-based PK generation in 1.2?
    >>> Is
    >>> it there?
    >>>
    >>> 2) Does (or will) 1.2 come with a MySQL adaptor that directly
    >>> implements
    >>> support for native-pk generation?
    >>>
    >>> 2a) Or is the plan to support the retrieval of db-generated keys in
    >>> an
    >>> architectural sense, but still leave it as an exercise for the user
    >>> to
    >>> subclass the MySQL adaptor?
    >>>
    >>> Thanks much. I'll keep dealing with Hibernate, for now, but I'm very
    >>> much hoping Cayenne can save me soon. :-)
    >>>
    >>> - Gary
    >>
    >>
    >>
    >
    >



    This archive was generated by hypermail 2.0.0 : Thu Mar 31 2005 - 12:27:53 EST