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);
}
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);
}
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);
}
}
Aggregations