Search in sources :

Example 1 with ProxiedEntityReference

use of org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReference in project morphia by mongodb.

the class Mapper method getKey.

/**
     * Gets the Key for an entity and a specific collection
     *
     * @param entity     the entity to process
     * @param collection the collection to use in the Key rather than the mapped collection as defined on the entity's class
     * @param <T>        the type of the entity
     * @return the Key
     */
public <T> Key<T> getKey(final T entity, final String collection) {
    T unwrapped = entity;
    if (unwrapped instanceof ProxiedEntityReference) {
        final ProxiedEntityReference proxy = (ProxiedEntityReference) unwrapped;
        return (Key<T>) proxy.__getKey();
    }
    unwrapped = ProxyHelper.unwrap(unwrapped);
    if (unwrapped instanceof Key) {
        return (Key<T>) unwrapped;
    }
    final Object id = getId(unwrapped);
    final Class<T> aClass = (Class<T>) unwrapped.getClass();
    return id == null ? null : new Key<T>(aClass, collection, id);
}
Also used : DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ReflectionUtils.getParameterizedClass(org.mongodb.morphia.utils.ReflectionUtils.getParameterizedClass) ProxiedEntityReference(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReference) Key(org.mongodb.morphia.Key)

Example 2 with ProxiedEntityReference

use of org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReference in project morphia by mongodb.

the class Mapper method getKey.

/**
     * Gets the Key for an entity
     *
     * @param entity the entity to process
     * @param <T>    the type of the entity
     * @return the Key
     */
public <T> Key<T> getKey(final T entity) {
    T unwrapped = entity;
    if (unwrapped instanceof ProxiedEntityReference) {
        final ProxiedEntityReference proxy = (ProxiedEntityReference) unwrapped;
        return (Key<T>) proxy.__getKey();
    }
    unwrapped = ProxyHelper.unwrap(unwrapped);
    if (unwrapped instanceof Key) {
        return (Key<T>) unwrapped;
    }
    final Object id = getId(unwrapped);
    final Class<T> aClass = (Class<T>) unwrapped.getClass();
    return id == null ? null : new Key<T>(aClass, getCollectionName(aClass), id);
}
Also used : DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ReflectionUtils.getParameterizedClass(org.mongodb.morphia.utils.ReflectionUtils.getParameterizedClass) ProxiedEntityReference(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReference) Key(org.mongodb.morphia.Key)

Example 3 with ProxiedEntityReference

use of org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReference in project morphia by mongodb.

the class ReferenceMapper method getKey.

private Key<?> getKey(final Object entity, final Mapper mapper) {
    try {
        if (entity instanceof ProxiedEntityReference) {
            final ProxiedEntityReference proxy = (ProxiedEntityReference) entity;
            return proxy.__getKey();
        }
        final MappedClass mappedClass = mapper.getMappedClass(entity);
        Object id = mappedClass.getIdField().get(entity);
        if (id == null) {
            throw new MappingException("@Id field cannot be null!");
        }
        return new Key(mappedClass.getClazz(), mappedClass.getCollectionName(), id);
    } catch (IllegalAccessException iae) {
        throw new RuntimeException(iae);
    }
}
Also used : DBObject(com.mongodb.DBObject) ProxiedEntityReference(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReference) Key(org.mongodb.morphia.Key)

Aggregations

DBObject (com.mongodb.DBObject)3 Key (org.mongodb.morphia.Key)3 ProxiedEntityReference (org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReference)3 BasicDBObject (com.mongodb.BasicDBObject)2 ReflectionUtils.getParameterizedClass (org.mongodb.morphia.utils.ReflectionUtils.getParameterizedClass)2