@@ -376,7 +376,9 @@ protected Document getMappedKeyword(Keyword keyword, @Nullable MongoPersistentEn
376376 if (keyword .isOrOrNor () || (keyword .hasIterableValue () && !keyword .isGeometry ())) {
377377
378378 Iterable <?> conditions = keyword .getValue ();
379- List <Object > newConditions = conditions instanceof Collection ? new ArrayList <>(((Collection <?>) conditions ).size ()) : new ArrayList <>();
379+ List <Object > newConditions = conditions instanceof Collection
380+ ? new ArrayList <>(((Collection <?>) conditions ).size ())
381+ : new ArrayList <>();
380382
381383 for (Object condition : conditions ) {
382384 newConditions .add (isDocument (condition ) ? getMappedObject ((Document ) condition , entity )
@@ -434,8 +436,10 @@ protected Object getMappedValue(Field documentField, Object sourceValue) {
434436
435437 Object value = applyFieldTargetTypeHintToValue (documentField , sourceValue );
436438
437- if (documentField .getProperty () != null && converter .getCustomConversions ().getPropertyValueConversions ().hasValueConverter (documentField .getProperty ())) {
438- return converter .getCustomConversions ().getPropertyValueConversions ().getValueConverter (documentField .getProperty ())
439+ if (documentField .getProperty () != null
440+ && converter .getCustomConversions ().getPropertyValueConversions ().hasValueConverter (documentField .getProperty ())) {
441+ return converter .getCustomConversions ().getPropertyValueConversions ()
442+ .getValueConverter (documentField .getProperty ())
439443 .write (value , new MongoConversionContext (documentField .getProperty (), converter ));
440444 }
441445
@@ -619,7 +623,11 @@ protected Object delegateConvertToMongoType(Object source, @Nullable MongoPersis
619623 }
620624
621625 protected Object convertAssociation (Object source , Field field ) {
622- return convertAssociation (source , field .getProperty ());
626+ Object value = convertAssociation (source , field .getProperty ());
627+ if (value != null && field .isIdField () && field .getFieldType () != value .getClass ()) {
628+ return convertId (value , field .getFieldType ());
629+ }
630+ return value ;
623631 }
624632
625633 /**
@@ -1045,6 +1053,9 @@ public TypeInformation<?> getTypeHint() {
10451053 return ClassTypeInformation .OBJECT ;
10461054 }
10471055
1056+ public Class <?> getFieldType () {
1057+ return Object .class ;
1058+ }
10481059 }
10491060
10501061 /**
@@ -1197,10 +1208,11 @@ private Association<MongoPersistentProperty> findAssociation() {
11971208 return null ;
11981209 }
11991210
1200- /*
1201- * (non-Javadoc)
1202- * @see org.springframework.data.mongodb.core.convert.QueryMapper.Field#getTargetKey()
1203- */
1211+ @ Override
1212+ public Class <?> getFieldType () {
1213+ return property .getFieldType ();
1214+ }
1215+
12041216 @ Override
12051217 public String getMappedKey () {
12061218 return path == null ? name : path .toDotPath (isAssociation () ? getAssociationConverter () : getPropertyConverter ());
0 commit comments