org.datanucleus.store.mapped.mapping
Class JavaTypeMapping

java.lang.Object
  extended by org.datanucleus.store.mapped.mapping.JavaTypeMapping
Direct Known Subclasses:
MultiMapping, SingleFieldMapping, SingleFieldMultiMapping

public abstract class JavaTypeMapping
extends java.lang.Object

Representation of the mapping of a Java type. The java type maps to one or more datastore mappings. This means that a field/property in a java class can be mapped to many columns in a table (in an RDBMS). A JavaTypeMapping can exist in 2 forms


Field Summary
protected  DatastoreContainerObject datastoreContainer
          The Datastore Container storing this mapping.
protected  DatastoreMapping[] datastoreMappings
          The Datastore mappings for this Java type.
protected static Localiser LOCALISER
          Localiser for messages
protected  AbstractMemberMetaData mmd
          MetaData for the field/property that we are mapping.
protected  JavaTypeMapping referenceMapping
          Mapping of the reference on the end of a bidirectional association.
protected  int roleForMember
          Role of the mapping for the field.
protected  MappedStoreManager storeMgr
          StoreManager for the datastore being used.
protected  java.lang.String type
          Actual type being mapped
 
Constructor Summary
protected JavaTypeMapping()
          Create a new empty JavaTypeMapping.
 
Method Summary
 void addDatastoreMapping(DatastoreMapping datastoreMapping)
          Method to add a datastore mapping
 boolean equals(java.lang.Object obj)
          Equality operator.
protected  java.lang.String failureMessage(java.lang.String method)
          Utility to output any error message.
 boolean getBoolean(ExecutionContext ec, java.lang.Object datastoreResults, int[] exprIndex)
          Obtains a value from datastoreResults at position specified by exprIndex.
 byte getByte(ExecutionContext ec, java.lang.Object datastoreResults, int[] exprIndex)
          Obtains a value from datastoreResults at position specified by exprIndex.
 char getChar(ExecutionContext ec, java.lang.Object datastoreResults, int[] exprIndex)
          Obtains a value from datastoreResults at position specified by exprIndex.
protected static ColumnMetaData[] getColumnMetaDataForMember(AbstractMemberMetaData mmd, int role)
          Convenience method to return the ColumnMetaData appropriate for this mapping.
 DatastoreContainerObject getDatastoreContainer()
          Accessor for the datastore class (e.g in an RDBMS context, the Table).
 DatastoreMapping getDatastoreMapping(int index)
          Accessor for a datastore mapping
 DatastoreMapping[] getDatastoreMappings()
          Accessor for the datastore mappings for this java type
 double getDouble(ExecutionContext ec, java.lang.Object datastoreResults, int[] exprIndex)
          Obtains a value from datastoreResults at position specified by exprIndex.
 float getFloat(ExecutionContext ec, java.lang.Object datastoreResults, int[] exprIndex)
          Obtains a value from datastoreResults at position specified by exprIndex.
 int getInt(ExecutionContext ec, java.lang.Object datastoreResults, int[] exprIndex)
          Obtains a value from datastoreResults at position specified by exprIndex.
abstract  java.lang.Class getJavaType()
          Accessor for the java type being mapped.
 java.lang.String getJavaTypeForDatastoreMapping(int index)
          Accessor for the name of the java-type actually used when mapping the particular datastore field.
 long getLong(ExecutionContext ec, java.lang.Object datastoreResults, int[] exprIndex)
          Obtains a value from datastoreResults at position specified by exprIndex.
 AbstractMemberMetaData getMemberMetaData()
          Accessor for the MetaData of the field/property to be mapped.
 int getNumberOfDatastoreMappings()
          Acessor for the number of datastore mappings.
 java.lang.Object getObject(ExecutionContext ec, java.lang.Object datastoreResults, int[] exprIndex)
          Obtains a value from datastoreResults at position specified by exprIndex.
 java.lang.Object getObject(ExecutionContext ec, java.lang.Object datastoreResults, int[] exprIndex, ObjectProvider ownerSM, int ownerFieldNumber)
          Obtains a value from datastoreResults at position specified by exprIndex.
 JavaTypeMapping getReferenceMapping()
          Accessor for the mapping at the other end of a relation when this field is part of a 1-1, 1-N, M-N (bidirectional) relation.
 int getRoleForMember()
          Accessor for the role of this mapping for the field/property.
 short getShort(ExecutionContext ec, java.lang.Object datastoreResults, int[] exprIndex)
          Obtains a value from datastoreResults at position specified by exprIndex.
 java.lang.String getString(ExecutionContext ec, java.lang.Object datastoreResults, int[] exprIndex)
          Obtains a value from datastoreResults at position specified by exprIndex.
 java.lang.String getType()
          Accessor for the class name of the object that is being mapped here.
 java.lang.Object getValueForDatastoreMapping(OMFContext omfCtx, int index, java.lang.Object value)
          Method to return the value to be stored in the specified datastore index given the overall value for this java type.
 int hashCode()
          Hash code function.
 boolean hasSimpleDatastoreRepresentation()
          Whether the mapping has a simple (single column) datastore representation.
 boolean includeInFetchStatement()
          Accessor for whether this mapping is to be included in any fetch statement.
 boolean includeInInsertStatement()
          Accessor for whether this mapping is to be included in the insert statement.
 boolean includeInUpdateStatement()
          Accessor for whether this mapping is to be included in the update statement.
 void initialize(AbstractMemberMetaData mmd, DatastoreContainerObject container, ClassLoaderResolver clr)
          Initialize this JavaTypeMapping for the supplied table and field/property metadata.
 void initialize(MappedStoreManager storeMgr, java.lang.String type)
          Initialize this JavaTypeMapping with the given StoreManager for the given type.
 boolean isNullable()
          Accessor for whether this mapping is nullable
 boolean isSerialised()
          Convenience method to return if the (part of the) field being represented by this mapping is serialised.
 void setBoolean(ExecutionContext ec, java.lang.Object datastoreStatement, int[] exprIndex, boolean value)
          Sets a value into datastoreStatement at position specified by exprIndex.
 void setByte(ExecutionContext ec, java.lang.Object datastoreStatement, int[] exprIndex, byte value)
          Sets a value into datastoreStatement at position specified by exprIndex.
 void setChar(ExecutionContext ec, java.lang.Object datastoreStatement, int[] exprIndex, char value)
          Sets a value into datastoreStatement at position specified by exprIndex.
 void setDatastoreContainer(DatastoreContainerObject table)
           
 void setDouble(ExecutionContext ec, java.lang.Object datastoreStatement, int[] exprIndex, double value)
          Sets a value into datastoreStatement at position specified by exprIndex.
 void setFloat(ExecutionContext ec, java.lang.Object datastoreStatement, int[] exprIndex, float value)
          Sets a value into datastoreStatement at position specified by exprIndex.
 void setInt(ExecutionContext ec, java.lang.Object datastoreStatement, int[] exprIndex, int value)
          Sets a value into datastoreStatement at position specified by exprIndex.
 void setLong(ExecutionContext ec, java.lang.Object datastoreStatement, int[] exprIndex, long value)
          Sets a value into datastoreStatement at position specified by exprIndex.
 void setMemberMetaData(AbstractMemberMetaData mmd)
          Method to set the metadata of the member for which this mapping applies.
 void setObject(ExecutionContext ec, java.lang.Object datastoreStatement, int[] exprIndex, java.lang.Object value)
          Sets a value into datastoreStatement at position specified by exprIndex.
 void setObject(ExecutionContext ec, java.lang.Object datastoreStatement, int[] exprIndex, java.lang.Object value, ObjectProvider ownerSM, int ownerFieldNumber)
          Sets a value into datastoreStatement at position specified by exprIndex.
 void setReferenceMapping(JavaTypeMapping referenceMapping)
          Method to set the mapping at the other end of the relation.
 void setRoleForMember(int role)
          Method to set the role for the field/property.
 void setShort(ExecutionContext ec, java.lang.Object datastoreStatement, int[] exprIndex, short value)
          Sets a value into datastoreStatement at position specified by exprIndex.
 void setString(ExecutionContext ec, java.lang.Object datastoreStatement, int[] exprIndex, java.lang.String value)
          Sets a value into datastoreStatement at position specified by exprIndex.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCALISER

protected static final Localiser LOCALISER
Localiser for messages


mmd

protected AbstractMemberMetaData mmd
MetaData for the field/property that we are mapping. Null when it applies to a query parameter.


roleForMember

protected int roleForMember
Role of the mapping for the field. Whether it is for the field as a whole, or element of a collection field (in a join table), or key/value of a map field (in a join table).


datastoreMappings

protected DatastoreMapping[] datastoreMappings
The Datastore mappings for this Java type.


datastoreContainer

protected DatastoreContainerObject datastoreContainer
The Datastore Container storing this mapping. Null when it applies to a query parameter.


storeMgr

protected MappedStoreManager storeMgr
StoreManager for the datastore being used.


type

protected java.lang.String type
Actual type being mapped


referenceMapping

protected JavaTypeMapping referenceMapping
Mapping of the reference on the end of a bidirectional association. Only used when this mapping doesn't have datastore fields, but the other side has.

Constructor Detail

JavaTypeMapping

protected JavaTypeMapping()
Create a new empty JavaTypeMapping. The caller must call one of the initialize() methods to initialise the instance with the DatastoreAdapter and its type. The combination of this empty constructor and one of the initialize() methods is used instead of parameterised constructors for efficiency purpose, both in execution time and code maintainability. See MappingFactory for how they are used. Concrete subclasses must have a publicly accessible empty constructor.

Method Detail

initialize

public void initialize(MappedStoreManager storeMgr,
                       java.lang.String type)
Initialize this JavaTypeMapping with the given StoreManager for the given type. Used when the mapping is for a parameter in a query. This will not set the "mmd" and "datastoreContainer" parameters. If these are required for usage of the mapping then you should call "setFieldInformation(AbstractMemberMetaData, DatastoreContainerObject)" below Subclasses should override this method to perform any datastore initialization operations.

Parameters:
storeMgr - The Datastore Adapter that this Mapping should use.
type - The Class that this mapping maps to the database.
See Also:
MappingFactory.createMapping(Class, MappedStoreManager, String)

initialize

public void initialize(AbstractMemberMetaData mmd,
                       DatastoreContainerObject container,
                       ClassLoaderResolver clr)
Initialize this JavaTypeMapping for the supplied table and field/property metadata. Subclasses should override this method to perform any datastore initialization operations. Assumes the "roleForMember" is already set

Parameters:
mmd - MetaData for the field/property to be mapped (if any)
container - The datastore container storing this mapping (if any)
clr - the ClassLoaderResolver

hashCode

public int hashCode()
Hash code function.

Overrides:
hashCode in class java.lang.Object
Returns:
The hash code for this object

equals

public boolean equals(java.lang.Object obj)
Equality operator.

Overrides:
equals in class java.lang.Object
Parameters:
obj - Object to compare against
Returns:
Whether they are equal

setMemberMetaData

public void setMemberMetaData(AbstractMemberMetaData mmd)
Method to set the metadata of the member for which this mapping applies. For use where the mapping was created for a particular type (using the initialize(DatastoreAdapter, String) and we now have the member that it applies for.

Parameters:
mmd - Field/Property MetaData

setDatastoreContainer

public void setDatastoreContainer(DatastoreContainerObject table)

setRoleForMember

public void setRoleForMember(int role)
Method to set the role for the field/property. Should be called before initialize().

Parameters:
role - Role for field/property.

getMemberMetaData

public AbstractMemberMetaData getMemberMetaData()
Accessor for the MetaData of the field/property to be mapped. Will be null if this mapping is for a literal in a query.

Returns:
Returns the metadata for the field or property

getRoleForMember

public int getRoleForMember()
Accessor for the role of this mapping for the field/property.

Returns:
Role of this mapping for the field/property

isSerialised

public boolean isSerialised()
Convenience method to return if the (part of the) field being represented by this mapping is serialised.

Returns:
Whether to use Java serialisation

isNullable

public boolean isNullable()
Accessor for whether this mapping is nullable

Returns:
Whether it is nullable

hasSimpleDatastoreRepresentation

public boolean hasSimpleDatastoreRepresentation()
Whether the mapping has a simple (single column) datastore representation.

Returns:
Whether it has a simple datastore representation (single column)

getDatastoreMappings

public DatastoreMapping[] getDatastoreMappings()
Accessor for the datastore mappings for this java type

Returns:
The datastore mapping(s)

getDatastoreContainer

public DatastoreContainerObject getDatastoreContainer()
Accessor for the datastore class (e.g in an RDBMS context, the Table). Will be null if this mapping is for a literal in a query.

Returns:
The datastore class containing this mapped field.

getDatastoreMapping

public DatastoreMapping getDatastoreMapping(int index)
Accessor for a datastore mapping

Parameters:
index - The id of the mapping
Returns:
The datastore mapping

getValueForDatastoreMapping

public java.lang.Object getValueForDatastoreMapping(OMFContext omfCtx,
                                                    int index,
                                                    java.lang.Object value)
Method to return the value to be stored in the specified datastore index given the overall value for this java type. All multi-column mappings must override this.

Parameters:
omfCtx - OMF Context
index - The datastore index
value - The overall value for this java type
Returns:
The value for this datastore index

getReferenceMapping

public JavaTypeMapping getReferenceMapping()
Accessor for the mapping at the other end of a relation when this field is part of a 1-1, 1-N, M-N (bidirectional) relation. Will be null otherwise.

Returns:
The mapping at the other end.

setReferenceMapping

public void setReferenceMapping(JavaTypeMapping referenceMapping)
Method to set the mapping at the other end of the relation. Only used when part of a (bidirectional) relation.

Parameters:
referenceMapping - The mapping at the other end

addDatastoreMapping

public void addDatastoreMapping(DatastoreMapping datastoreMapping)
Method to add a datastore mapping

Parameters:
datastoreMapping - The datastore mapping

getNumberOfDatastoreMappings

public int getNumberOfDatastoreMappings()
Acessor for the number of datastore mappings. This typically equates to the number of columns

Returns:
the number of datastore mappings

getJavaType

public abstract java.lang.Class getJavaType()
Accessor for the java type being mapped. This is the java type that the mapping represents. Some examples : The "java type" is the java-type name used in the plugin.xml mapping file

Returns:
The java type

getJavaTypeForDatastoreMapping

public java.lang.String getJavaTypeForDatastoreMapping(int index)
Accessor for the name of the java-type actually used when mapping the particular datastore field. This java-type must have an entry in the datastore mappings. The default implementation throws an UnsupportedOperationException.

Parameters:
index - requested datastore field index.
Returns:
the name of java-type for the requested datastore field.

getType

public java.lang.String getType()
Accessor for the class name of the object that is being mapped here. There are mainly two situations:

Returns:
The actual type that this Mapping maps.

includeInFetchStatement

public boolean includeInFetchStatement()
Accessor for whether this mapping is to be included in any fetch statement.

Returns:
Whether to include this mapping in a fetch statement

includeInUpdateStatement

public boolean includeInUpdateStatement()
Accessor for whether this mapping is to be included in the update statement.

Returns:
Whether to include in update statement

includeInInsertStatement

public boolean includeInInsertStatement()
Accessor for whether this mapping is to be included in the insert statement.

Returns:
Whether to include in insert statement

failureMessage

protected java.lang.String failureMessage(java.lang.String method)
Utility to output any error message.

Parameters:
method - The method that failed.
Returns:
the localised failure message

setBoolean

public void setBoolean(ExecutionContext ec,
                       java.lang.Object datastoreStatement,
                       int[] exprIndex,
                       boolean value)
Sets a value into datastoreStatement at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreStatement - a datastore object that executes statements in the database
exprIndex - the position of the value in the statement
value - the value

getBoolean

public boolean getBoolean(ExecutionContext ec,
                          java.lang.Object datastoreResults,
                          int[] exprIndex)
Obtains a value from datastoreResults at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreResults - an object returned from the datastore with values
exprIndex - the position of the value in the result
Returns:
the value

setChar

public void setChar(ExecutionContext ec,
                    java.lang.Object datastoreStatement,
                    int[] exprIndex,
                    char value)
Sets a value into datastoreStatement at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreStatement - a datastore object that executes statements in the database
exprIndex - the position of the value in the statement
value - the value

getChar

public char getChar(ExecutionContext ec,
                    java.lang.Object datastoreResults,
                    int[] exprIndex)
Obtains a value from datastoreResults at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreResults - an object returned from the datastore with values
exprIndex - the position of the value in the result
Returns:
the value

setByte

public void setByte(ExecutionContext ec,
                    java.lang.Object datastoreStatement,
                    int[] exprIndex,
                    byte value)
Sets a value into datastoreStatement at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreStatement - a datastore object that executes statements in the database
exprIndex - the position of the value in the statement
value - the value

getByte

public byte getByte(ExecutionContext ec,
                    java.lang.Object datastoreResults,
                    int[] exprIndex)
Obtains a value from datastoreResults at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreResults - an object returned from the datastore with values
exprIndex - the position of the value in the result
Returns:
the value

setShort

public void setShort(ExecutionContext ec,
                     java.lang.Object datastoreStatement,
                     int[] exprIndex,
                     short value)
Sets a value into datastoreStatement at position specified by exprIndex.

Parameters:
ec - execution context
datastoreStatement - a datastore object that executes statements in the database
exprIndex - the position of the value in the statement
value - the value

getShort

public short getShort(ExecutionContext ec,
                      java.lang.Object datastoreResults,
                      int[] exprIndex)
Obtains a value from datastoreResults at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreResults - an object returned from the datastore with values
exprIndex - the position of the value in the result
Returns:
the value

setInt

public void setInt(ExecutionContext ec,
                   java.lang.Object datastoreStatement,
                   int[] exprIndex,
                   int value)
Sets a value into datastoreStatement at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreStatement - a datastore object that executes statements in the database
exprIndex - the position of the value in the statement
value - the value

getInt

public int getInt(ExecutionContext ec,
                  java.lang.Object datastoreResults,
                  int[] exprIndex)
Obtains a value from datastoreResults at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreResults - an object returned from the datastore with values
exprIndex - the position of the value in the result
Returns:
the value

setLong

public void setLong(ExecutionContext ec,
                    java.lang.Object datastoreStatement,
                    int[] exprIndex,
                    long value)
Sets a value into datastoreStatement at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreStatement - a datastore object that executes statements in the database
exprIndex - the position of the value in the statement
value - the value

getLong

public long getLong(ExecutionContext ec,
                    java.lang.Object datastoreResults,
                    int[] exprIndex)
Obtains a value from datastoreResults at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreResults - an object returned from the datastore with values
exprIndex - the position of the value in the result
Returns:
the value

setFloat

public void setFloat(ExecutionContext ec,
                     java.lang.Object datastoreStatement,
                     int[] exprIndex,
                     float value)
Sets a value into datastoreStatement at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreStatement - a datastore object that executes statements in the database
exprIndex - the position of the value in the statement
value - the value

getFloat

public float getFloat(ExecutionContext ec,
                      java.lang.Object datastoreResults,
                      int[] exprIndex)
Obtains a value from datastoreResults at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreResults - an object returned from the datastore with values
exprIndex - the position of the value in the result
Returns:
the value

setDouble

public void setDouble(ExecutionContext ec,
                      java.lang.Object datastoreStatement,
                      int[] exprIndex,
                      double value)
Sets a value into datastoreStatement at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreStatement - a datastore object that executes statements in the database
exprIndex - the position of the value in the statement
value - the value

getDouble

public double getDouble(ExecutionContext ec,
                        java.lang.Object datastoreResults,
                        int[] exprIndex)
Obtains a value from datastoreResults at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreResults - an object returned from the datastore with values
exprIndex - the position of the value in the result
Returns:
the value

setString

public void setString(ExecutionContext ec,
                      java.lang.Object datastoreStatement,
                      int[] exprIndex,
                      java.lang.String value)
Sets a value into datastoreStatement at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreStatement - a datastore object that executes statements in the database
exprIndex - the position of the value in the statement
value - the value

getString

public java.lang.String getString(ExecutionContext ec,
                                  java.lang.Object datastoreResults,
                                  int[] exprIndex)
Obtains a value from datastoreResults at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreResults - an object returned from the datastore with values
exprIndex - the position of the value in the result
Returns:
the value

setObject

public void setObject(ExecutionContext ec,
                      java.lang.Object datastoreStatement,
                      int[] exprIndex,
                      java.lang.Object value,
                      ObjectProvider ownerSM,
                      int ownerFieldNumber)
Sets a value into datastoreStatement at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreStatement - a datastore object that executes statements in the database
exprIndex - the position of the value in the statement
value - the value
ownerSM - the owner StateManager
ownerFieldNumber - the owner absolute field number

setObject

public void setObject(ExecutionContext ec,
                      java.lang.Object datastoreStatement,
                      int[] exprIndex,
                      java.lang.Object value)
Sets a value into datastoreStatement at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreStatement - a datastore object that executes statements in the database
exprIndex - the position of the value in the statement
value - the value

getObject

public java.lang.Object getObject(ExecutionContext ec,
                                  java.lang.Object datastoreResults,
                                  int[] exprIndex,
                                  ObjectProvider ownerSM,
                                  int ownerFieldNumber)
Obtains a value from datastoreResults at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreResults - an object returned from the datastore with values
exprIndex - the position of the value in the result
ownerSM - the owner StateManager
ownerFieldNumber - the owner absolute field number
Returns:
the value

getObject

public java.lang.Object getObject(ExecutionContext ec,
                                  java.lang.Object datastoreResults,
                                  int[] exprIndex)
Obtains a value from datastoreResults at position specified by exprIndex.

Parameters:
ec - ExecutionContext
datastoreResults - an object returned from the datastore with values
exprIndex - the position of the value in the result
Returns:
the value

getColumnMetaDataForMember

protected static ColumnMetaData[] getColumnMetaDataForMember(AbstractMemberMetaData mmd,
                                                             int role)
Convenience method to return the ColumnMetaData appropriate for this mapping. If the mapping is in a join table then picks the correct component for the column definition.

Returns:
The column metadata (if any)


Copyright © 2011. All Rights Reserved.