Class ReferenceMapping

All Implemented Interfaces:
MappingCallbacks
Direct Known Subclasses:
InterfaceMapping, ObjectMapping, ReferenceIdMapping

public abstract class ReferenceMapping extends MultiPersistableMapping implements MappingCallbacks
Mapping for a "reference" type. This can be used for things like interfaces, or Object which are simply a reference to some specific (persistable) class. This can be persisted in several ways (see "mappingStrategy") :-
  • List of possible "implementations" of the reference type where column(s) are created for each possible implementation of the reference as a FK to the implementation table. This has the advantage that it retains referential integrity since direct FKs are used.
  • What Kodo/Xcalia used was a single column storing the identity toString() form.
  • Field Details

    • PER_IMPLEMENTATION_MAPPING

      public static final int PER_IMPLEMENTATION_MAPPING
      Each implementation has its own column(s) as a FK to the related table.
      See Also:
    • ID_MAPPING

      public static final int ID_MAPPING
      Single column containing the "identity" of an object.
      See Also:
    • XCALIA_MAPPING

      public static final int XCALIA_MAPPING
      Single column containing the Xcalia form of the "identity" of an object.
      See Also:
    • mappingStrategy

      protected int mappingStrategy
      Strategy for how the reference(s) are mapped.
  • Constructor Details

    • ReferenceMapping

      public ReferenceMapping()
  • Method Details

    • initialize

      public void initialize(org.datanucleus.metadata.AbstractMemberMetaData mmd, Table table, org.datanucleus.ClassLoaderResolver clr)
      Initialize this JavaTypeMapping for the specified field/property.
      Overrides:
      initialize in class JavaTypeMapping
      Parameters:
      mmd - AbstractMemberMetaData for the field to be mapped (if any)
      table - The datastore container storing this mapping (if any)
      clr - the ClassLoaderResolver
    • getMappingStrategy

      public int getMappingStrategy()
      Accessor for the mapping strategy. There are various supported strategies for reference fields with the default being one mapping per implementation, but also allowing a single (String) mapping for all implementations.
      Returns:
      The mapping strategy
    • getJavaTypeMappingForType

      public JavaTypeMapping getJavaTypeMappingForType(Class<?> type)
      Method to return the submapping for the specified (implementation) type. If PER_IMPLEMENTATION_MAPPING is specified then tries to find the appropriate submapping for this implementation type. Otherwise just returns this mapping.
      Parameters:
      type - The implementation type we want the submapping for
      Returns:
      The javaTypeMapping for this implementation
    • prepareColumnMapping

      protected void prepareColumnMapping(org.datanucleus.ClassLoaderResolver clr)
      Convenience method to create the necessary columns to represent this reference in the datastore. With "per-implementation" mapping strategy will create columns for each of the possible implementations. With "identity"/"xcalia" will create a single column to store a reference to the implementation value.
      Parameters:
      clr - The ClassLoaderResolver
    • getReferenceFieldType

      private String getReferenceFieldType(org.datanucleus.metadata.FieldRole fieldRole)
      Convenience method to extract the type of the reference field.
      Parameters:
      fieldRole - Role of this field
      Returns:
      The field type name
    • createPerImplementationColumnsForReferenceField

      void createPerImplementationColumnsForReferenceField(boolean pk, boolean nullable, boolean serialised, boolean embedded, org.datanucleus.metadata.FieldRole fieldRole, org.datanucleus.metadata.ColumnMetaData[] columnMetaData, org.datanucleus.ClassLoaderResolver clr)
      Create columns for reference (Interface/Object) fields on a per-implementation basis. This call ColumnCreator.createColumnsForField for each implementation class of the reference.
    • getJavaTypeForColumnMapping

      public String getJavaTypeForColumnMapping(int index)
      Accessor for the java type represented by a particular datastore mapping. This implementation relays to the superclass implementation except in the case of "identity" mapping strategy, in which case it returns "java.lang.String".
      Overrides:
      getJavaTypeForColumnMapping in class JavaTypeMapping
      Parameters:
      index - requested column index.
      Returns:
      the name of java-type for the requested column.
    • getMappingNumberForValue

      public int getMappingNumberForValue(org.datanucleus.ExecutionContext ec, Object value)
      Convenience accessor for the number of the java type mapping where the passed value would be stored. If no suitable mapping is found will return -1. If is a persistent interface then will return -2 meaning persist against *any* mapping
      Overrides:
      getMappingNumberForValue in class MultiPersistableMapping
      Parameters:
      ec - ExecutionContext
      value - The value
      Returns:
      The index of javaTypeMappings to use (if any), or -1 (none), or -2 (any)
    • setObject

      public void setObject(org.datanucleus.ExecutionContext ec, PreparedStatement ps, int[] pos, Object value, org.datanucleus.state.DNStateManager ownerSM, int ownerFieldNumber)
      Sets the specified positions in the PreparedStatement associated with this field, and value.
      Overrides:
      setObject in class MultiPersistableMapping
      Parameters:
      ec - the ExecutionContext
      ps - a datastore object that executes statements in the database
      pos - The position(s) of the PreparedStatement to populate
      value - the value stored in this field
      ownerSM - the owner StateManager
      ownerFieldNumber - the owner absolute field number
    • getObject

      public Object getObject(org.datanucleus.ExecutionContext ec, ResultSet rs, int[] pos)
      Method to retrieve an object of this type from the ResultSet.
      Overrides:
      getObject in class MultiPersistableMapping
      Parameters:
      ec - ExecutionContext
      rs - The ResultSet
      pos - The parameter positions
      Returns:
      The object
    • getJavaType

      public Class getJavaType()
      Description copied from class: JavaTypeMapping
      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.
      The "java type" is the java-type name used in the plugin.xml mapping file
      Specified by:
      getJavaType in class JavaTypeMapping
      Returns:
      The java type
    • getReferenceStringForObject

      protected String getReferenceStringForObject(org.datanucleus.ExecutionContext ec, Object value)
      Method to convert an object to be stored into a "reference string" to store. Reference string is of the form :
      • ID_MAPPING : "{classname}:{id}"
      • XCALIA_MAPPING (datastore-id) : "{definer}:{id-key}" where definer is discriminator/classname
      • XCALIA_MAPPING (app-id) : "{definer}:{id}" where definer is discriminator/classname
      Parameters:
      ec - ExecutionContext
      value - The object
      Returns:
      The reference string
    • getObjectForReferenceString

      protected Object getObjectForReferenceString(org.datanucleus.ExecutionContext ec, String refString)
      Method to convert a "reference string" into the associated object. Reference string is of the form :
      • ID_MAPPING : "{classname}:{id}"
      • XCALIA_MAPPING (datastore-id) : "{definer}:{id-key}" where definer is discriminator/classname
      • XCALIA_MAPPING (app-id) : "{definer}:{id}" where definer is discriminator/classname
      Parameters:
      ec - execution context
      refString - The reference string
      Returns:
      The referenced object
    • preDelete

      public void preDelete(org.datanucleus.state.DNStateManager sm)
      Description copied from interface: MappingCallbacks
      Method called before the delete of objects, so that additional operations can be performed if necessary.
      Specified by:
      preDelete in interface MappingCallbacks
      Parameters:
      sm - StateManager of the owner