RE: UUID as primary key

From: Gentry, Michael \(Contractor\) ("Gentry,)
Date: Wed Jul 12 2006 - 10:39:48 EDT

  • Next message: Andrus Adamchik: "Re: Building Expression on the fly and many to many problems"

    You could override setPersistenceState() in your Treatment class:

    public void setPersistenceState(int state)
    {
      super.setPersistenceState(state);

      // If object was just created, set PK
      if (state == PersistenceState.NEW)
        setId(UUID.randomUUID());
    }

    /dev/mrg

    -----Original Message-----
    From: oyvindharbo..mail.com [mailto:oyvindharboe@gmail.com] On Behalf Of Øyvind Harboe
    Sent: Monday, July 10, 2006 4:00 PM
    To: cayenne-use..ncubator.apache.org
    Subject: UUID as primary key

    Is there a way to set the primary key without mapping the primary key
    as an object attribute?

    Or is there a more elegant way of handling UUID as pk?

    I have lots of tables where the pk is an UUID and the only reason why
    I have to map the primary key as an object attribute is to be able to
    set the pk to an UUID.

    // Create object
    Treatment treatment = (Treatment)
    dataContext.createAndRegisterNewObject(Treatment.class);
    // set pk as UUID
    treatment.setId(UUID.randomUUID());

    Where "setId()" is the primary key mapped as an ObjAttribute.

    -- 
    Øyvind Harboe
    http://www.zylin.com
    



    This archive was generated by hypermail 2.0.0 : Wed Jul 12 2006 - 10:40:22 EDT