"objectForPK()" executes a query though unless the object is already cached. So 
this is not entirely the same as creating a fault. 

Andrus



> On Oct 26, 2025, at 6:54 AM, Ricardo Parada <[email protected]> wrote:
> 
> Yes, Cayenne.objectForPK() works. 
> 
> I did not know about the Cayenne class. 
> 
> Thank you,
> Ricardo Parada
> 
> 
>> 
>> On Oct 26, 2025, at 4:58 AM, Jurgen Doll <[email protected]> wrote:
>> 
>> I'm wondering if you couldn't just use:
>> 
>> Cayenne.objectForPK( context, objClass, pkValue );
>> regards Jurgen
>>> On Oct 26 2025, at 1:13 am, Ricardo Parada <[email protected]> wrote:
>>> 
>>> I put together this utility method. It seems to work:
>>> public static <T extends Persistent> T faultForPrimaryKeyValue(
>>> Class<T> type, Object pkValue, ObjectContext context) {
>>> if (type == null || pkValue == null || context == null) {
>>> throw new IllegalArgumentException("type, pkValue, and context must not be 
>>> null");
>>> }
>>> 
>>> EntityResolver resolver = context.getEntityResolver();
>>> ObjEntity objEntity = resolver.getObjEntity(type);
>>> if (objEntity == null) {
>>> throw new IllegalArgumentException("No ObjEntity found for class " + 
>>> type.getName());
>>> }
>>> 
>>> DbEntity dbEntity = objEntity.getDbEntity();
>>> if (dbEntity == null) {
>>> throw new IllegalStateException("No DbEntity mapped for ObjEntity " + 
>>> objEntity.getName());
>>> }
>>> 
>>> if (dbEntity.getPrimaryKeys().size() != 1) {
>>> throw new IllegalStateException(
>>> "Entity " + dbEntity.getName() + " must have exactly one primary key 
>>> column");
>>> }
>>> 
>>> // Build the ObjectId
>>> DbAttribute pkAttr = dbEntity.getPrimaryKeys().iterator().next();
>>> String pkName = pkAttr.getName();
>>> String entityName = objEntity.getName();
>>> ObjectId objectId = ObjectId.of(entityName, pkName, pkValue);
>>> 
>>> // Check if object already registered
>>> Persistent existing = (Persistent) 
>>> context.getGraphManager().getNode(objectId);
>>> if (existing != null) {
>>> @SuppressWarnings("unchecked")
>>> T casted = (T) existing;
>>> return casted;
>>> }
>>> 
>>> // Create a new hollow (fault) object
>>> ClassDescriptor descriptor = resolver.getClassDescriptor(entityName);
>>> Persistent obj = (Persistent) descriptor.createObject();
>>> 
>>> obj.setObjectContext(context);
>>> obj.setObjectId(objectId);
>>> obj.setPersistenceState(PersistenceState.HOLLOW);
>>> 
>>> context.getGraphManager().registerNode(objectId, obj);
>>> @SuppressWarnings("unchecked")
>>> T casted = (T) obj;
>>> return casted;
>>> }
>>> 
>>> 
>>>>> On Oct 25, 2025, at 4:34 PM, Ricardo Parada <[email protected]> wrote:
>>>> 
>>>> 
>>>> Awesome, thank you for that code Andrus.
>>>> 
>>>> Ricardo Parada
>>>> 
>>>> 
>>>> 
>>>>> 
>>>>> On Oct 25, 2025, at 12:16 PM, Andrus Adamchik <[email protected]> wrote:
>>>>> 
>>>>> Hi Ricardo,
>>>>> 
>>>>> A rough equivalent used by Cayenne internally is this:
>>>>> 
>>>>> ObjectId oid = ObjectId.of(entityName, "PK_COL_NAME", id);
>>>>> ClassDescriptor descriptor = 
>>>>> context.getEntityResolver().getClassDescriptor(entityName);
>>>>> 
>>>>> Persistent o = (Persistent) descriptor.createObject();
>>>>> o.setObjectContext(context);
>>>>> o.setObjectId(oid);
>>>>> o.setPersistenceState(PersistenceState.HOLLOW);
>>>>> 
>>>>> context.getGraphManager().registerNode(oid, o);
>>>>> 
>>>>> Thanks,
>>>>> Andrus
>>>>> 
>>>>> 
>>>>>> On Oct 24, 2025, at 9:05 AM, Ricardo Parada <[email protected]> 
>>>>>> wrote:
>>>>>> Good morning,
>>>>>> I’m looking for the equivalent of the following EOF code:
>>>>>> var obj = EOUtilities.faultWithPrimaryKeyValue(editingContext, 
>>>>>> entityName, id);
>>>>>> What I have so far is:
>>>>>> var objClass = 
>>>>>> oc.getEntityResolver().getClassDescriptor(objEntityName).getObjectClass();
>>>>>> var obj = SelectById.query(objClass, id)
>>>>>> .localCache()
>>>>>> .selectOne(oc);
>>>>>> But it seems to fetch the object the first time even though the object 
>>>>>> already exists in the object context.
>>>>>> Thanks in advance,
>>>>>> Ricardo Parada
>>> 
>> 

Reply via email to