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.createPermIdsForObjEntity(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
Andrus Adamchik schrieb:
> Do not map "id" as as an ObjAttribute in the Modeler - this should
> address the first problem. If you need access to id, you can create a
> getId() method in ExtResources class that relies on DataObjectUtils
> (see below).
>
> Regarding the second problem. By default id is not known until object
> is committed. But you can generate it yourself and stick in the
> ObjectId. You can implement "getId()" method mentioned above like
> that (assuming Cayenne 1.2) -
>
> public int getId() {
> ObjectId oid = getObjectId();
>
> if(oid.isTemporary() && !id.isReplacementIdAttached()) {
> // generate id here
> updateId();
> }
>
> // now we can safely use DataObjectUtils
> return DataObjectUtils.intPKForObject(this);
> }
>
> void updateId() {
> DbEntity entity = getDataContext().getEntityResolver
> ().lookupDbEntity(this);
> DataNode node = getDataContext().lookupDataNode(entity.getDataMap ());
>
> Object pk = node.getAdapter().getPkGenerator
> ().generatePkForDbEntity(node, entity);
> getObjectId().getReplacementIdMap().put
> ("<actual_pk_attribute_name>", pk);
> }
>
> Andrus
>
>
> On Jan 15, 2006, at 11:05 AM, Chronos wrote:
>
>> Hey!
>>
>> I´ve got two little problems, I hope somebody can help me to solve it:
>>
>> 1. I will generate a new entry:
>>
>> ExtResources newRes = (ExtResources)
>> ctxt.createAndRegisterNewObject(ExtResources.class);
>> newRes.setContent(content);
>> newRes.setContent(content);
>> ...
>> ctxt.commitChanges();
>>
>>
>> But then I get an exception:
>> "Validation failure for
>> edu.unika.aifb.webex.webexauthor.server.model.ExtResources.id: "id" is
>> required",
>> but ID is my PK, so how can I add this entry?
>>
>> 2. When I add this entry, is there a possibility to get the ID or
>> must I
>> use DataObjectUtils.objectForPK etc.?
>>
>> Thans for your help,
>>
>> gr
>>
>> PK
>>
>
>
This archive was generated by hypermail 2.0.0 : Mon Jan 16 2006 - 04:21:20 EST