code: private void marshalKey() {
  
      Key parent = null;
  
      ParentKeyMetadata parentKeyMetadata = entityMetadata.getParentKeyMetadata();
      if (parentKeyMetadata != null) {
        DatastoreKey parentDatastoreKey = (DatastoreKey) getFieldValue(parentKeyMetadata);
        if (parentDatastoreKey != null) {
          parent = parentDatastoreKey.nativeKey();
        }
      }
  
      IdentifierMetadata identifierMetadata = entityMetadata.getIdentifierMetadata();
      IdClassMetadata idClassMetadata = identifierMetadata.getIdClassMetadata();
      DataType identifierType = identifierMetadata.getDataType();
      Object idValue = getFieldValue(identifierMetadata);
  
      // If ID value is null, we don't have to worry about if it is a simple
      // type of a complex type. Otherwise, we need to see if the ID is a
      // complex type, and if it is, extract the real ID.
      if (idValue != null && idClassMetadata != null) {
        try {
          idValue = idClassMetadata.getReadMethod().invoke(idValue);
        } catch (Throwable t) {
          throw new EntityManagerException(t);
        }
      }
  
      boolean validId = isValidId(idValue, identifierType);
      boolean autoGenerateId = identifierMetadata.isAutoGenerated();
  
      if (validId) {
        if (identifierType == DataType.STRING) {
          createCompleteKey(parent, (String) idValue);
        } else {
          createCompleteKey(parent, (long) idValue);
        }
      } else {
        if (intent.isKeyRequired()) {
          throw new EntityManagerException(String
              .format("Identifier is not set or valid for entity of type %s", entity.getClass()));
        }
        if (!autoGenerateId) {
          String pattern = "Identifier is not set or valid for entity of type %s. Auto generation "
              + "of ID is explicitly turned off. ";
          throw new EntityManagerException(String.format(pattern, entity.getClass()));
        } else {
          if (identifierType == DataType.STRING) {
            createCompleteKey(parent);
          } else {
            createIncompleteKey(parent);
          }
        }
      }
    }
docs: Marshals the key.