Class ClassTable

  • All Implemented Interfaces:
    DatastoreClass, Table, org.datanucleus.store.schema.table.Table

    public class ClassTable
    extends AbstractClassTable
    implements DatastoreClass
    Table representing a Java class (or classes) as a first class object (FCO). Uses the inheritance strategy to control whether this represents multiple classes or just the one class.

    Mappings

    This class adds some additional mappings over what the superclass provides. Here we add
    • externalFkMappings - any mappings for Collections that have no associated field in this class, providing the foreign key column(s)
    • externalOrderMappings - mappings for any ordering column used by Lists for ordering elements of this class
    • externalFkDiscriminatorMappings - mappings for any discriminator column used when sharing external foreign keys to distinguish the element owner field

    Classes

    A table can represent multiple classes. It has a nominal owner which is the class that has an inheritance strategy of "new-table". All classes that utilise this table have their MetaData stored in this object.

    Secondary Tables

    This class represents a "primary" table. That is, the main table where objects of a class are persisted. It can have several "secondary" tables where some of the classes fields are stored at persistence.
    • Field Detail

      • cmd

        private final org.datanucleus.metadata.ClassMetaData cmd
        MetaData for the principal class being stored here. In inheritance situations where multiple classes share the same table, this will be the class which uses "new-table" strategy.
      • managedClassMetaData

        private final java.util.Collection<org.datanucleus.metadata.AbstractClassMetaData> managedClassMetaData
        MetaData for all classes being managed here.
      • callbacksAppliedForManagedClass

        private final java.util.Map<java.lang.String,​java.util.Collection<org.datanucleus.metadata.AbstractMemberMetaData>> callbacksAppliedForManagedClass
        Callbacks that have been applied keyed by the managed class.
      • supertable

        private ClassTable supertable
        Table above this table storing superclass information (if any).
      • secondaryTables

        private java.util.Map<java.lang.String,​SecondaryTable> secondaryTables
        Secondary tables for this table (if any).
      • externalFkMappings

        private java.util.Map<org.datanucleus.metadata.AbstractMemberMetaData,​JavaTypeMapping> externalFkMappings
        Mappings for FK Collections/Lists not managed by this class (1-N unidirectional). Keyed by the metadata of the owner field/property.
      • externalFkDiscriminatorMappings

        private java.util.Map<org.datanucleus.metadata.AbstractMemberMetaData,​JavaTypeMapping> externalFkDiscriminatorMappings
        Mappings for FK Collections/Lists relation discriminators not managed by this class (1-N unidirectional). Keyed by the metadata of the owner field/property.
      • externalOrderMappings

        private java.util.Map<org.datanucleus.metadata.AbstractMemberMetaData,​JavaTypeMapping> externalOrderMappings
        Mappings for FK Lists order columns not managed by this class (1-N unidirectional). Keyed by the metadata of the owner field/property.
      • tableDef

        private org.datanucleus.util.MacroString tableDef
        User defined table schema
      • createStatementDDL

        private java.lang.String createStatementDDL
        DDL statement for creating the table, if using user defined table schema.
      • candidateKeysByMapField

        java.util.Map<org.datanucleus.metadata.AbstractMemberMetaData,​CandidateKey> candidateKeysByMapField
      • unmappedColumns

        java.util.Set<Column> unmappedColumns
        Set of unmapped "Column" objects that have no associated field (and hence ColumnMapping).
      • managingClassCurrent

        protected transient java.lang.String managingClassCurrent
        Name of class currently being processed in manageClass (if any).
      • runCallbacksAfterManageClass

        protected boolean runCallbacksAfterManageClass
        Flag to run the callbacks after the current class is managed fully.
    • Constructor Detail

      • ClassTable

        public ClassTable​(DatastoreIdentifier tableName,
                          RDBMSStoreManager storeMgr,
                          org.datanucleus.metadata.ClassMetaData cmd)
        Constructor.
        Parameters:
        tableName - Table name SQL identifier
        storeMgr - Store Manager to manage this table
        cmd - MetaData for the class.
    • Method Detail

      • preInitialize

        public void preInitialize​(org.datanucleus.ClassLoaderResolver clr)
        Pre-initialize. We require any supertable, and the PK to be ready before we start initialisation.
        Specified by:
        preInitialize in interface Table
        Overrides:
        preInitialize in class AbstractTable
        Parameters:
        clr - the ClassLoaderResolver
      • initialize

        public void initialize​(org.datanucleus.ClassLoaderResolver clr)
        Method to initialise the table. This adds the columns based on the MetaData representation for the class being represented by this table.
        Specified by:
        initialize in interface Table
        Parameters:
        clr - The ClassLoaderResolver
      • postInitialize

        public void postInitialize​(org.datanucleus.ClassLoaderResolver clr)
        Post initilize. For things that must be set after all classes have been initialized before
        Specified by:
        postInitialize in interface Table
        Overrides:
        postInitialize in class AbstractTable
        Parameters:
        clr - the ClassLoaderResolver
      • manageClass

        public void manageClass​(org.datanucleus.metadata.AbstractClassMetaData theCmd,
                                org.datanucleus.ClassLoaderResolver clr)
        Method that adds the specified class to be managed by this table. Will provide mapping of all persistent fields to their underlying columns, map all necessary identity fields, and manage all "unmapped" columns that have no associated field.
        Parameters:
        theCmd - ClassMetaData for the class to be managed
        clr - The ClassLoaderResolver
      • getManagedClasses

        public java.lang.String[] getManagedClasses()
        Accessor for the names of all classes managed by this table.
        Specified by:
        getManagedClasses in interface DatastoreClass
        Returns:
        Names of the classes managed (stored) here
      • manageMembers

        private void manageMembers​(org.datanucleus.metadata.AbstractClassMetaData theCmd,
                                   org.datanucleus.ClassLoaderResolver clr,
                                   org.datanucleus.metadata.AbstractMemberMetaData[] mmds)
        Goes through all specified members for the specified class and adds a mapping for each. Ignores primary-key fields which are added elsewhere.
        Parameters:
        theCmd - ClassMetaData for the class to be managed
        clr - The ClassLoaderResolver
        mmds - the fields/properties to manage
      • manageUnmappedColumns

        private void manageUnmappedColumns​(org.datanucleus.metadata.AbstractClassMetaData theCmd,
                                           org.datanucleus.ClassLoaderResolver clr)
        Adds on management of the columns in the defined MetaData that are "unmapped" (have no field associated).
        Parameters:
        theCmd - ClassMetaData for the class to be managed
        clr - The ClassLoaderResolver
      • managesClass

        public boolean managesClass​(java.lang.String className)
        Accessor for whether this table manages the specified class.
        Specified by:
        managesClass in interface DatastoreClass
        Parameters:
        className - Name of the class
        Returns:
        Whether it is managed by this table
      • initializePK

        protected void initializePK​(org.datanucleus.ClassLoaderResolver clr)
        Method to initialise the table primary key field(s).
        Specified by:
        initializePK in class AbstractClassTable
        Parameters:
        clr - The ClassLoaderResolver
      • getClassWithPrimaryKeyForClass

        private org.datanucleus.metadata.AbstractClassMetaData getClassWithPrimaryKeyForClass​(org.datanucleus.metadata.AbstractClassMetaData cmd,
                                                                                              org.datanucleus.ClassLoaderResolver clr)
        Utility to navigate the inheritance hierarchy to find the base class that defines the primary keys for this tree. This has the assumption that there is no supertable, and will go up until it finds the superclass which has PK fields but no classes above.
        Parameters:
        cmd - AbstractClassMetaData for this class
        clr - The ClassLoaderResolver
        Returns:
        The AbstractClassMetaData for the class defining the primary keys
      • initializeForClass

        private void initializeForClass​(org.datanucleus.metadata.AbstractClassMetaData theCmd,
                                        org.datanucleus.ClassLoaderResolver clr)
        Method to initialise this table to include all fields in the specified class. This is used to recurse up the hierarchy so that we include all immediate superclasses that have "subclass-table" specified as their inheritance strategy. If we encounter the parent of this class with other than "subclass-table" we stop the process.
        Parameters:
        theCmd - The ClassMetaData for the class
      • runCallBacks

        private void runCallBacks​(org.datanucleus.ClassLoaderResolver clr)
        Execute the callbacks for the classes that this table maps to.
        Parameters:
        clr - ClassLoader resolver
      • addOrderMapping

        private JavaTypeMapping addOrderMapping​(org.datanucleus.metadata.AbstractMemberMetaData mmd,
                                                JavaTypeMapping orderMapping,
                                                org.datanucleus.ClassLoaderResolver clr)
        Convenience method to add an order mapping to the table. Used with 1-N FK "indexed List"/array relations.
        Parameters:
        mmd - Owner field MetaData
        orderMapping - The order mapping (maybe already set and just needs adding)
        clr - ClassLoader resolver
        Returns:
        The order mapping (if updated)
      • getType

        public java.lang.String getType()
        Accessor for the main class represented.
        Specified by:
        getType in interface DatastoreClass
        Returns:
        The name of the class
      • getVersionMetaData

        public final org.datanucleus.metadata.VersionMetaData getVersionMetaData()
        Accessor for versionMetaData
        Specified by:
        getVersionMetaData in interface Table
        Overrides:
        getVersionMetaData in class AbstractTable
        Returns:
        Returns the versionMetaData.
      • getTableWithDiscriminator

        public final ClassTable getTableWithDiscriminator()
        Convenience method to return the root table with a discriminator in this inheritance tree.
        Returns:
        The root table which has the discriminator in this inheritance tree
      • isBaseDatastoreClass

        public boolean isBaseDatastoreClass()
        Whether this table is the base table in the inheritance hierarchy.
        Specified by:
        isBaseDatastoreClass in interface DatastoreClass
        Returns:
        true if this table is a root table
      • getBaseDatastoreClass

        public DatastoreClass getBaseDatastoreClass()
        Description copied from interface: DatastoreClass
        Accessor for the base datastore class. Returns this object if it has no superclass table, otherwise goes up to the superclass etc.
        Specified by:
        getBaseDatastoreClass in interface DatastoreClass
        Returns:
        The base datastore class
      • isSuperDatastoreClass

        public boolean isSuperDatastoreClass​(DatastoreClass table)
        Accessor whether the supplied DatastoreClass is a supertable of this table.
        Specified by:
        isSuperDatastoreClass in interface DatastoreClass
        Parameters:
        table - The DatastoreClass to check
        Returns:
        Whether it is a supertable (somewhere up the inheritance tree)
      • getSurrogateMapping

        public JavaTypeMapping getSurrogateMapping​(org.datanucleus.store.schema.table.SurrogateColumnType colType,
                                                   boolean allowSuperclasses)
        Description copied from interface: Table
        Accessor for the mapping for the specified surrogate type.
        Specified by:
        getSurrogateMapping in interface Table
        Overrides:
        getSurrogateMapping in class AbstractClassTable
        Parameters:
        colType - Column type for the surrogate
        allowSuperclasses - Whether to allow searching superclasses when not specified in this table.
        Returns:
        The mapping
      • getSupertable

        private ClassTable getSupertable​(org.datanucleus.metadata.AbstractClassMetaData theCmd,
                                         org.datanucleus.ClassLoaderResolver clr)
        Utility to find the table above this one. Will recurse to cater for inheritance strategies where fields are handed up to the super class, or down to this class.
        Parameters:
        theCmd - ClassMetaData of the class to find the supertable for.
        Returns:
        The table above this one in any inheritance hierarchy
      • getBaseDatastoreClassWithMember

        public DatastoreClass getBaseDatastoreClassWithMember​(org.datanucleus.metadata.AbstractMemberMetaData mmd)
        Convenience accessor for the base table for this table which has the specified member.
        Specified by:
        getBaseDatastoreClassWithMember in interface DatastoreClass
        Parameters:
        mmd - Member MetaData for this field
        Returns:
        The base table which has the field specified
      • getClassMetaData

        public org.datanucleus.metadata.ClassMetaData getClassMetaData()
        Accessor for the (primary) class MetaData. Package-level access to restrict to other table types only.
        Specified by:
        getClassMetaData in interface org.datanucleus.store.schema.table.Table
        Overrides:
        getClassMetaData in class AbstractTable
        Returns:
        The (primary) class MetaData
      • getExpectedIndices

        protected java.util.Set<Index> getExpectedIndices​(org.datanucleus.ClassLoaderResolver clr)
        Accessor for the indices for this table. This includes both the user-defined indices (via MetaData), and the ones required by foreign keys (required by relationships).
        Overrides:
        getExpectedIndices in class TableImpl
        Parameters:
        clr - The ClassLoaderResolver
        Returns:
        The indices
      • getIndexForIndexMetaDataAndMapping

        private Index getIndexForIndexMetaDataAndMapping​(org.datanucleus.metadata.IndexMetaData imd,
                                                         JavaTypeMapping mapping)
        Convenience method to convert an IndexMetaData and a mapping into an Index.
        Parameters:
        imd - The Index MetaData
        mapping - The mapping
        Returns:
        The Index
      • getIndexForIndexMetaData

        private Index getIndexForIndexMetaData​(org.datanucleus.metadata.IndexMetaData imd)
        Convenience method to convert an IndexMetaData into an Index.
        Parameters:
        imd - The Index MetaData
        Returns:
        The Index
      • getExpectedForeignKeys

        public java.util.List<ForeignKey> getExpectedForeignKeys​(org.datanucleus.ClassLoaderResolver clr)
        Accessor for the expected foreign keys for this table.
        Overrides:
        getExpectedForeignKeys in class TableImpl
        Parameters:
        clr - The ClassLoaderResolver
        Returns:
        The expected foreign keys.
      • addExpectedForeignKeysForEmbeddedPCField

        private void addExpectedForeignKeysForEmbeddedPCField​(java.util.List<ForeignKey> foreignKeys,
                                                              boolean autoMode,
                                                              org.datanucleus.ClassLoaderResolver clr,
                                                              EmbeddedPCMapping embeddedMapping)
        Convenience method to add the expected FKs for an embedded PC field.
        Parameters:
        foreignKeys - The list of FKs to add the FKs to
        autoMode - Whether operating in "auto-mode" where DataNucleus can create its own FKs
        clr - ClassLoader resolver
        embeddedMapping - The embedded PC mapping
      • getForeignKeyForForeignKeyMetaData

        private ForeignKey getForeignKeyForForeignKeyMetaData​(org.datanucleus.metadata.ForeignKeyMetaData fkmd)
        Convenience method to create a FK for the specified ForeignKeyMetaData. Used for foreign-keys specified at <class> level.
        Parameters:
        fkmd - ForeignKey MetaData
        Returns:
        The ForeignKey
      • getExpectedCandidateKeys

        protected java.util.List<CandidateKey> getExpectedCandidateKeys()
        Accessor for the expected candidate keys for this table.
        Overrides:
        getExpectedCandidateKeys in class TableImpl
        Returns:
        The expected candidate keys.
      • getCandidateKeyForUniqueMetaData

        private CandidateKey getCandidateKeyForUniqueMetaData​(org.datanucleus.metadata.UniqueMetaData umd)
        Convenience method to convert a UniqueMetaData into a CandidateKey.
        Parameters:
        umd - The Unique MetaData
        Returns:
        The Candidate Key
      • getPrimaryKey

        public PrimaryKey getPrimaryKey()
        Accessor for the primary key for this table. Overrides the method in TableImpl to add on any specification of PK name in the metadata.
        Overrides:
        getPrimaryKey in class TableImpl
        Returns:
        The primary key.
      • getSQLCreateStatements

        protected java.util.List<java.lang.String> getSQLCreateStatements​(java.util.Properties props)
        Accessor for the CREATE statements for this table. Creates this table followed by all secondary tables (if any).
        Overrides:
        getSQLCreateStatements in class TableImpl
        Parameters:
        props - Properties for creating the table
        Returns:
        the SQL statements to be executed for creation
      • getSQLDropStatements

        protected java.util.List<java.lang.String> getSQLDropStatements()
        Accessor for the DROP statements for this table. Drops all secondary tables (if any) followed by the table itself.
        Overrides:
        getSQLDropStatements in class TableImpl
        Returns:
        List of statements
      • initializeFKMapUniqueConstraints

        private void initializeFKMapUniqueConstraints​(org.datanucleus.metadata.AbstractMemberMetaData ownerMmd)
        Method to initialise unique constraints for 1-N Map using FK.
        Parameters:
        ownerMmd - metadata for the field/property with the map in the owner class
      • initializeIDMapping

        private void initializeIDMapping()
        Initialize the ID Mapping. For datastore identity this will be a PCMapping that contains the DatastoreIdMapping. For application identity this will be a PCMapping that contains the PK mapping(s).
      • getIdMapping

        public JavaTypeMapping getIdMapping()
        Accessor for a mapping for the ID (persistable) for this table.
        Specified by:
        getIdMapping in interface Table
        Returns:
        The (persistable) ID mapping.
      • getExternalOrderMappings

        private java.util.Map<org.datanucleus.metadata.AbstractMemberMetaData,​JavaTypeMapping> getExternalOrderMappings()
        Accessor for all of the order mappings (used by FK Lists, Collections, Arrays)
        Returns:
        The mappings for the order columns.
      • hasExternalFkMappings

        public boolean hasExternalFkMappings()
      • getExternalFkMappings

        private java.util.Map<org.datanucleus.metadata.AbstractMemberMetaData,​JavaTypeMapping> getExternalFkMappings()
        Accessor for all of the external FK mappings.
        Returns:
        The mappings for external FKs
      • getExternalMapping

        public JavaTypeMapping getExternalMapping​(org.datanucleus.metadata.AbstractMemberMetaData mmd,
                                                  MappingType mappingType)
        Accessor for an external mapping for the specified field of the required type.
        Specified by:
        getExternalMapping in interface DatastoreClass
        Parameters:
        mmd - MetaData for the field/property
        mappingType - Type of mapping
        Returns:
        The (external) mapping
      • getMetaDataForExternalMapping

        public org.datanucleus.metadata.AbstractMemberMetaData getMetaDataForExternalMapping​(JavaTypeMapping mapping,
                                                                                             MappingType mappingType)
        Accessor for the MetaData for the (owner) field that an external mapping corresponds to.
        Specified by:
        getMetaDataForExternalMapping in interface DatastoreClass
        Parameters:
        mapping - The mapping
        mappingType - The mapping type
        Returns:
        metadata for the external mapping
      • getExternalFkDiscriminatorMappings

        private java.util.Map<org.datanucleus.metadata.AbstractMemberMetaData,​JavaTypeMapping> getExternalFkDiscriminatorMappings()
        Accessor for all of the external FK discriminator mappings.
        Returns:
        The mappings for external FKs
      • getMemberMapping

        public JavaTypeMapping getMemberMapping​(org.datanucleus.metadata.AbstractMemberMetaData mmd)
        Accessor for the field mapping for the specified field. The field can be managed by a supertable of this table.
        Specified by:
        getMemberMapping in interface DatastoreClass
        Specified by:
        getMemberMapping in interface Table
        Parameters:
        mmd - MetaData for this field/property
        Returns:
        the Mapping for the field/property
      • getMemberMappingInDatastoreClass

        public JavaTypeMapping getMemberMappingInDatastoreClass​(org.datanucleus.metadata.AbstractMemberMetaData mmd)
        Accessor for the mapping for the specified field only in this datastore class.
        Specified by:
        getMemberMappingInDatastoreClass in interface DatastoreClass
        Parameters:
        mmd - Metadata of the field/property
        Returns:
        The Mapping for the field/property (or null if not present here)
      • getMemberMapping

        public JavaTypeMapping getMemberMapping​(java.lang.String memberName)
        Accessor for the java mapping for the named member. The member may exist in a parent table or a secondary table. Throws a NoSuchPersistentFieldException if the member name is not found. TODO Use of this is discouraged since the memberName is not fully qualified and if a superclass-table inheritance is used we could have 2 members of that name here.
        Specified by:
        getMemberMapping in interface DatastoreClass
        Parameters:
        memberName - Name of member
        Returns:
        The mapping
        Throws:
        NoSuchPersistentFieldException - Thrown when the field/property is not found
      • getMetaDataForMember

        org.datanucleus.metadata.AbstractMemberMetaData getMetaDataForMember​(java.lang.String memberName)
        Acessor for the MetaData for the member with the specified name. Searches the MetaData of all classes managed by this table. Doesn't allow for cases where the table manages subclasses with the same member name, in that case you should use the equivalent method passing metaData. TODO Support subclasses with members of the same name TODO Use of this is discouraged since the memberName is not fully qualified and if a superclass-table inheritance is used we could have 2 members of that name here.
        Parameters:
        memberName - the member name
        Returns:
        metadata for the member
      • addOrderColumn

        private JavaTypeMapping addOrderColumn​(org.datanucleus.metadata.AbstractMemberMetaData mmd,
                                               org.datanucleus.ClassLoaderResolver clr)
        Adds an ordering column to the element table (this) in FK list relationships. Used to store the position of the element in the List. If the <order> provides a mapped-by, this will return the existing column mapping.
        Parameters:
        mmd - The MetaData of the field/property with the list for the column to map to
        Returns:
        The Mapping for the order column
      • provideExternalMappings

        public final void provideExternalMappings​(MappingConsumer consumer,
                                                  MappingType mappingType)
        Provide the mappings to the consumer for all external fields mapped to this table of the specified type
        Specified by:
        provideExternalMappings in interface DatastoreClass
        Parameters:
        consumer - Consumer for the mappings
        mappingType - Type of external mapping
      • provideMappingsForMembers

        public void provideMappingsForMembers​(MappingConsumer consumer,
                                              org.datanucleus.metadata.AbstractMemberMetaData[] fieldMetaData,
                                              boolean includeSecondaryTables)
        Provide the mappings to the consumer for all absolute field Numbers in this table that are container in the fieldNumbers parameter.
        Specified by:
        provideMappingsForMembers in interface DatastoreClass
        Overrides:
        provideMappingsForMembers in class AbstractClassTable
        Parameters:
        consumer - Consumer for the mappings
        fieldMetaData - MetaData for the fields to provide mappings for
        includeSecondaryTables - Whether to provide fields in secondary tables
      • provideUnmappedColumns

        public void provideUnmappedColumns​(MappingConsumer consumer)
        Method to provide all unmapped columns to the consumer.
        Specified by:
        provideUnmappedColumns in interface DatastoreClass
        Parameters:
        consumer - Consumer of information
      • validateConstraints

        public boolean validateConstraints​(java.sql.Connection conn,
                                           boolean autoCreate,
                                           java.util.Collection<java.lang.Throwable> autoCreateErrors,
                                           org.datanucleus.ClassLoaderResolver clr)
                                    throws java.sql.SQLException
        Method to validate the constraints of this table.
        Overrides:
        validateConstraints in class TableImpl
        Parameters:
        conn - Connection to use in validation
        autoCreate - Whether to auto create the constraints
        autoCreateErrors - Whether to log a warning only on errors during "auto create"
        clr - The ClassLoaderResolver
        Returns:
        Whether the DB was modified
        Throws:
        java.sql.SQLException - Thrown when an error occurs in validation