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