code: public <E> E upsert(E entity) {
      try {
        entityManager.executeEntityListeners(CallbackType.PRE_UPSERT, entity);
        FullEntity<?> nativeEntity = (FullEntity<?>) Marshaller.marshal(entityManager, entity,
            Intent.UPSERT);
        Entity upsertedNativeEntity = nativeWriter.put(nativeEntity);
        @SuppressWarnings("unchecked")
        E upsertedEntity = (E) Unmarshaller.unmarshal(upsertedNativeEntity, entity.getClass());
        entityManager.executeEntityListeners(CallbackType.POST_UPSERT, upsertedEntity);
        return upsertedEntity;
      } catch (DatastoreException exp) {
        throw DatastoreUtils.wrap(exp);
      }
    }
docs: Updates or inserts the given entity in the Cloud Datastore. If the entity does not have an ID,
  it may be generated.
  
  @param entity
  the entity to update or insert
  @return the updated/inserted entity.
  @throws EntityManagerException
  if any error occurs while saving.