Class JavaTypeMapping
java.lang.Object
org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping
- Direct Known Subclasses:
MultiMapping
,SerialisedLocalFileMapping
,SingleCollectionMapping
,SingleFieldMapping
,SingleFieldMultiMapping
Representation of the mapping of a Java type. The java type maps to one or more column mappings.
This means that a field/property in a java class can be mapped to many columns in a table.
A JavaTypeMapping can exist in 2 forms
- Constructed for a field/property managed by a datastore container, and so has metadata/container information
- Constructed to represent a parameter in a query, so has no metadata and container information.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int
Absolute field number for this mapping.protected ColumnMapping[]
The Column mappings for this Java type.protected org.datanucleus.metadata.AbstractMemberMetaData
MetaData for the field/property that we are mapping.protected JavaTypeMapping
Mapping of the reference on the end of a bidirectional association.protected org.datanucleus.metadata.FieldRole
Role of the mapping for the field.protected RDBMSStoreManager
StoreManager for the datastore being used.protected Table
The Table storing this mapping.protected String
Actual type being mapped -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Method to add a column mappingboolean
Equality operator.protected String
failureMessage
(String method) Utility to output any error message.protected int
boolean
getBoolean
(org.datanucleus.ExecutionContext ec, ResultSet rs, int[] exprIndex) Obtains a value fromdatastoreResults
at position specified byexprIndex
.byte
Obtains a value fromdatastoreResults
at position specified byexprIndex
.char
Obtains a value fromdatastoreResults
at position specified byexprIndex
.getColumnMapping
(int index) Accessor for a column mappingAccessor for the column mappings for this java typeprotected static org.datanucleus.metadata.ColumnMetaData[]
getColumnMetaDataForMember
(org.datanucleus.metadata.AbstractMemberMetaData mmd, org.datanucleus.metadata.FieldRole role) Convenience method to return the ColumnMetaData appropriate for this mapping.double
Obtains a value fromdatastoreResults
at position specified byexprIndex
.float
Obtains a value fromdatastoreResults
at position specified byexprIndex
.int
Obtains a value fromdatastoreResults
at position specified byexprIndex
.abstract Class
Accessor for the java type being mapped.getJavaTypeForColumnMapping
(int index) Accessor for the name of the java-type actually used when mapping the particular column.long
Obtains a value fromdatastoreResults
at position specified byexprIndex
.org.datanucleus.metadata.AbstractMemberMetaData
Accessor for the MetaData of the field/property being mapped.int
Accessor for the number of column mappings.Obtains a value fromdatastoreResults
at position specified byexprIndex
.getObject
(org.datanucleus.ExecutionContext ec, ResultSet rs, int[] exprIndex, org.datanucleus.state.DNStateManager ownerSM, int ownerFieldNumber) Obtains a value from the result set at position specified byexprIndex
based on being embedded/serialised into the provided owner.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.org.datanucleus.metadata.FieldRole
Accessor for the role of this mapping for the field/property.short
Obtains a value fromdatastoreResults
at position specified byexprIndex
.Obtains a value fromdatastoreResults
at position specified byexprIndex
.getTable()
Accessor for the table.getType()
Accessor for the class name of the object that is being mapped here.getValueForColumnMapping
(org.datanucleus.NucleusContext nucleusCtx, int index, Object value) Method to return the value to be stored in the specified column index given the overall value for this java type.int
hashCode()
Hash code function.boolean
Whether the mapping has a simple (single column) datastore representation.boolean
Accessor for whether this mapping is to be included in any fetch statement.boolean
Accessor for whether this mapping is to be included in the insert statement.boolean
Accessor for whether this mapping is to be included in the update statement.void
initialize
(org.datanucleus.metadata.AbstractMemberMetaData mmd, Table table, org.datanucleus.ClassLoaderResolver clr) Initialize this JavaTypeMapping for the supplied table and field/property metadata.void
initialize
(RDBMSStoreManager storeMgr, String type) Initialise this JavaTypeMapping with the given StoreManager for the given type.boolean
Accessor for whether this mapping is nullableboolean
Convenience method to return if the (part of the) field being represented by this mapping is serialised.void
performSetPostProcessing
(org.datanucleus.state.DNStateManager sm) Perform any INSERT/UPDATE post processing as required by constituent ColumnMappings.boolean
The vast majority of types can be added to a (SQL) statement in their String form and the statement would operate ok.boolean
Accessor for whether any of the column mappings requires INSERT/UPDATE post processing.void
setAbsFieldNumber
(int num) void
setBoolean
(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, boolean value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.void
setByte
(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, byte value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.void
setChar
(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, char value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.void
setDouble
(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, double value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.void
setFloat
(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, float value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.void
setInt
(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, int value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.void
setLong
(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, long value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.void
setMemberMetaData
(org.datanucleus.metadata.AbstractMemberMetaData mmd) Method to set the metadata of the member for which this mapping applies.void
setObject
(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, Object value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.void
setObject
(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, Object value, org.datanucleus.state.DNStateManager ownerSM, int ownerFieldNumber) Sets avalue
intops
at position specified byexprIndex
when embedded/serialised into the specified owner.void
setReferenceMapping
(JavaTypeMapping referenceMapping) Method to set the mapping at the other end of the relation.void
setRoleForMember
(org.datanucleus.metadata.FieldRole role) Method to set the role for the field/property.void
setShort
(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, short value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.void
setString
(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, String value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.void
-
Field Details
-
mmd
protected org.datanucleus.metadata.AbstractMemberMetaData mmdMetaData for the field/property that we are mapping. Null when it applies to a query parameter. -
roleForMember
protected org.datanucleus.metadata.FieldRole roleForMemberRole 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). -
columnMappings
The Column mappings for this Java type. -
table
The Table storing this mapping. Null when it applies to a query parameter. -
storeMgr
StoreManager for the datastore being used. -
type
Actual type being mapped -
referenceMapping
Mapping of the reference on the end of a bidirectional association. Only used when this mapping doesn't have columns, but the other side has. -
absFieldNumber
protected int absFieldNumberAbsolute field number for this mapping. Will match "mmd" unless this is an embedded field.
-
-
Constructor Details
-
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 Details
-
initialize
Initialise 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.
-
initialize
public void initialize(org.datanucleus.metadata.AbstractMemberMetaData mmd, Table table, org.datanucleus.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)table
- The table storing this mapping (if any)clr
- the ClassLoaderResolver
-
hashCode
public int hashCode()Hash code function. -
equals
Equality operator. -
setMemberMetaData
public void setMemberMetaData(org.datanucleus.metadata.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(StoreManager, String) and we now have the member that it applies for.- Parameters:
mmd
- Field/Property MetaData
-
getMemberMetaData
public org.datanucleus.metadata.AbstractMemberMetaData getMemberMetaData()Accessor for the MetaData of the field/property being mapped. Will be null if this mapping is for a literal in a query.- Returns:
- Returns the metadata for the field or property
-
getStoreManager
-
setTable
-
getTable
Accessor for the table. Will be null if this mapping is for a literal in a query.- Returns:
- The datastore class containing this mapped field.
-
getRoleForMember
public org.datanucleus.metadata.FieldRole getRoleForMember()Accessor for the role of this mapping for the field/property.- Returns:
- Role of this mapping for the field/property
-
setRoleForMember
public void setRoleForMember(org.datanucleus.metadata.FieldRole role) Method to set the role for the field/property. Should be called before initialize().- Parameters:
role
- Role for field/property.
-
getAbsoluteFieldNumber
protected int getAbsoluteFieldNumber() -
setAbsFieldNumber
public void setAbsFieldNumber(int num) -
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)
-
representableAsStringLiteralInStatement
public boolean representableAsStringLiteralInStatement()The vast majority of types can be added to a (SQL) statement in their String form and the statement would operate ok. Some types (e.g spatial types) cannot be used in their String form in a statement, so have to be represented as parameters to the statement.- Returns:
- Whether a literal of this type can be considered in its String form (otherwise has to be a parameter).
-
getColumnMappings
Accessor for the column mappings for this java type- Returns:
- The column mapping(s)
-
getColumnMapping
Accessor for a column mapping- Parameters:
index
- The id of the column- Returns:
- The column mapping
-
addColumnMapping
Method to add a column mapping- Parameters:
cm
- The column mapping
-
getNumberOfColumnMappings
public int getNumberOfColumnMappings()Accessor for the number of column mappings.- Returns:
- the number of column mappings
-
getValueForColumnMapping
public Object getValueForColumnMapping(org.datanucleus.NucleusContext nucleusCtx, int index, Object value) Method to return the value to be stored in the specified column index given the overall value for this java type. All multi-column mappings must override this.- Parameters:
nucleusCtx
- Contextindex
- The datastore indexvalue
- The overall value for this java type- Returns:
- The value for this datastore index
-
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
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
-
getJavaType
Accessor for the java type being mapped. This is the java type that the mapping represents. Some examples :- if the field is of type "MyClass" then the mapping will be OIDMapping (or subclass) the javaType will be OID, and the type will be MyClass.
- if the field is of type "int" then the mapping will be IntegerMapping, the javaType will be Integer, and the type will be int.
- Returns:
- The java type
-
getJavaTypeForColumnMapping
Accessor for the name of the java-type actually used when mapping the particular column. This java-type must have an entry in the column mappings. The default implementation throws an UnsupportedOperationException.- Parameters:
index
- requested column index.- Returns:
- the name of java-type for the requested column.
-
getType
Accessor for the class name of the object that is being mapped here. There are mainly two situations:- For a JavaTypeMapping that maps a persistable class field, this will return the type of the field. For example with a field of type "MyClass" this will return "MyClass"
- For a JavaTypeMapping that maps a variable or parameter in a query, this will return the type declared in the query.
- 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
Utility to output any error message.- Parameters:
method
- The method that failed.- Returns:
- the localised failure message
-
setBoolean
public void setBoolean(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, boolean value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextps
- PreparedStatementexprIndex
- the position of the value in the statementvalue
- the value
-
getBoolean
Obtains a value fromdatastoreResults
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextrs
- ResultSetexprIndex
- the position of the value in the result- Returns:
- the value
-
setChar
public void setChar(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, char value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextps
- PreparedStatementexprIndex
- the position of the value in the statementvalue
- the value
-
getChar
Obtains a value fromdatastoreResults
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextrs
- ResultSetexprIndex
- the position of the value in the result- Returns:
- the value
-
setByte
public void setByte(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, byte value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextps
- PreparedStatementexprIndex
- the position of the value in the statementvalue
- the value
-
getByte
Obtains a value fromdatastoreResults
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextrs
- ResultSetexprIndex
- the position of the value in the result- Returns:
- the value
-
setShort
public void setShort(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, short value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.- Parameters:
ec
- execution contextps
- PreparedStatementexprIndex
- the position of the value in the statementvalue
- the value
-
getShort
Obtains a value fromdatastoreResults
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextrs
- ResultSetexprIndex
- the position of the value in the result- Returns:
- the value
-
setInt
public void setInt(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, int value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextps
- PreparedStatementexprIndex
- the position of the value in the statementvalue
- the value
-
getInt
Obtains a value fromdatastoreResults
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextrs
- ResultSetexprIndex
- the position of the value in the result- Returns:
- the value
-
setLong
public void setLong(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, long value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextps
- PreparedStatementexprIndex
- the position of the value in the statementvalue
- the value
-
getLong
Obtains a value fromdatastoreResults
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextrs
- ResultSetexprIndex
- the position of the value in the result- Returns:
- the value
-
setFloat
public void setFloat(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, float value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextps
- PreparedStatementexprIndex
- the position of the value in the statementvalue
- the value
-
getFloat
Obtains a value fromdatastoreResults
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextrs
- ResultSetexprIndex
- the position of the value in the result- Returns:
- the value
-
setDouble
public void setDouble(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, double value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextps
- PreparedStatementexprIndex
- the position of the value in the statementvalue
- the value
-
getDouble
Obtains a value fromdatastoreResults
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextrs
- ResultSetexprIndex
- the position of the value in the result- Returns:
- the value
-
setString
public void setString(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, String value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextps
- PreparedStatementexprIndex
- the position of the value in the statementvalue
- the value
-
getString
Obtains a value fromdatastoreResults
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextrs
- ResultSetexprIndex
- the position of the value in the result- Returns:
- the value
-
setObject
public void setObject(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, Object value, org.datanucleus.state.DNStateManager ownerSM, int ownerFieldNumber) Sets avalue
intops
at position specified byexprIndex
when embedded/serialised into the specified owner.- Parameters:
ec
- ExecutionContextps
- PreparedStatementexprIndex
- the position of the value in the statementvalue
- the valueownerSM
- the owner StateManager where this is embeddedownerFieldNumber
- the owner absolute field number where this is embedded
-
setObject
public void setObject(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] exprIndex, Object value) Sets avalue
intodatastoreStatement
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextps
- PreparedStatementexprIndex
- the position of the value in the statementvalue
- the value
-
getObject
public Object getObject(org.datanucleus.ExecutionContext ec, ResultSet rs, int[] exprIndex, org.datanucleus.state.DNStateManager ownerSM, int ownerFieldNumber) Obtains a value from the result set at position specified byexprIndex
based on being embedded/serialised into the provided owner.- Parameters:
ec
- ExecutionContextrs
- an object returned from the datastore with valuesexprIndex
- the position of the value in the result setownerSM
- the owner StateManager where this is embeddedownerFieldNumber
- the owner field number where this is embedded- Returns:
- the value
-
getObject
Obtains a value fromdatastoreResults
at position specified byexprIndex
.- Parameters:
ec
- ExecutionContextrs
- ResultSetexprIndex
- the position of the value in the result- Returns:
- the value
-
getColumnMetaDataForMember
protected static org.datanucleus.metadata.ColumnMetaData[] getColumnMetaDataForMember(org.datanucleus.metadata.AbstractMemberMetaData mmd, org.datanucleus.metadata.FieldRole 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.- Parameters:
mmd
- Metadata for the memberrole
- The role this mapping plays for the specified member- Returns:
- The column metadata (if any)
-
requiresSetPostProcessing
public boolean requiresSetPostProcessing()Accessor for whether any of the column mappings requires INSERT/UPDATE post processing.- Returns:
- True if a column needs an INSERT/UPDATE post processing call
-
performSetPostProcessing
public void performSetPostProcessing(org.datanucleus.state.DNStateManager sm) Perform any INSERT/UPDATE post processing as required by constituent ColumnMappings. Default implementation does nothing.- Parameters:
sm
- StateManager of the object with this mapping
-