diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java index 1bb0cf83a..f0caf4e7d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java @@ -79,8 +79,14 @@ public class BasicMongoPersistentProperty extends AnnotationBasedPersistentPrope this.fieldNamingStrategy = fieldNamingStrategy == null ? PropertyNameFieldNamingStrategy.INSTANCE : fieldNamingStrategy; - if (isIdProperty() && getFieldName() != ID_FIELD_NAME) { - LOG.warn("Customizing field name for id property not allowed! Custom name will not be considered!"); + if (isIdProperty() && hasExplicitFieldName()) { + + String annotatedName = getAnnotatedFieldName(); + if (!ID_FIELD_NAME.equals(annotatedName)) { + LOG.warn( + "Customizing field name for id property '{}.{}' is not allowed! Custom name ('{}') will not be considered!", + owner.getName(), getName(), annotatedName); + } } } @@ -167,6 +173,11 @@ public class BasicMongoPersistentProperty extends AnnotationBasedPersistentPrope FieldType fieldType = fieldAnnotation.targetType(); if (fieldType == FieldType.IMPLICIT) { + + if (isEntity()) { + return org.bson.Document.class; + } + return getType(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java index 5e9d6868c..3e0671f04 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; +import org.bson.Document; import org.bson.types.ObjectId; import org.junit.Before; import org.junit.Test; @@ -223,6 +224,13 @@ public class BasicMongoPersistentPropertyUnitTests { assertThat(property.getFieldType()).isEqualTo(ObjectId.class); } + @Test // DATAMONGO-2460 + public void fieldTypeShouldBeDocumentForPropertiesAnnotatedIdWhenAComplexTypeAndFieldTypeImplicit() { + + MongoPersistentProperty property = getPropertyFor(WithComplexId.class, "id"); + assertThat(property.getFieldType()).isEqualTo(Document.class); + } + private MongoPersistentProperty getPropertyFor(Field field) { return getPropertyFor(entity, field); } @@ -329,4 +337,16 @@ public class BasicMongoPersistentPropertyUnitTests { @MongoId(FieldType.OBJECT_ID) String id; } + + static class ComplexId { + + String value; + } + + static class WithComplexId { + + @Id + @org.springframework.data.mongodb.core.mapping.Field + ComplexId id; + } }