protected DBRef createDBRef(Object target, MongoPersistentProperty property) { Assert.notNull(target); if (target instanceof DBRef) { return (DBRef) target; } MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass()); targetEntity = targetEntity == null ? targetEntity = mappingContext.getPersistentEntity(property) : targetEntity; if (null == targetEntity) { throw new MappingException("No mapping metadata found for " + target.getClass()); } MongoPersistentProperty idProperty = targetEntity.getIdProperty(); if (idProperty == null) { throw new MappingException("No id property found on class " + targetEntity.getType()); } Object id = null; if (target.getClass().equals(idProperty.getType())) { id = target; } else { PersistentPropertyAccessor accessor = targetEntity.getPropertyAccessor(target); id = accessor.getProperty(idProperty); } if (null == id) { throw new MappingException("Cannot create a reference to an object with a NULL id."); } return dbRefResolver.createDbRef( property == null ? null : property.getDBRef(), targetEntity, idMapper.convertId(id)); }
/** * Performs the fetch operation for the given {@link DBRef}. * * @param ref * @return */ DBObject readRef(DBRef ref) { return dbRefResolver.fetch(ref); }