public class Mapper extends Object
This is the heart of Morphia and takes care of mapping from/to POJOs/DBObjects
This class is thread-safe and keeps various "cached" data which should speed up processing.
| Modifier and Type | Field and Description |
|---|---|
static String |
CLASS_NAME_FIELDNAME
Special field used by morphia to support various possibly loading issues; will be replaced when discriminators are implemented to
support polymorphism
|
static String |
ID_KEY
The @
Id field name that is stored with mongodb. |
static String |
IGNORED_FIELDNAME
Special name that can never be used.
|
| Constructor and Description |
|---|
Mapper()
Creates a Mapper with default options
|
Mapper(MapperOptions opts)
Creates a Mapper with the given options.
|
Mapper(MapperOptions options,
Mapper mapper)
Creates a new Mapper with the given options and a Mapper to copy.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addInterceptor(EntityInterceptor ei)
Adds an
EntityInterceptor |
MappedClass |
addMappedClass(Class c)
Creates a MappedClass and validates it.
|
EntityCache |
createEntityCache()
Creates a cache for tracking entities seen during processing
|
<T> T |
fromDb(Datastore datastore,
DBObject dbObject,
T entity,
EntityCache cache)
Converts a DBObject back to a type-safe java object (POJO)
|
<T> T |
fromDBObject(Datastore datastore,
Class<T> entityClass,
DBObject dbObject,
EntityCache cache)
Converts a DBObject back to a type-safe java object (POJO)
|
Class<?> |
getClassFromCollection(String collection)
Looks up the class mapped to a named collection.
|
String |
getCollectionName(Object object)
Gets the mapped collection for an object instance or Class reference.
|
Converters |
getConverters() |
Object |
getId(Object entity)
Gets the ID value for an entity
|
Map<Class,Object> |
getInstanceCache() |
Collection<EntityInterceptor> |
getInterceptors()
Gets list of
EntityInterceptors |
<T> Key<T> |
getKey(T entity)
Gets the Key for an entity
|
<T> Key<T> |
getKey(T entity,
String collection)
Gets the Key for an entity and a specific collection
|
<T> List<Key<T>> |
getKeysByManualRefs(Class<T> clazz,
List<Object> refs)
Gets the Keys for a list of objects
|
<T> List<Key<T>> |
getKeysByRefs(List<DBRef> refs)
Gets the Keys for a list of objects
|
MappedClass |
getMappedClass(Object obj)
Gets the
MappedClass for the object (type). |
Collection<MappedClass> |
getMappedClasses() |
Map<String,MappedClass> |
getMCMap() |
MapperOptions |
getOptions() |
protected LazyProxyFactory |
getProxyFactory() |
boolean |
isMapped(Class c)
Checks to see if a Class has been mapped.
|
DBRef |
keyToDBRef(Key key)
Converts a Key to a DBRef
|
<T> Key<T> |
manualRefToKey(Class<T> type,
Object id)
Creates a Key for a type and an ID value
|
<T> Key<T> |
refToKey(DBRef ref)
Converts a DBRef to a Key
|
void |
setOptions(MapperOptions options)
Sets the options this Mapper should use
|
DBObject |
toDBObject(Object entity)
/**
Converts an entity (POJO) to a DBObject.
|
DBObject |
toDBObject(Object entity,
Map<Object,DBObject> involvedObjects)
Converts an entity (POJO) to a DBObject.
|
Object |
toMongoObject(MappedField mf,
MappedClass mc,
Object value)
Converts a java object to a mongo-compatible object (possibly a DBObject for complex mappings).
|
String |
updateCollection(Key key)
Updates the collection value on a Key with the mapped value on the Key's type Class
|
void |
updateKeyAndVersionInfo(Datastore datastore,
DBObject dbObj,
EntityCache cache,
Object entity)
|
public static final String ID_KEY
Id field name that is stored with mongodb.public static final String IGNORED_FIELDNAME
public static final String CLASS_NAME_FIELDNAME
public Mapper(MapperOptions opts)
opts - the options to usepublic Mapper()
MapperOptionspublic Mapper(MapperOptions options, Mapper mapper)
options - the options to usemapper - the collection of MappedClasses to addpublic void addInterceptor(EntityInterceptor ei)
EntityInterceptorei - the interceptor to addpublic MappedClass addMappedClass(Class c)
c - the Class to mappublic EntityCache createEntityCache()
public <T> T fromDBObject(Datastore datastore, Class<T> entityClass, DBObject dbObject, EntityCache cache)
T - the type of the entitydatastore - the Datastore to use when fetching this referenceentityClass - The type to return, or use; can be overridden by the @see Mapper.CLASS_NAME_FIELDNAME in the DBObjectdbObject - the DBObject containing the document from mongodbcache - the EntityCache to useCLASS_NAME_FIELDNAMEpublic <T> T fromDb(Datastore datastore, DBObject dbObject, T entity, EntityCache cache)
T - the type of the entitydatastore - the Datastore to use when fetching this referencedbObject - the DBObject containing the document from mongodbentity - the instance to populatecache - the EntityCache to usepublic Class<?> getClassFromCollection(String collection)
collection - the collection namepublic String getCollectionName(Object object)
object - the object to processpublic Converters getConverters()
public Object getId(Object entity)
entity - the entity to processpublic Collection<EntityInterceptor> getInterceptors()
EntityInterceptorspublic <T> Key<T> getKey(T entity)
T - the type of the entityentity - the entity to processpublic <T> Key<T> getKey(T entity, String collection)
T - the type of the entityentity - the entity to processcollection - the collection to use in the Key rather than the mapped collection as defined on the entity's classpublic <T> List<Key<T>> getKeysByManualRefs(Class<T> clazz, List<Object> refs)
T - the type of the entityclazz - the Class of the objectsrefs - the objects to fetch the keys forpublic <T> List<Key<T>> getKeysByRefs(List<DBRef> refs)
T - the type of the objectsrefs - the objects to processpublic Map<String,MappedClass> getMCMap()
public MappedClass getMappedClass(Object obj)
MappedClass for the object (type). If it isn't mapped, create a new class and cache it (without validating).obj - the object to processpublic Collection<MappedClass> getMappedClasses()
public MapperOptions getOptions()
public void setOptions(MapperOptions options)
options - the options to usepublic boolean isMapped(Class c)
c - the Class to checkpublic DBRef keyToDBRef(Key key)
key - the Key to convertpublic <T> Key<T> manualRefToKey(Class<T> type, Object id)
T - the type of the entitytype - the Class of the entityid - the ID valuepublic <T> Key<T> refToKey(DBRef ref)
T - the type of the referenced entityref - the DBRef to convertpublic DBObject toDBObject(Object entity)
entity - The POJOCLASS_NAME_FIELDNAMEpublic DBObject toDBObject(Object entity, Map<Object,DBObject> involvedObjects)
entity - The POJOinvolvedObjects - A Map of (already converted) POJOsCLASS_NAME_FIELDNAMEpublic Object toMongoObject(MappedField mf, MappedClass mc, Object value)
toDBObject(java.lang.Object). Used (mainly) by query/update operations.mf - the MappedField for this valuemc - the MappedClass for this valuevalue - the value to convertpublic String updateCollection(Key key)
key - the Key to updatepublic void updateKeyAndVersionInfo(Datastore datastore, DBObject dbObj, EntityCache cache, Object entity)
datastore - the Datastore to use when fetching this referencedbObj - Value to update with; null means skipcache - the EntityCacheentity - The object to updateprotected LazyProxyFactory getProxyFactory()