You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Øyvind Harboe <oy...@zylin.com> on 2006/07/10 22:00:27 UTC

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

Re: UUID as primary key

Posted by Øyvind Harboe <oy...@zylin.com>.
On 7/12/06, Andrus Adamchik <an...@objectstyle.org> wrote:
> Yes, there are a few ways of doing this. The cleanest one is this:
>
> object.getObjectId().getReplacementIdMap().put(Treatment.BLA_BLA_PK,
> UUID.randomUUID());

Thanks! Works great AFAICT.

Note that I had to remove the pk ObjAttribute as it is a required
DbAttribute and causes a validation failure if I don't remove it.

> BTW, this code can be added to "setPersistenceState()" to be executed
> when persistence state is changed to NEW.

I'm not comfortable with this, because the UUID should only be set
once and I haven't figured out how to tell when to execute the code
above if I override setPersistenceState().



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

Re: UUID as primary key

Posted by Andrus Adamchik <an...@objectstyle.org>.
Yes, there are a few ways of doing this. The cleanest one is this:

object.getObjectId().getReplacementIdMap().put(Treatment.BLA_BLA_PK,  
UUID.randomUUID());

BTW, this code can be added to "setPersistenceState()" to be executed  
when persistence state is changed to NEW.

Andrus


On Jul 10, 2006, at 4:00 PM, Øyvind Harboe wrote:

> 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
>


Re: UUID as primary key

Posted by Øyvind Harboe <oy...@zylin.com>.
On 7/13/06, Andrus Adamchik <an...@objectstyle.org> wrote:
>
> On Jul 13, 2006, at 12:32 PM, Øyvind Harboe wrote:
>
> > On 7/12/06, Gentry, Michael (Contractor)
> > <mi...@fanniemae.com> wrote:
> >> 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());
> >> }
> >
> > Hmmm... I tried that, but setPersistanceState() is invoked multilple
> > times with state=NEW and thus I'm changing the UUID each time. That
> > can't be right.
>
> No it can't - it should only be invoked once per object.

Note that my object is in a child context when I perform a commit.


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

Re: UUID as primary key

Posted by Øyvind Harboe <oy...@zylin.com>.
On 7/22/06, Andrus Adamchik <an...@objectstyle.org> wrote:
>
> I see. That makes sense - each DataContext in a nested context chain
> would create its own instance of object and transition it through the
> lifecycle. So technically it is still called once per object
> instance. However the fact that ID can be passed from a peer object
> in the upstream context requires an extra check. E.g.:
>
>   public void setPersistenceState(int state) {
>     super.setPersistenceState(state);
>
>     if (state == PersistenceState.NEW && getObjectId() == null) {
>       setId(UUID.randomUUID());
>     }
>   }


That didn't work for me. getObjectId()  is never null when
setPersistanceState() is called, AFAICT.

Otherwise, it seems to work fine though.



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

Re: UUID as primary key

Posted by Andrus Adamchik <an...@objectstyle.org>.
I see. That makes sense - each DataContext in a nested context chain  
would create its own instance of object and transition it through the  
lifecycle. So technically it is still called once per object  
instance. However the fact that ID can be passed from a peer object  
in the upstream context requires an extra check. E.g.:

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

    if (state == PersistenceState.NEW && getObjectId() == null) {
      setId(UUID.randomUUID());
    }
  }

Andrus

On Jul 21, 2006, at 5:35 PM, Øyvind Harboe wrote:

> On 7/13/06, Andrus Adamchik <an...@objectstyle.org> wrote:
>>
>>
>> On Jul 13, 2006, at 12:32 PM, Øyvind Harboe wrote:
>>
>> > On 7/12/06, Gentry, Michael (Contractor)
>> > <mi...@fanniemae.com> wrote:
>> >> 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());
>> >> }
>> >
>> > Hmmm... I tried that, but setPersistanceState() is invoked  
>> multilple
>> > times with state=NEW and thus I'm changing the UUID each time. That
>> > can't be right.
>>
>> No it can't - it should only be invoked once per object.
>
>
> As far as I can tell in the debugger setPersistanceState(state=NEW)  
> is being
> invoked multiple times..
>
> First when I create the object and then later on when I commit 
> (there are
> child contexts involved, and I first commit to parent, then to the
> database):
>
>
>        if (dataObject.getObjectId() == null) {
>            dataObject.setObjectId(new ObjectId(objEntity.getName 
> ()));  //
> DataContext.registerNewObject(DataObject) line: 920
>        }
>
>
>
> Treatment(Workaround).setPersistenceState(int) line: 31
> DataContext.registerNewObject(DataObject) line: 920
> ChildDiffLoader.nodeCreated(Object) line: 110
> NodeCreateOperation.apply(GraphChangeHandler) line: 73
> CompoundDiff.apply(GraphChangeHandler) line: 133
> ObjectStoreGraphDiff.apply(GraphChangeHandler) line: 155
> DataContext.onContextFlush(ObjectContext, GraphDiff, boolean) line:  
> 1215
> DataContext.onSync(ObjectContext, GraphDiff, int) line: 1194
> DataContext.flushToParent(boolean) line: 1261
> DataContext.commitChanges() line: 1165
> EditTreatment$Enhance_217(EditTreatment).submit(IRequestCycle)  
> line: 142
>
>
>
> -- 
> Øyvind Harboe
> http://www.zylin.com


Re: UUID as primary key

Posted by Øyvind Harboe <oy...@zylin.com>.
On 7/13/06, Andrus Adamchik <an...@objectstyle.org> wrote:
>
>
> On Jul 13, 2006, at 12:32 PM, Øyvind Harboe wrote:
>
> > On 7/12/06, Gentry, Michael (Contractor)
> > <mi...@fanniemae.com> wrote:
> >> 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());
> >> }
> >
> > Hmmm... I tried that, but setPersistanceState() is invoked multilple
> > times with state=NEW and thus I'm changing the UUID each time. That
> > can't be right.
>
> No it can't - it should only be invoked once per object.


As far as I can tell in the debugger setPersistanceState(state=NEW) is being
invoked multiple times..

First when I create the object and then later on when I commit(there are
child contexts involved, and I first commit to parent, then to the
database):


        if (dataObject.getObjectId() == null) {
            dataObject.setObjectId(new ObjectId(objEntity.getName()));  //
DataContext.registerNewObject(DataObject) line: 920
        }



Treatment(Workaround).setPersistenceState(int) line: 31
DataContext.registerNewObject(DataObject) line: 920
ChildDiffLoader.nodeCreated(Object) line: 110
NodeCreateOperation.apply(GraphChangeHandler) line: 73
CompoundDiff.apply(GraphChangeHandler) line: 133
ObjectStoreGraphDiff.apply(GraphChangeHandler) line: 155
DataContext.onContextFlush(ObjectContext, GraphDiff, boolean) line: 1215
DataContext.onSync(ObjectContext, GraphDiff, int) line: 1194
DataContext.flushToParent(boolean) line: 1261
DataContext.commitChanges() line: 1165
EditTreatment$Enhance_217(EditTreatment).submit(IRequestCycle) line: 142



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

Re: UUID as primary key

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Jul 13, 2006, at 12:32 PM, Øyvind Harboe wrote:

> On 7/12/06, Gentry, Michael (Contractor)  
> <mi...@fanniemae.com> wrote:
>> 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());
>> }
>
> Hmmm... I tried that, but setPersistanceState() is invoked multilple
> times with state=NEW and thus I'm changing the UUID each time. That
> can't be right.

No it can't - it should only be invoked once per object.

Andrus

Re: UUID as primary key

Posted by Øyvind Harboe <oy...@zylin.com>.
On 7/12/06, Gentry, Michael (Contractor) <mi...@fanniemae.com> wrote:
> 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());
> }

Hmmm... I tried that, but setPersistanceState() is invoked multilple
times with state=NEW and thus I'm changing the UUID each time. That
can't be right.


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

RE: UUID as primary key

Posted by "Gentry, Michael (Contractor)" <mi...@fanniemae.com>.
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: oyvindharboe@gmail.com [mailto:oyvindharboe@gmail.com] On Behalf Of Øyvind Harboe
Sent: Monday, July 10, 2006 4:00 PM
To: cayenne-user@incubator.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