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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Øyvind Harboe
Sent: Monday, July 10, 2006 4:00 PM
To: [email protected]
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

Reply via email to