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 <amedeomant...@me.com> 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 via email to