Re: Getting fault or object

Jurgen Doll <[email protected]> Sun, 26 Oct 2025 10:57:18 +0200
Newsgroups gmane.comp.java.cayenne.user
Message-ID <[email protected]>
--68fde26e_0_a78ffbdd
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I'm wondering if you couldn't just use:

Cayenne.object=46orPK( context, objClass, pkValue );
regards Jurgen
On Oct 26 2025, at 1:13 am, Ricardo Parada <rparada=40mac.com.INVALID> wr=
ote:
>
> I put together this utility method. It seems to work:
> public static <T extends Persistent> T fault=46orPrimaryKeyValue(
> Class<T> type, Object pkValue, ObjectContext context) =7B
> if (type =3D=3D null =7C=7C pkValue =3D=3D null =7C=7C context =3D=3D n=
ull) =7B
> throw new IllegalArgumentException(=22type, pkValue, and context must n=
ot be null=22);
> =7D
>
> EntityResolver resolver =3D context.getEntityResolver();
> ObjEntity objEntity =3D resolver.getObjEntity(type);
> if (objEntity =3D=3D null) =7B
> throw new IllegalArgumentException(=22No ObjEntity found for class =22 =
+ type.getName());
> =7D
>
> DbEntity dbEntity =3D objEntity.getDbEntity();
> if (dbEntity =3D=3D null) =7B
> throw new IllegalStateException(=22No DbEntity mapped for ObjEntity =22=
 + objEntity.getName());
> =7D
>
> if (dbEntity.getPrimaryKeys().size() =21=3D 1) =7B
> throw new IllegalStateException(
> =22Entity =22 + dbEntity.getName() + =22 must have exactly one primary =
key column=22);
> =7D
>
> // 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);
>
> // Check if object already registered
> Persistent existing =3D (Persistent) context.getGraphManager().getNode(=
objectId);
> if (existing =21=3D null) =7B
> =40SuppressWarnings(=22unchecked=22)
> T casted =3D (T) existing;
> return casted;
> =7D
>
> // Create a new hollow (fault) object
> ClassDescriptor descriptor =3D resolver.getClassDescriptor(entityName);=

> Persistent obj =3D (Persistent) descriptor.createObject();
>
> obj.setObjectContext(context);
> obj.setObjectId(objectId);
> obj.setPersistenceState(PersistenceState.HOLLOW);
>
> context.getGraphManager().registerNode(objectId, obj);
> =40SuppressWarnings(=22unchecked=22)
> T casted =3D (T) obj;
> return casted;
> =7D
>
>
> > On Oct 25, 2025, at 4:34=E2=80=AFPM, Ricardo Parada <rparada=40mac.co=
m> wrote:
> > =EF=BB=BF
> >
> > Awesome, thank you for that code Andrus.
> >
> > Ricardo Parada
> >
> >
> >
> >>
> >> On Oct 25, 2025, at 12:16=E2=80=AFPM, Andrus Adamchik <aadamchik=40g=
mail.com> wrote:
> >>
> >> =EF=BB=BFHi Ricardo,
> >>
> >> A rough equivalent used by Cayenne internally is this:
> >>
> >> ObjectId oid =3D ObjectId.of(entityName, =22PK=5FCOL=5FNAME=22, id);=

> >> ClassDescriptor descriptor =3D context.getEntityResolver().getClassD=
escriptor(entityName);
> >>
> >> Persistent o =3D (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=E2=80=AFAM, Ricardo Parada <rparada=40mac.=
com.INVALID> wrote:
> >>> Good morning,
> >>> I=E2=80=99m looking for the equivalent of the following EO=46 code:=

> >>> var obj =3D EOUtilities.faultWithPrimaryKeyValue(editingContext, en=
tityName, id);
> >>> What I have so far is:
> >>> var objClass =3D oc.getEntityResolver().getClassDescriptor(objEntit=
yName).getObjectClass();
> >>> var obj =3D SelectById.query(objClass, id)
> >>> .localCache()
> >>> .selectOne(oc);
> >>> But it seems to fetch the object the first time even though the obj=
ect already exists in the object context.
> >>> Thanks in advance,
> >>> Ricardo Parada
>


--68fde26e_0_a78ffbdd--