code: public void updateKeyAndVersionInfo(final Datastore datastore, final DBObject dbObj, final EntityCache cache, final Object entity) {
          final MappedClass mc = getMappedClass(entity);
  
          // update id field, if there.
          if ((mc.getIdField() != null) && (dbObj != null) && (dbObj.get("_id") != null)) {
              try {
                  final MappedField mf = mc.getMappedIdField();
                  final Object oldIdValue = mc.getIdField().get(entity);
                  readMappedField(datastore, mf, entity, cache, dbObj);
                  if (oldIdValue != null) {
                      // The entity already had an id set. Check to make sure it hasn't changed. That would be unexpected, and could
                      // indicate a bad state.
                      final Object dbIdValue = mf.getFieldValue(entity);
                      if (!dbIdValue.equals(oldIdValue)) {
                          mf.setFieldValue(entity, oldIdValue); //put the value back...
                          throw new RuntimeException(format("@Id mismatch: %s != %s for %s", oldIdValue, dbIdValue,
                              entity.getClass().getName()));
                      }
                  }
              } catch (Exception e) {
                  if (e instanceof RuntimeException) {
                      throw (RuntimeException) e;
                  }
  
                  throw new RuntimeException("Error setting @Id field after save/insert.", e);
              }
          }
          if (mc.getMappedVersionField() != null && (dbObj != null)) {
              readMappedField(datastore, mc.getMappedVersionField(), entity, cache, dbObj);
          }
      }
docs: Updates the @{@link dev.morphia.annotations.Id} and @{@link dev.morphia.annotations.Version} fields.
  
  @param datastore the Datastore to use when fetching this reference
  @param dbObj     Value to update with; null means skip
  @param cache     the EntityCache
  @param entity    The object to update