/** * Sets the key onto the POJO id/parent fields */ @SuppressWarnings("unchecked") private void setKey(final P pojo, final IncompleteKey key, final LoadContext ctx, final Path containerPath) { if (!clazz.isAssignableFrom(pojo.getClass())) throw new IllegalArgumentException("Trying to use metadata for " + clazz.getName() + " to set key of " + pojo.getClass().getName()); // If no key, don't need to do anything if (key == null) return; idMeta.setValue(pojo, Keys.getIdValue(key), ctx, containerPath); final com.google.cloud.datastore.Key parentKey = key.getParent(); if (parentKey != null) { if (this.parentMeta == null) throw new IllegalStateException("Loaded Entity has parent but " + clazz.getName() + " has no @Parent"); parentMeta.setValue(pojo, (Value)KeyValue.of(parentKey), ctx, containerPath); } }
/** * Unamrshals the entity's key and parent key. * * @throws Throwable * propagated * */ private void unmarshalKeyAndParentKey() throws Throwable { KeyMetadata keyMetadata = entityMetadata.getKeyMetadata(); if (keyMetadata != null) { MethodHandle writeMethod = keyMetadata.getWriteMethod(); Key entityKey = (Key) nativeEntity.getKey(); writeMethod.invoke(entity, new DefaultDatastoreKey(entityKey)); } ParentKeyMetadata parentKeyMetadata = entityMetadata.getParentKeyMetadata(); if (parentKeyMetadata != null) { MethodHandle writeMethod = parentKeyMetadata.getWriteMethod(); Key parentKey = nativeEntity.getKey().getParent(); if (parentKey != null) { writeMethod.invoke(entity, new DefaultDatastoreKey(parentKey)); } } }