Re: Getting fault or object
Ricardo Parada <[email protected]> Sat, 25 Oct 2025 19:13:07 -0400
| Newsgroups | gmane.comp.java.cayenne.user |
|---|---|
| Message-ID | <[email protected]> |
--Apple-Mail-22CB64E6-7F72-43D1-82B1-0DD40F6BC1D4
Content-Type: text/plain;
charset=utf-8
Content-Transfer-Encoding: quoted-printable
I put together this utility method. It seems to work:
public static <T extends Persistent> T faultForPrimaryKeyValue(
Class<T> type, Object pkValue, ObjectContext contex=
t) {
if (type =3D=3D null || pkValue =3D=3D null || context =3D=3D=
null) {
throw new IllegalArgumentException("type, pkValue, a=
nd context must not be null");
}
EntityResolver resolver =3D context.getEntityResolver();
ObjEntity objEntity =3D resolver.getObjEntity(type);
if (objEntity =3D=3D null) {
throw new IllegalArgumentException("No ObjEntity fo=
und for class " + type.getName());
}
DbEntity dbEntity =3D objEntity.getDbEntity();
if (dbEntity =3D=3D null) {
throw new IllegalStateException("No DbEntity mapped=
for ObjEntity " + objEntity.getName());
}
if (dbEntity.getPrimaryKeys().size() !=3D 1) {
throw new IllegalStateException(
"Entity " + dbEntity.getName() + " m=
ust have exactly one primary key column");
}
// 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, pkVal=
ue);
// Check if object already registered
Persistent existing =3D (Persistent) context.getGraphManage=
r().getNode(objectId);
if (existing !=3D null) {
@SuppressWarnings("unchecked")
T casted =3D (T) existing;
return casted;
}
// 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);
@SuppressWarnings("unchecked")
T casted =3D (T) obj;
return casted;
}
> On Oct 25, 2025, at 4:34=E2=80=AFPM, Ricardo Parada <[email protected]> wrot=
e:
> =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]=
m> 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().getClassDescri=
ptor(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]=
ALID> wrote:
>>> Good morning,
>>> I=E2=80=99m looking for the equivalent of the following EOF code:
>>> var obj =3D EOUtilities.faultWithPrimaryKeyValue(editingContext, entityN=
ame, 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 a=
lready exists in the object context.
>>> Thanks in advance,
>>> Ricardo Parada
--Apple-Mail-22CB64E6-7F72-43D1-82B1-0DD40F6BC1D4--