Re: Getting fault or object
Andrus Adamchik <[email protected]> Sun, 26 Oct 2025 15:57:01 -0700
| Newsgroups | gmane.comp.java.cayenne.user |
|---|---|
| Message-ID | <[email protected]> |
"objectForPK()" executes a query though unless the object is already = cached. So this is not entirely the same as creating a fault.=20 Andrus > On Oct 26, 2025, at 6:54=E2=80=AFAM, Ricardo Parada = <[email protected]> wrote: >=20 > Yes, Cayenne.objectForPK() works.=20 >=20 > I did not know about the Cayenne class.=20 >=20 > Thank you, > Ricardo Parada >=20 >=20 >>=20 >> On Oct 26, 2025, at 4:58=E2=80=AFAM, Jurgen Doll = <[email protected]> wrote: >>=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]> = wrote: >>>=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 be 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 " = + objEntity.getName()); >>> } >>>=20 >>> if (dbEntity.getPrimaryKeys().size() !=3D 1) { >>> throw new IllegalStateException( >>> "Entity " + dbEntity.getName() + " must have exactly one primary key = column"); >>> } >>>=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(objectId); >>> 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]> wrote: >>>> =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 = <[email protected]> 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().getClassDescriptor(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]> wrote: >>>>>> Good morning, >>>>>> I=E2=80=99m looking for the equivalent of the following EOF code: >>>>>> var obj =3D EOUtilities.faultWithPrimaryKeyValue(editingContext, = entityName, id); >>>>>> What I have so far is: >>>>>> var objClass =3D = oc.getEntityResolver().getClassDescriptor(objEntityName).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