primary key in transaction

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

primary key in transaction

Amedeo Mantica
Hi all,

Do you know how to obtain the primary key for a newly created object ?

On EOF there were the method EOAdaptorChannel.primaryKeysForNewRowsWithEntity

mostly user by higher level Wonder method eo.primaryKeyInTransaction()

Anything similar in Cayenne ?

Thank you
Amedeo
Reply | Threaded
Open this post in threaded view
|

Re: primary key in transaction

Andrus Adamchik
Hi Amedeo,

I assume you want to know the PK before commit (as otherwise Cayenne would handle that for you transparently). In which case you have two options:

1. Map a meaningful PK, and just call a setter on the object with whatever meaningful value you might have.

2. Use PkGenerator and set the PK via Object's ObjectId. The code is a bit long, so you may need to package it in your own utility method. Something like this:

// node, pkGen and pk attribute can be located once and cached in your own PK gen code for further resue
DataNode node = serverRuntime
  .getDataDomain()
  .getDefaultNode();

PkGenerator pkGen = node
  .getAdapter()
  .getPkGenerator();

DbAttribute pkAttribute = node
  .getEntityResolver().getObjEntity(MyClass.class).getDbEntity().getPrimaryKeys().iterator().next();

// generate PK for your objects
DataObject o = ...
Object pk = pkGen.generatePk(node, pkAttribute);
o.getObjectId().getReplacementIdMap().put(pkAttribute.getName(), pk);


Andrus


> On Nov 24, 2017, at 3:16 PM, Amedeo Mantica <[hidden email]> wrote:
>
> Hi all,
>
> Do you know how to obtain the primary key for a newly created object ?
>
> On EOF there were the method EOAdaptorChannel.primaryKeysForNewRowsWithEntity
>
> mostly user by higher level Wonder method eo.primaryKeyInTransaction()
>
> Anything similar in Cayenne ?
>
> Thank you
> Amedeo

Reply | Threaded
Open this post in threaded view
|

Re: primary key in transaction

Amedeo Mantica
The option 2 looks like exactly what I want, I'm going to try
Thank you !

Amedeo

> On 27 Nov 2017, at 07:05, Andrus Adamchik <[hidden email]> wrote:
>
> Hi Amedeo,
>
> I assume you want to know the PK before commit (as otherwise Cayenne would handle that for you transparently). In which case you have two options:
>
> 1. Map a meaningful PK, and just call a setter on the object with whatever meaningful value you might have.
>
> 2. Use PkGenerator and set the PK via Object's ObjectId. The code is a bit long, so you may need to package it in your own utility method. Something like this:
>
> // node, pkGen and pk attribute can be located once and cached in your own PK gen code for further resue
> DataNode node = serverRuntime
>  .getDataDomain()
>  .getDefaultNode();
>
> PkGenerator pkGen = node
>  .getAdapter()
>  .getPkGenerator();
>
> DbAttribute pkAttribute = node
>  .getEntityResolver().getObjEntity(MyClass.class).getDbEntity().getPrimaryKeys().iterator().next();
>
> // generate PK for your objects
> DataObject o = ...
> Object pk = pkGen.generatePk(node, pkAttribute);
> o.getObjectId().getReplacementIdMap().put(pkAttribute.getName(), pk);
>
>
> Andrus
>
>
>> On Nov 24, 2017, at 3:16 PM, Amedeo Mantica <[hidden email]> wrote:
>>
>> Hi all,
>>
>> Do you know how to obtain the primary key for a newly created object ?
>>
>> On EOF there were the method EOAdaptorChannel.primaryKeysForNewRowsWithEntity
>>
>> mostly user by higher level Wonder method eo.primaryKeyInTransaction()
>>
>> Anything similar in Cayenne ?
>>
>> Thank you
>> Amedeo
>