Re: Getting fault or object

Ricardo Parada <[email protected]> Sun, 26 Oct 2025 09:54:08 -0400
Newsgroups gmane.comp.java.cayenne.user
Message-ID <[email protected]>
Yes, Cayenne.objectForPK() works.=20

I did not know about the Cayenne class.=20

Thank you,
Ricardo Parada


>=20
> On Oct 26, 2025, at 4:58=E2=80=AFAM, Jurgen Doll <[email protected]> w=
rote:
>=20
> =EF=BB=BFI'm wondering if you couldn't just use:
>=20
> Cayenne.objectForPK( context, objClass, pkValue );
> regards Jurgen
>> On Oct 26 2025, at 1:13 am, Ricardo Parada <[email protected]> wrot=
e:
>>=20
>> 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 =3D=3D null || pkValue =3D=3D null || context =3D=3D null) {
>> throw new IllegalArgumentException("type, pkValue, and context must not b=
e null");
>> }
>>=20
>> EntityResolver resolver =3D context.getEntityResolver();
>> ObjEntity objEntity =3D resolver.getObjEntity(type);
>> if (objEntity =3D=3D null) {
>> throw new IllegalArgumentException("No ObjEntity found for class " + type=
.getName());
>> }
>>=20
>> DbEntity dbEntity =3D objEntity.getDbEntity();
>> if (dbEntity =3D=3D null) {
>> throw new IllegalStateException("No DbEntity mapped for ObjEntity " + obj=
Entity.getName());
>> }
>>=20
>> if (dbEntity.getPrimaryKeys().size() !=3D 1) {
>> throw new IllegalStateException(
>> "Entity " + dbEntity.getName() + " must have exactly one primary key colu=
mn");
>> }
>>=20
>> // Build the ObjectId
>> DbAttribute pkAttr =3D dbEntity.getPrimaryKeys().iterator().next();
>> String pkName =3D pkAttr.getName();
>> String entityName =3D objEntity.getName();
>> ObjectId objectId =3D ObjectId.of(entityName, pkName, pkValue);
>>=20
>> // Check if object already registered
>> Persistent existing =3D (Persistent) context.getGraphManager().getNode(ob=
jectId);
>> if (existing !=3D null) {
>> @SuppressWarnings("unchecked")
>> T casted =3D (T) existing;
>> return casted;
>> }
>>=20
>> // Create a new hollow (fault) object
>> ClassDescriptor descriptor =3D resolver.getClassDescriptor(entityName);
>> Persistent obj =3D (Persistent) descriptor.createObject();
>>=20
>> obj.setObjectContext(context);
>> obj.setObjectId(objectId);
>> obj.setPersistenceState(PersistenceState.HOLLOW);
>>=20
>> context.getGraphManager().registerNode(objectId, obj);
>> @SuppressWarnings("unchecked")
>> T casted =3D (T) obj;
>> return casted;
>> }
>>=20
>>=20
>>>> On Oct 25, 2025, at 4:34=E2=80=AFPM, Ricardo Parada <[email protected]> w=
rote:
>>> =EF=BB=BF
>>>=20
>>> Awesome, thank you for that code Andrus.
>>>=20
>>> Ricardo Parada
>>>=20
>>>=20
>>>=20
>>>>=20
>>>> On Oct 25, 2025, at 12:16=E2=80=AFPM, Andrus Adamchik <aadamchik@gmail.=
com> wrote:
>>>>=20
>>>> =EF=BB=BFHi Ricardo,
>>>>=20
>>>> A rough equivalent used by Cayenne internally is this:
>>>>=20
>>>> ObjectId oid =3D ObjectId.of(entityName, "PK_COL_NAME", id);
>>>> ClassDescriptor descriptor =3D context.getEntityResolver().getClassDesc=
riptor(entityName);
>>>>=20
>>>> Persistent o =3D (Persistent) descriptor.createObject();
>>>> o.setObjectContext(context);
>>>> o.setObjectId(oid);
>>>> o.setPersistenceState(PersistenceState.HOLLOW);
>>>>=20
>>>> context.getGraphManager().registerNode(oid, o);
>>>>=20
>>>> Thanks,
>>>> Andrus
>>>>=20
>>>>=20
>>>>> On Oct 24, 2025, at 9:05=E2=80=AFAM, Ricardo Parada <[email protected]=
NVALID> wrote:
>>>>> Good morning,
>>>>> I=E2=80=99m looking for the equivalent of the following EOF code:
>>>>> var obj =3D EOUtilities.faultWithPrimaryKeyValue(editingContext, entit=
yName, id);
>>>>> What I have so far is:
>>>>> var objClass =3D oc.getEntityResolver().getClassDescriptor(objEntityNa=
me).getObjectClass();
>>>>> var obj =3D 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
>>=20
>=20