private Object readMapOrCollectionOrEntity(final Datastore datastore, final Mapper mapper, final EntityCache cache, final MappedField mf, final EphemeralMappedField ephemeralMappedField, final DBObject dbObj) { if (ephemeralMappedField != null) { mapper.fromDb(datastore, dbObj, ephemeralMappedField, cache); return ephemeralMappedField.getValue(); } else { final Object newEntity = mapper.getOptions().getObjectFactory().createInstance(mapper, mf, dbObj); return mapper.fromDb(datastore, dbObj, newEntity, cache); } }
/** * Converts a DBObject back to a type-safe java object (POJO) * * @param <T> the type of the entity * @param datastore the Datastore to use when fetching this reference * @param entityClass The type to return, or use; can be overridden by the @see Mapper.CLASS_NAME_FIELDNAME in the DBObject * @param dbObject the DBObject containing the document from mongodb * @param cache the EntityCache to use * @return the new entity * @see Mapper#CLASS_NAME_FIELDNAME */ public <T> T fromDBObject(final Datastore datastore, final Class<T> entityClass, final DBObject dbObject, final EntityCache cache) { if (dbObject == null) { final Throwable t = new Throwable(); LOG.error("A null reference was passed in for the dbObject", t); return null; } T entity; entity = opts.getObjectFactory().createInstance(entityClass, dbObject); entity = fromDb(datastore, dbObject, entity, cache); return entity; }
/** * Converts an entity (POJO) to a DBObject. A special field will be added to keep track of the class type. * * @param datastore the Datastore to use when fetching this reference * @param dbObject the DBObject * @param <T> the type of the referenced entity * @return the entity */ <T> T fromDBObject(final Datastore datastore, final DBObject dbObject) { if (dbObject.containsField(CLASS_NAME_FIELDNAME)) { T entity = opts.getObjectFactory().createInstance(null, dbObject); entity = fromDb(datastore, dbObject, entity, createEntityCache()); return entity; } else { throw new MappingException(format("The DBOBbject does not contain a %s key. Determining entity type is impossible.", CLASS_NAME_FIELDNAME)); } }
refObj = mapper.fromDb(datastore, refDbObject, refObj, cache); cache.putEntity(key, refObj); return refObj;
DBObject value = (DBObject) dbVal; refObj = mapper.getOptions().getObjectFactory().createInstance(mapper, mf, value); refObj = mapper.fromDb(datastore, value, refObj, cache);