@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) )); } } }
/** * 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(); }
private Query<?> buildExistsQuery(final Object entityOrKey) { final Object unwrapped = ProxyHelper.unwrap(entityOrKey); final Key<?> key = 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("_id", key.getId()); }
/** * 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); }
@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 update(createQuery(clazz).disableValidation().filter("_id", key.getId()), operations, new UpdateOptions()); }
collection = datastore.getCollection(key.getType()); id = ref; } else {