Package dev.morphia.mapping
Class Mapper
- java.lang.Object
-
- dev.morphia.mapping.Mapper
-
public class Mapper extends Object
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
-
Field Summary
Fields Modifier and Type Field Description static String
IGNORED_FIELDNAME
Special name that can never be used.
-
Constructor Summary
Constructors Constructor Description Mapper(Datastore datastore, org.bson.codecs.configuration.CodecRegistry codecRegistry, MapperOptions options)
Creates a Mapper with the given options.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addInterceptor(EntityInterceptor ei)
Adds anEntityInterceptor
<T> EntityModel<T>
createEntityModel(Class<T> clazz)
com.mongodb.client.MongoCollection
enforceWriteConcern(com.mongodb.client.MongoCollection collection, Class type)
Updates a collection to use a specific WriteConcern<T> T
fromDocument(Class<T> type, org.bson.Document document)
Converts a Document back to a type-safe java object (POJO)Class
getClass(String discriminator)
<T> Class<T>
getClass(org.bson.Document document)
Gets the class as defined by any discriminator fieldList<MappedClass>
getClassesMappedToCollection(String collection)
Finds all the types mapped to a named collection<T> Class<T>
getClassFromCollection(String collection)
Looks up the class mapped to a named collection.org.bson.codecs.configuration.CodecRegistry
getCodecRegistry()
<T> com.mongodb.client.MongoCollection<T>
getCollection(Class<T> type)
DiscriminatorLookup
getDiscriminatorLookup()
Object
getId(Object entity)
Gets the ID value for an entityCollection<EntityInterceptor>
getInterceptors()
Gets list ofEntityInterceptor
s<T> Key<T>
getKey(T entity)
Deprecated, for removal: This API element is subject to removal in a future version.<T> Key<T>
getKey(T entity, String collection)
Deprecated, for removal: This API element is subject to removal in a future version.MappedClass
getMappedClass(Class type)
Gets theMappedClass
for the object (type).Collection<MappedClass>
getMappedClasses()
MapperOptions
getOptions()
List<MappedClass>
getSubTypes(MappedClass mc)
Finds any subtypes for the given MappedClass.com.mongodb.WriteConcern
getWriteConcern(Class clazz)
Gets the write concern for entity or returns the default write concern for this datastoreboolean
hasInterceptors()
<T> boolean
isMappable(Class<T> type)
Checks if a type is mappable or notboolean
isMapped(Class c)
Checks to see if a Class has been mapped.List<MappedClass>
map(Class... entityClasses)
Maps a set of classesList<MappedClass>
map(List<Class> classes)
Maps a set of classesvoid
mapPackage(String packageName)
Tries to map all classes in the package specified.void
mapPackageFromClass(Class clazz)
Maps all the classes found in the package to which the given class belongs.<T> void
refresh(T entity)
Refreshes an entity with the current state in the database.void
setOptions(MapperOptions options)
Deprecated, for removal: This API element is subject to removal in a future version.no longer usedorg.bson.Document
toDocument(Object entity)
Converts an entity (POJO) to a Document.String
updateCollection(Key key)
Deprecated, for removal: This API element is subject to removal in a future version.
-
-
-
Field Detail
-
IGNORED_FIELDNAME
public static final 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.
-
-
Constructor Detail
-
Mapper
public Mapper(Datastore datastore, org.bson.codecs.configuration.CodecRegistry codecRegistry, MapperOptions options)
Creates a Mapper with the given options.- Parameters:
datastore
- the datastore to usecodecRegistry
- the codec registryoptions
- the options to use
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
-
Method Detail
-
addInterceptor
public void addInterceptor(EntityInterceptor ei)
Adds anEntityInterceptor
- Parameters:
ei
- the interceptor to add
-
createEntityModel
public <T> EntityModel<T> createEntityModel(Class<T> clazz)
- Type Parameters:
T
- type model type- Parameters:
clazz
- the model type- Returns:
- the new model
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
enforceWriteConcern
public com.mongodb.client.MongoCollection enforceWriteConcern(com.mongodb.client.MongoCollection collection, Class type)
Updates a collection to use a specific WriteConcern- Parameters:
collection
- the collection to updatetype
- the entity type- Returns:
- the updated collection
-
fromDocument
public <T> T fromDocument(Class<T> type, org.bson.Document document)
Converts a Document back to a type-safe java object (POJO)- Type Parameters:
T
- the type of the entity- Parameters:
type
- the target typedocument
- the Document containing the document from mongodb- 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.
-
getClass
public <T> Class<T> getClass(org.bson.Document document)
Gets the class as defined by any discriminator field- Type Parameters:
T
- the class type- Parameters:
document
- the document to check- Returns:
- the class reference. might be null
-
getClass
public Class getClass(String discriminator)
- Parameters:
discriminator
- the lookup value- Returns:
- the class mapped to this discrimiator value
-
getClassFromCollection
public <T> Class<T> getClassFromCollection(String collection)
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.
-
getClassesMappedToCollection
public List<MappedClass> getClassesMappedToCollection(String collection)
Finds all the types mapped to a named collection- Parameters:
collection
- the collection to check- Returns:
- the mapped types
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
getCodecRegistry
public org.bson.codecs.configuration.CodecRegistry getCodecRegistry()
- Returns:
- the codec registry
-
getCollection
public <T> com.mongodb.client.MongoCollection<T> getCollection(Class<T> type)
- Type Parameters:
T
- the class type- Parameters:
type
- the type look up- Returns:
- the collection mapped for this class
-
getDiscriminatorLookup
public DiscriminatorLookup getDiscriminatorLookup()
- Returns:
- the DiscriminatorLookup in use
-
getId
public Object getId(Object entity)
Gets the ID value for an entity- Parameters:
entity
- the entity to process- Returns:
- the ID value
-
getInterceptors
public Collection<EntityInterceptor> getInterceptors()
Gets list ofEntityInterceptor
s- Returns:
- the Interceptors
-
getKey
@Deprecated(since="2.0", forRemoval=true) public <T> Key<T> getKey(T entity)
Deprecated, for removal: This API element is subject to removal in a future version.Gets the Key for an entity- Type Parameters:
T
- the type of the entity- Parameters:
entity
- the entity to process- Returns:
- the Key
-
getKey
@Deprecated(since="2.0", forRemoval=true) public <T> Key<T> getKey(T entity, String collection)
Deprecated, for removal: This API element is subject to removal in a future version.Gets the Key for an entity and a specific collection- Type Parameters:
T
- the type of the entity- Parameters:
entity
- the entity to processcollection
- the collection to use in the Key rather than the mapped collection as defined on the entity's class- Returns:
- the Key
-
getMappedClass
public MappedClass getMappedClass(Class type)
Gets theMappedClass
for the object (type). If it isn't mapped, create a new class and cache it (without validating).- Parameters:
type
- the type to process- Returns:
- the MappedClass for the object given
-
map
public List<MappedClass> map(Class... entityClasses)
Maps a set of classes- Parameters:
entityClasses
- the classes to map- Returns:
- the MappedClass references
-
getMappedClasses
public Collection<MappedClass> getMappedClasses()
- Returns:
- collection of MappedClasses
-
getOptions
public MapperOptions getOptions()
- Returns:
- the options used by this Mapper
-
setOptions
@Deprecated(since="2.0", forRemoval=true) public void setOptions(MapperOptions options)
Deprecated, for removal: This API element is subject to removal in a future version.no longer usedSets the options this Mapper should use- Parameters:
options
- the options to use
-
getSubTypes
public 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
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
getWriteConcern
public com.mongodb.WriteConcern getWriteConcern(Class clazz)
Gets the write concern for entity or returns the default write concern for this datastore- Parameters:
clazz
- the class to use when looking up the WriteConcern- Returns:
- the write concern for the type
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
hasInterceptors
public boolean hasInterceptors()
- Returns:
- true if there are global interceptors defined
-
isMappable
public <T> boolean isMappable(Class<T> type)
Checks if a type is mappable or not- Type Parameters:
T
- the type- Parameters:
type
- the class to check- Returns:
- true if the type is mappable
-
isMapped
public boolean isMapped(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
-
map
public List<MappedClass> map(List<Class> classes)
Maps a set of classes- Parameters:
classes
- the classes to map- Returns:
- the list of mapped classes
-
mapPackage
public void mapPackage(String packageName)
Tries to map all classes in the package specified.- Parameters:
packageName
- the name of the package to process
-
mapPackageFromClass
public void mapPackageFromClass(Class clazz)
Maps all the classes found in the package to which the given class belongs.- Parameters:
clazz
- the class to use when trying to find others to map
-
refresh
public <T> void refresh(T entity)
Refreshes an entity with the current state in the database.- Type Parameters:
T
- the entity type- Parameters:
entity
- the entity to refresh
-
toDocument
public org.bson.Document toDocument(Object entity)
Converts an entity (POJO) to a Document. A special field will be added to keep track of the class type.- Parameters:
entity
- The POJO- Returns:
- the Document
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
updateCollection
@Deprecated(since="2.0", forRemoval=true) public String updateCollection(Key key)
Deprecated, for removal: This API element is subject to removal in a future version.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
-
-