}
final DBRef dbRef = idOnly ? null : (DBRef) ref;
final Key key = mapper.createKey(mf.isSingleValue() ? mf.getType() : mf.getSubClass(),
idOnly ? ref : dbRef.getId());
final Datastore ds = idOnly ? mapper.getDatastoreProvider().get() : null;
final Object cached = cache.getEntity(key);
if (cached != null) {
return cached;
}
//TODO: if _db is null, set it?
final DBObject refDbObject = idOnly ? ds.getCollection(key.getKindClass()).findOne(ref) : dbRef.fetch();
if (refDbObject != null) {
Object refObj = mapper.getOptions().getObjectFactory().createInstance(mapper, mf, refDbObject);
refObj = mapper.fromDb(refDbObject, refObj, cache);
cache.putEntity(key, refObj);