protected void writeInternal(Object obj, final DBObject dbo, MongoPersistentEntity<?> entity) {

    if (obj == null) {
      return;
    }

    if (null == entity) {
      throw new MappingException(
          "No mapping metadata found for entity of type " + obj.getClass().getName());
    }

    final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(obj);
    final MongoPersistentProperty idProperty = entity.getIdProperty();

    if (!dbo.containsField("_id") && null != idProperty) {

      try {
        Object id = accessor.getProperty(idProperty);
        dbo.put("_id", idMapper.convertId(id));
      } catch (ConversionException ignored) {
      }
    }

    // Write the properties
    entity.doWithProperties(
        new PropertyHandler<MongoPersistentProperty>() {
          public void doWithPersistentProperty(MongoPersistentProperty prop) {

            if (prop.equals(idProperty) || !prop.isWritable()) {
              return;
            }

            Object propertyObj = accessor.getProperty(prop);

            if (null != propertyObj) {

              if (!conversions.isSimpleType(propertyObj.getClass())) {
                writePropertyInternal(propertyObj, dbo, prop);
              } else {
                writeSimpleInternal(propertyObj, dbo, prop);
              }
            }
          }
        });

    entity.doWithAssociations(
        new AssociationHandler<MongoPersistentProperty>() {

          public void doWithAssociation(Association<MongoPersistentProperty> association) {

            MongoPersistentProperty inverseProp = association.getInverse();
            Object propertyObj = accessor.getProperty(inverseProp);

            if (null != propertyObj) {
              writePropertyInternal(propertyObj, dbo, inverseProp);
            }
          }
        });
  }
  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));
  }
  private <S extends Object> S read(
      final MongoPersistentEntity<S> entity, final DBObject dbo, final ObjectPath path) {

    final DefaultSpELExpressionEvaluator evaluator =
        new DefaultSpELExpressionEvaluator(dbo, spELContext);

    ParameterValueProvider<MongoPersistentProperty> provider =
        getParameterProvider(entity, dbo, evaluator, path);
    EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
    S instance = instantiator.createInstance(entity, provider);

    final PersistentPropertyAccessor accessor =
        new ConvertingPropertyAccessor(entity.getPropertyAccessor(instance), conversionService);

    final MongoPersistentProperty idProperty = entity.getIdProperty();
    final S result = instance;

    // make sure id property is set before all other properties
    Object idValue = null;

    if (idProperty != null) {
      idValue = getValueInternal(idProperty, dbo, evaluator, path);
      accessor.setProperty(idProperty, idValue);
    }

    final ObjectPath currentPath =
        path.push(result, entity, idValue != null ? dbo.get(idProperty.getFieldName()) : null);

    // Set properties not already set in the constructor
    entity.doWithProperties(
        new PropertyHandler<MongoPersistentProperty>() {
          public void doWithPersistentProperty(MongoPersistentProperty prop) {

            // we skip the id property since it was already set
            if (idProperty != null && idProperty.equals(prop)) {
              return;
            }

            if (!dbo.containsField(prop.getFieldName()) || entity.isConstructorArgument(prop)) {
              return;
            }

            accessor.setProperty(prop, getValueInternal(prop, dbo, evaluator, currentPath));
          }
        });

    // Handle associations
    entity.doWithAssociations(
        new AssociationHandler<MongoPersistentProperty>() {
          public void doWithAssociation(Association<MongoPersistentProperty> association) {

            final MongoPersistentProperty property = association.getInverse();
            Object value = dbo.get(property.getFieldName());

            if (value == null || entity.isConstructorArgument(property)) {
              return;
            }

            DBRef dbref = value instanceof DBRef ? (DBRef) value : null;

            DbRefProxyHandler handler =
                new DefaultDbRefProxyHandler(
                    spELContext, mappingContext, MappingMongoConverter.this);
            DbRefResolverCallback callback =
                new DefaultDbRefResolverCallback(
                    dbo, currentPath, evaluator, MappingMongoConverter.this);

            accessor.setProperty(
                property, dbRefResolver.resolveDbRef(property, dbref, callback, handler));
          }
        });

    return result;
  }