org.mongodb.morphia.Key类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(145)

本文整理了Java中org.mongodb.morphia.Key类的一些代码示例,展示了Key类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Key类的具体详情如下:
包路径:org.mongodb.morphia.Key
类名称:Key

Key介绍

[英]The key object; this class is take from the app-engine datastore (mostly) implementation. It is also Serializable and GWT-safe, enabling your entity objects to be used for GWT RPC should you so desire.

You may use normal DBRef objects as relationships in your entities if you desire neither type safety nor GWT-ability.
[中]

代码示例

代码示例来源:origin: querydsl/querydsl

@Override
  protected DBRef asReferenceKey(Class<?> entity, Object id) {
    String collection = morphia.getMapper().getCollectionName(entity);
    Key<?> key = new Key<Object>(entity, collection, id);
    return morphia.getMapper().keyToDBRef(key);
  }
}

代码示例来源:origin: org.mongodb.morphia/morphia

/**
 * Converts a Key to a DBRef
 *
 * @param key the Key to convert
 * @return the DBRef
 */
public DBRef keyToDBRef(final Key key) {
  if (key == null) {
    return null;
  }
  if (key.getType() == null && key.getCollection() == null) {
    throw new IllegalStateException("How can it be missing both?");
  }
  if (key.getCollection() == null) {
    key.setCollection(getCollectionName(key.getType()));
  }
  Object id = key.getId();
  if (isMapped(id.getClass())) {
    id = toMongoObject(id, true);
  }
  return new DBRef(key.getCollection(), id);
}

代码示例来源:origin: org.mongodb.morphia/morphia

private Query<?> buildExistsQuery(final Object entityOrKey) {
  final Object unwrapped = ProxyHelper.unwrap(entityOrKey);
  final Key<?> key = mapper.getKey(unwrapped);
  final Object id = key.getId();
  if (id == null) {
    throw new MappingException("Could not get id for " + unwrapped.getClass().getName());
  }
  return find(key.getCollection(), key.getType()).filter(Mapper.ID_KEY, key.getId());
}

代码示例来源:origin: org.mongodb.morphia/morphia

Object keyToId(final Key key) {
  return key == null ? null : key.getId();
}

代码示例来源:origin: org.mongodb.morphia/morphia

/**
 * Updates the collection value on a Key with the mapped value on the Key's type Class
 *
 * @param key the Key to update
 * @return the collection name on the Key
 */
public String updateCollection(final Key key) {
  if (key.getCollection() == null && key.getType() == null) {
    throw new IllegalStateException("Key is invalid! " + toString());
  } else if (key.getCollection() == null) {
    key.setCollection(getMappedClass(key.getType()).getCollectionName());
  }
  return key.getCollection();
}

代码示例来源:origin: org.mongodb.morphia/morphia

if (kindMap.containsKey(key.getCollection())) {
  kindMap.get(key.getCollection()).add(key);
} else {
  kindMap.put(key.getCollection(), new ArrayList<Key>(singletonList((Key) key)));
  objIds.add(key.getId());

代码示例来源:origin: org.mongodb.morphia/morphia

@Override
public int compareTo(final Key<T> other) {
  checkState(this);
  checkState(other);
  int cmp;
  // First collection
  if (other.type != null && type != null) {
    cmp = type.getName().compareTo(other.type.getName());
    if (cmp != 0) {
      return cmp;
    }
  }
  cmp = compareNullable(collection, other.collection);
  if (cmp != 0) {
    return cmp;
  }
  try {
    cmp = compareNullable((Comparable<?>) id, (Comparable<?>) other.id);
    if (cmp != 0) {
      return cmp;
    }
  } catch (Exception e) {
    // Not a comparable, use equals and String.compareTo().
    cmp = id.equals(other.id) ? 0 : 1;
    if (cmp != 0) {
      return id.toString().compareTo(other.id.toString());
    }
  }
  return 0;
}

代码示例来源:origin: org.mongodb.morphia/morphia

@Override
  protected void validate(final Class<?> type, final Object value, final List<ValidationFailure> validationFailures) {
    if (!type.equals(((Key) value).getType()) && !type.equals(Key.class)) {
      validationFailures.add(new ValidationFailure(format("When value is a Key, the type needs to be the right kind of class. "
                                + "Type was %s and value was '%s'", type, value)
      ));
    }
  }
}

代码示例来源:origin: org.mongodb.morphia/morphia

@Override
@SuppressWarnings("unchecked")
public boolean equals(final Object obj) {
  return obj != null && obj instanceof Key<?> && compareTo((Key<T>) obj) == 0;
}

代码示例来源:origin: org.mongodb.morphia/morphia

@Override
@SuppressWarnings("unchecked")
public <T> UpdateResults update(final Key<T> key, final UpdateOperations<T> operations) {
  Class<T> clazz = (Class<T>) key.getType();
  if (clazz == null) {
    clazz = (Class<T>) mapper.getClassFromCollection(key.getCollection());
  }
  return updateFirst(createQuery(clazz).disableValidation().filter(Mapper.ID_KEY, key.getId()), operations);
}

代码示例来源:origin: org.mongodb.morphia/morphia

/**
 * Converts from a List<Key> to their id values
 */
protected List<?> keysToIds(final List<Key<T>> keys) {
  final List<Object> ids = new ArrayList<Object>(keys.size() * 2);
  for (final Key<T> key : keys) {
    ids.add(key.getId());
  }
  return ids;
}

代码示例来源:origin: org.mongodb.morphia/morphia

collection = datastore.getCollection(key.getType());
  id = ref;
} else {

代码示例来源:origin: org.mongodb.morphia/morphia

/**
 * Creates a Key for a type and an ID value
 *
 * @param type the Class of the entity
 * @param id   the ID value
 * @param <T>  the type of the entity
 * @return the Key
 */
public <T> Key<T> manualRefToKey(final Class<T> type, final Object id) {
  return id == null ? null : new Key<T>(type, getCollectionName(type), id);
}

代码示例来源:origin: de.mhus.lib/mhu-lib-persistence

public Object getId(Object object) {
  if (object == null) return "";
  return datastore.getKey(object).getId();
}

代码示例来源:origin: org.mongodb.morphia/morphia

<T> Key<T> manualRefToKey(final String collection, final Object id) {
  return id == null ? null : new Key<T>((Class<? extends T>) getClassFromCollection(collection), collection, id);
}

代码示例来源:origin: org.mongodb.morphia/morphia

private Object getDBRefs(final MappedField field, final Iterable value) {
  final List<Object> refs = new ArrayList<Object>();
  Reference annotation = field.getAnnotation(Reference.class);
  boolean idOnly = annotation != null && annotation.idOnly();
  for (final Object o : value) {
    Key<?> key = (o instanceof Key) ? (Key<?>) o : getKey(o);
    refs.add(idOnly ? key.getId() : keyToDBRef(key));
  }
  return refs;
}

代码示例来源:origin: org.mongodb.morphia/morphia

<T> Key<T> createKey(final Class<T> clazz, final Serializable id) {
  return new Key<T>(clazz, getCollectionName(clazz), id);
}

代码示例来源:origin: org.mongodb.morphia/morphia

@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
  if ("finalize".equals(method.getName()) && args != null && args.length == 0) {
    return null;
  }
  /*
   * If the method being invoked is annotated with @IdGetter and the delegate reference is an EntityObjectReference,
   * return the id of the EntityObjectReference's key. This allows us to return the referenced entity's id without
   * fetching the entity from the datastore.
   */
  if (method.getAnnotation(IdGetter.class) != null) {
    ObjectReference<Object> delegateReference = getDelegateReference();
    if (delegateReference instanceof EntityObjectReference) {
      EntityObjectReference entityObjectReference = (EntityObjectReference) delegateReference;
      return entityObjectReference.__getKey().getId();
    }
  }
  return super.invoke(proxy, method, args);
}

代码示例来源:origin: katharsis-project/katharsis-framework

public Project findOne(ObjectId id, QueryParams requestParams) {
  return datastore.getByKey(Project.class, new Key<>(Project.class, id));
}

代码示例来源:origin: org.mongodb.morphia/morphia

@Override
public <T> T getByKey(final Class<T> clazz, final Key<T> key) {
  final String collectionName = mapper.getCollectionName(clazz);
  final String keyCollection = mapper.updateCollection(key);
  if (!collectionName.equals(keyCollection)) {
    throw new RuntimeException("collection names don't match for key and class: " + collectionName + " != " + keyCollection);
  }
  Object id = key.getId();
  if (id instanceof DBObject) {
    ((DBObject) id).removeField(Mapper.CLASS_NAME_FIELDNAME);
  }
  return get(clazz, id);
}

相关文章