Class Mapper


  • public class Mapper
    extends java.lang.Object
    Developer note. This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
    • Field Detail

      • ID_KEY

        @Deprecated
        public static final java.lang.String ID_KEY
        Deprecated.
        use "_id" directly
        The @Id field name that is stored with mongodb.
        See Also:
        Constant Field Values
      • IGNORED_FIELDNAME

        public static final java.lang.String IGNORED_FIELDNAME
        Special name that can never be used. Used as default for some fields to indicate default state.
        See Also:
        Constant Field Values
        Developer note. This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
      • CLASS_NAME_FIELDNAME

        @Deprecated
        public static final java.lang.String CLASS_NAME_FIELDNAME
        Deprecated.
        Special field used by morphia to support various possibly loading issues; will be replaced when discriminators are implemented to support polymorphism
        See Also:
        Constant Field Values
    • Constructor Detail

      • Mapper

        public Mapper​(MapperOptions opts)
        Creates a Mapper with the given options.
        Parameters:
        opts - the options to use
      • Mapper

        public Mapper()
        Creates a Mapper with default options
        See Also:
        MapperOptions
      • Mapper

        public Mapper​(MapperOptions options,
                      Mapper mapper)
        Creates a new Mapper with the given options and a Mapper to copy. This is effectively a copy constructor that allows a developer to override the options.
        Parameters:
        options - the options to use
        mapper - the collection of MappedClasses to add
    • Method Detail

      • addMappedClass

        public MappedClass addMappedClass​(java.lang.Class c)
        Creates a MappedClass and validates it.
        Parameters:
        c - the Class to map
        Returns:
        the MappedClass for the given Class
      • createEntityCache

        public EntityCache createEntityCache()
        Creates a cache for tracking entities seen during processing
        Returns:
        the cache
      • fromDBObject

        @Deprecated
        public <T> T fromDBObject​(Datastore datastore,
                                  java.lang.Class<T> entityClass,
                                  com.mongodb.DBObject dbObject,
                                  EntityCache cache)
        Deprecated.
        no replacement is planned
        Converts a DBObject back to a type-safe java object (POJO)
        Type Parameters:
        T - the type of the entity
        Parameters:
        datastore - the Datastore to use when fetching this reference
        entityClass - The type to return, or use; can be overridden by the @see Mapper.CLASS_NAME_FIELDNAME in the DBObject
        dbObject - the DBObject containing the document from mongodb
        cache - the EntityCache to use
        Returns:
        the new entity
        Developer note. This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
      • getSubTypes

        public java.util.List<MappedClass> getSubTypes​(MappedClass mc)
        Finds any subtypes for the given MappedClass.
        Parameters:
        mc - the parent type
        Returns:
        the list of subtypes
        Since:
        1.3
      • fromDb

        @Deprecated
        public <T> T fromDb​(Datastore datastore,
                            com.mongodb.DBObject dbObject,
                            T entity,
                            EntityCache cache)
        Deprecated.
        no replacement is planned
        Converts a DBObject back to a type-safe java object (POJO)
        Type Parameters:
        T - the type of the entity
        Parameters:
        datastore - the Datastore to use when fetching this reference
        dbObject - the DBObject containing the document from mongodb
        entity - the instance to populate
        cache - the EntityCache to use
        Returns:
        the entity
        Developer note. This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
      • getClassFromCollection

        @Deprecated
        public <T> java.lang.Class<T> getClassFromCollection​(java.lang.String collection)
        Deprecated.
        no replacement is planned
        Looks up the class mapped to a named collection.
        Type Parameters:
        T - the class type
        Parameters:
        collection - the collection name
        Returns:
        the Class mapped to this collection name
        Developer note. This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
      • getCollectionName

        public java.lang.String getCollectionName​(java.lang.Object object)
        Gets the mapped collection for an object instance or Class reference.
        Parameters:
        object - the object to process
        Returns:
        the collection name
        Developer note. This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
      • getConverters

        public Converters getConverters()
        Returns:
        the Converters bundle this Mapper uses
      • getId

        public java.lang.Object getId​(java.lang.Object entity)
        Gets the ID value for an entity
        Parameters:
        entity - the entity to process
        Returns:
        the ID value
      • getInstanceCache

        public java.util.Map<java.lang.Class,​java.lang.Object> getInstanceCache()
        Deprecated.
        no replacement is planned
        Returns:
        the cache of instances
        Developer note. This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
      • getKey

        public <T> Key<T> getKey​(T entity)
        Gets the Key for an entity
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity to process
        Returns:
        the Key
      • getKey

        public <T> Key<T> getKey​(T entity,
                                 java.lang.String collection)
        Gets the Key for an entity and a specific collection
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity to process
        collection - the collection to use in the Key rather than the mapped collection as defined on the entity's class
        Returns:
        the Key
      • getKeysByManualRefs

        public <T> java.util.List<Key<T>> getKeysByManualRefs​(java.lang.Class<T> clazz,
                                                              java.util.List<java.lang.Object> refs)
        Gets the Keys for a list of objects
        Type Parameters:
        T - the type of the entity
        Parameters:
        clazz - the Class of the objects
        refs - the objects to fetch the keys for
        Returns:
        the list of Keys
      • getKeysByRefs

        public <T> java.util.List<Key<T>> getKeysByRefs​(java.util.List<com.mongodb.DBRef> refs)
        Gets the Keys for a list of objects
        Type Parameters:
        T - the type of the objects
        Parameters:
        refs - the objects to process
        Returns:
        the list of Keys
      • getMCMap

        public java.util.Map<java.lang.String,​MappedClass> getMCMap()
        Returns:
        map of MappedClasses by class name
      • getMappedClass

        public MappedClass getMappedClass​(java.lang.Object obj)
        Gets the MappedClass for the object (type). If it isn't mapped, create a new class and cache it (without validating).
        Parameters:
        obj - the object to process
        Returns:
        the MappedClass for the object given
      • getMappedClasses

        public java.util.Collection<MappedClass> getMappedClasses()
        Returns:
        collection of MappedClasses
      • getOptions

        public MapperOptions getOptions()
        Returns:
        the options used by this Mapper
      • setOptions

        public void setOptions​(MapperOptions options)
        Sets the options this Mapper should use
        Parameters:
        options - the options to use
      • isMapped

        public boolean isMapped​(java.lang.Class c)
        Checks to see if a Class has been mapped.
        Parameters:
        c - the Class to check
        Returns:
        true if the Class has been mapped
      • keyToDBRef

        public com.mongodb.DBRef keyToDBRef​(Key key)
        Converts a Key to a DBRef
        Parameters:
        key - the Key to convert
        Returns:
        the DBRef
      • manualRefToKey

        public <T> Key<T> manualRefToKey​(java.lang.Class<T> type,
                                         java.lang.Object id)
        Creates a Key for a type and an ID value
        Type Parameters:
        T - the type of the entity
        Parameters:
        type - the Class of the entity
        id - the ID value
        Returns:
        the Key
      • refToKey

        public <T> Key<T> refToKey​(com.mongodb.DBRef ref)
        Converts a DBRef to a Key
        Type Parameters:
        T - the type of the referenced entity
        Parameters:
        ref - the DBRef to convert
        Returns:
        the Key
      • toDBObject

        public com.mongodb.DBObject toDBObject​(java.lang.Object entity)
        /** Converts an entity (POJO) to a DBObject. A special field will be added to keep track of the class type.
        Parameters:
        entity - The POJO
        Returns:
        the DBObject
      • toDBObject

        public com.mongodb.DBObject toDBObject​(java.lang.Object entity,
                                               java.util.Map<java.lang.Object,​com.mongodb.DBObject> involvedObjects)
        Converts an entity (POJO) to a DBObject. A special field will be added to keep track of the class type.
        Parameters:
        entity - The POJO
        involvedObjects - A Map of (already converted) POJOs
        Returns:
        the DBObject
      • toMongoObject

        public java.lang.Object toMongoObject​(MappedField mf,
                                              MappedClass mc,
                                              java.lang.Object value)
        Converts a java object to a mongo-compatible object (possibly a DBObject for complex mappings). Very similar to toDBObject(java.lang.Object). Used (mainly) by query/update operations.
        Parameters:
        mf - the MappedField for this value
        mc - the MappedClass for this value
        value - the value to convert
        Returns:
        the MongoDB compatible object
      • updateCollection

        public java.lang.String updateCollection​(Key key)
        Updates the collection value on a Key with the mapped value on the Key's type Class
        Parameters:
        key - the Key to update
        Returns:
        the collection name on the Key
      • updateKeyAndVersionInfo

        public void updateKeyAndVersionInfo​(Datastore datastore,
                                            com.mongodb.DBObject dbObj,
                                            EntityCache cache,
                                            java.lang.Object entity)
        Updates the @Id and @Version fields.
        Parameters:
        datastore - the Datastore to use when fetching this reference
        dbObj - Value to update with; null means skip
        cache - the EntityCache
        entity - The object to update