> Thanks for your help, but there are some mistakes in it:
Oh please, I just outlined the solution, leaving such minor things
like producing the code that actually compiles as an exercise for the
users ;-)
Now I tried this code and it works fine. BTW, the exception that you
posted has different origins (see PrimaryKeyHelper mention - it is
not involved in any of the code below). All the same - you will need
to doublecheck that you have your PK support tables properly setup.
My guess from the error message is that you have your PK generation
setup at least once, i.e. you followed this procedure at some point:
http://objectstyle.org/cayenne/modelerguide/common-tasks/pksupport.html
however 'ext_resources' table was added later and AUTO_PK_SUPPORT
wasn't updated. So you may need to repeat the steps outlined in the
link above, only for the 'ext_resources' table.
Andrus
On Jan 16, 2006, at 4:21 AM, Chronos wrote:
> Thanks for your help, but there are some mistakes in it:
> public int getId() throws Exception {
> ObjectId oid = getObjectId();
>
> if(oid.isTemporary() && !oid.isReplacementIdAttached())
> { //should be oid not id
> // generate id here
> updateId();
> }
>
> // now we can safely use DataObjectUtils
> return DataObjectUtils.intPKForObject(this);
> }
>
> void updateId() throws Exception {
> DbEntity entity = getDataContext().getEntityResolver
> ().lookupDbEntity(this);
> DataNode node = getDataContext().lookupDataNode
> (entity.getDataMap ());
>
> Object pk = node.getAdapter().getPkGenerator
> ().generatePkForDbEntity(node, entity); // throws Exception!!
> getObjectId().getReplacementIdMap().put
> ("<actual_pk_attribute_name>", pk);
> }
>
> Now getId works fine, but updateId doesn´t work:
> "
> - LOCK TABLES AUTO_PK_SUPPORT WRITE
> - SELECT NEXT_ID FROM AUTO_PK_SUPPORT WHERE TABLE_NAME =
> 'ext_resources'
> - UNLOCK TABLES
> org.objectstyle.cayenne.CayenneRuntimeException: [v.1.2M9 December
> 15 2005] Error generating PK: No rows for 'ext_resources'
> at
> org.objectstyle.cayenne.access.PrimaryKeyHelper.createPermIdsForObjEnt
> ity(PrimaryKeyHelper.java:193)
> ...
> "
> Code:
> public class ExtResources extends
> org.objectstyle.cayenne.CayenneDataObject {
> ...
>
> public static final String ID_PK_COLUMN = "id";
>
> public int getId() throws Exception {
> ObjectId oid = getObjectId();
>
> if(oid.isTemporary() && !oid.isReplacementIdAttached()) {
> // generate id here
> updateId();
> }
>
> // now we can safely use DataObjectUtils
> return DataObjectUtils.intPKForObject(this);
> }
>
> void updateId() throws Exception {
> DbEntity entity = getDataContext().getEntityResolver
> ().lookupDbEntity(this);
> DataNode node = getDataContext().lookupDataNode
> (entity.getDataMap ());
>
> Object pk = node.getAdapter().getPkGenerator
> ().generatePkForDbEntity(node, entity);
> getObjectId().getReplacementIdMap().put ("id", pk);
> }
>
>
> Any idea? Thanks for your help,
> gr
> pk
>
This archive was generated by hypermail 2.0.0 : Mon Jan 16 2006 - 19:32:43 EST