Skip to content

Commit

Permalink
Cayenne entity ID part represented by a DB column to be prefixed wit…
Browse files Browse the repository at this point in the history
…h "db:" #521

.. in progress..
  • Loading branch information
andrus committed Jan 1, 2022
1 parent 6083986 commit 00726e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,25 @@ public PropertyReader reader(NestedResourceEntity<T> entity) {

AgEntity<?> parentEntity = entity.getParent().getAgEntity();

// Per #473 there may be no ID in AgEntity, but there's one in Cayenne DbEntity, so let's the one from
// Cayenne if possible. This would allow nested prefetches even if ID is excluded from the public data.
PropertyReader parentIdReader = parentEntity.getIdReader();

return entity instanceof ToManyResourceEntity
? new ToManyEntityResultReader((ToManyResourceEntity) entity, parentIdReader)
: new ToOneEntityResultReader((ToOneResourceEntity<?>) entity, parentIdReader);
}

protected PropertyReader parentIdReader(AgEntity<?> parentEntity) {

// Per #473 there may be no ID in AgEntity, but there's one in Cayenne DbEntity. So let's the one from Cayenne
// if that's the case. This would allow nested prefetches even if ID is excluded from the public data.

// Note that this ID reader should only be used inside "agrest-cayenne" and should not leak to the generic
// part of the Agrest stack.

PropertyReader parentIdReader = persister.entityResolver().getObjEntity(parentEntity.getName()) != null
return parentEntity.getIdParts().isEmpty() && persister.entityResolver().getObjEntity(parentEntity.getName()) != null
// TODO: ID snapshot keys should be prefixed with "db:"
? p -> ((Persistent) p).getObjectId().getIdSnapshot()
: parentEntity.getIdReader();

return entity instanceof ToManyResourceEntity
? new ToManyEntityResultReader((ToManyResourceEntity) entity, parentIdReader)
: new ToOneEntityResultReader((ToOneResourceEntity<?>) entity, parentIdReader);
}

protected void indexResultByParentId(NestedResourceEntity<T> entity, List<Object[]> result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ protected void assignChildrenToParent(DataObject root, Map<String, NestedResourc
continue;
}

// TODO: getIdSnapshot() will not prefix keys with "db". Must use AgIdParts to resolve the ID
AgObjectId id = root.getObjectId().getIdSnapshot().size() > 1
? new CompoundObjectId(root.getObjectId().getIdSnapshot())
: new SimpleObjectId(root.getObjectId().getIdSnapshot().values().iterator().next());
Expand Down

0 comments on commit 00726e6

Please sign in to comment.