if (value == null) {
return null;
}
if (value instanceof Key) {
DatastoreServiceConfig config = getStoreManager().getDefaultDatastoreServiceConfigForReads();
DatastoreService datastoreService = DatastoreServiceFactoryInternal.getDatastoreService(config);
try {
return EntityUtils.entityToPojo(datastoreService.get((Key)value), childCmd, clr, ec, false, ec.getFetchPlan());
} catch (EntityNotFoundException enfe) {
// TODO: Should this throw a data integrity exception? It seems to for 1-N.
NucleusLogger.PERSISTENCE.error("Member " + mmd.getFullFieldName() + " of " + getObjectProvider().getInternalObjectId() +
" was pointing to object with key " + value + " but this doesn't exist! Returning null");
return null;
}
}
} else if (MetaDataUtils.isOwnedRelation(mmd, getStoreManager())) {
// Not yet got the property in the parent, so this entity has not yet been migrated to latest storage version
NucleusLogger.PERSISTENCE.info("Persistable object at member " + mmd.getFullFieldName() + " of " + getObjectProvider() +
" not yet migrated to latest storage version, so reading the object via its parent key");
} else {
// Unowned relation but we don't have the property! Why? Maybe was originally uni but changed to bi?
NucleusLogger.PERSISTENCE.error("Object " + datastoreEntity.getKey() + " has unowned member " + mmd.getFullFieldName() +
" but no corresponding property " + propName + " on its datastore entity, so returning null");
return null;
}
}
// Owned 1-1, so find all entities with this as a parent. There ought to be only 1 (limitation of early GAE)
Entity parentEntity = datastoreEntity;
Query q = new Query(kind, parentEntity.getKey());
DatastoreServiceConfig config = getStoreManager().getDefaultDatastoreServiceConfigForReads();
DatastoreService datastoreService = DatastoreServiceFactoryInternal.getDatastoreService(config);
// We have to pull back all children because the datastore does not let us filter ancestors by
// depth and an indirect child could come back before a direct child. eg: a/b/c, a/c
for (Entity e : datastoreService.prepare(q).asIterable()) {
if (parentEntity.getKey().equals(e.getKey().getParent())) {