Class DefaultMongoTypeMapper

java.lang.Object
org.springframework.data.convert.DefaultTypeMapper<org.bson.conversions.Bson>
org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper
All Implemented Interfaces:
Aware, BeanClassLoaderAware, TypeMapper<org.bson.conversions.Bson>, MongoTypeMapper

public class DefaultMongoTypeMapper extends DefaultTypeMapper<org.bson.conversions.Bson> implements MongoTypeMapper
Default implementation of MongoTypeMapper allowing configuration of the key to lookup and store type information in Document. The key defaults to DEFAULT_TYPE_KEY.
Author:
Oliver Gierke, Thomas Darimont, Christoph Strobl, Mark Paluch
  • Field Details

  • Constructor Details

    • DefaultMongoTypeMapper

      public DefaultMongoTypeMapper()
      Create a new MongoTypeMapper with fully-qualified type hints using _class.
    • DefaultMongoTypeMapper

      public DefaultMongoTypeMapper(@Nullable String typeKey)
      Create a new MongoTypeMapper with fully-qualified type hints using typeKey.
      Parameters:
      typeKey - name of the field to read and write type hints. Can be null to disable type hints.
    • DefaultMongoTypeMapper

      public DefaultMongoTypeMapper(@Nullable String typeKey, MappingContext<? extends PersistentEntity<?,?>,?> mappingContext)
      Create a new MongoTypeMapper with fully-qualified type hints using typeKey.
      Parameters:
      typeKey - name of the field to read and write type hints. Can be null to disable type hints.
      mappingContext - the mapping context.
    • DefaultMongoTypeMapper

      public DefaultMongoTypeMapper(@Nullable String typeKey, MappingContext<? extends PersistentEntity<?,?>,?> mappingContext, UnaryOperator<Class<?>> writeTarget)
      Create a new MongoTypeMapper with fully-qualified type hints using typeKey. Uses UnaryOperator to apply CustomConversions.
      Parameters:
      typeKey - name of the field to read and write type hints. Can be null to disable type hints.
      mappingContext - the mapping context to look up types using type hints.
      See Also:
    • DefaultMongoTypeMapper

      public DefaultMongoTypeMapper(@Nullable String typeKey, List<? extends TypeInformationMapper> mappers)
      Create a new MongoTypeMapper with fully-qualified type hints using typeKey. Uses TypeInformationMapper to map type hints.
      Parameters:
      typeKey - name of the field to read and write type hints. Can be null to disable type hints.
      mappers - must not be null.
  • Method Details

    • isTypeKey

      public boolean isTypeKey(String key)
      Description copied from interface: MongoTypeMapper
      Returns whether the given key is the type key.
      Specified by:
      isTypeKey in interface MongoTypeMapper
      Returns:
    • writeTypeRestrictions

      public void writeTypeRestrictions(org.bson.Document result, @Nullable Set<Class<?>> restrictedTypes)
      Description copied from interface: MongoTypeMapper
      Writes type restrictions to the given Document. This usually results in an $in-clause to be generated that restricts the type-key (e.g. _class) to be in the set of type aliases for the given restrictedTypes.
      Specified by:
      writeTypeRestrictions in interface MongoTypeMapper
      Parameters:
      result - must not be null
      restrictedTypes - must not be null
    • getWriteTargetTypeFor

      public Class<?> getWriteTargetTypeFor(Class<?> source)
      Description copied from interface: MongoTypeMapper
      Compute the target type for a given source considering CustomConversions.
      Specified by:
      getWriteTargetTypeFor in interface MongoTypeMapper
      Parameters:
      source - the source type.
      Returns:
      never null.
    • getFallbackTypeFor

      protected TypeInformation<?> getFallbackTypeFor(org.bson.conversions.Bson source)
      Overrides:
      getFallbackTypeFor in class DefaultTypeMapper<org.bson.conversions.Bson>