Search in sources :

Example 21 with PersistableMapping

use of org.datanucleus.store.rdbms.mapping.java.PersistableMapping in project datanucleus-rdbms by datanucleus.

the class ClassTable method addExpectedForeignKeysForEmbeddedPCField.

/**
 * Convenience method to add the expected FKs for an embedded PC field.
 * @param foreignKeys The list of FKs to add the FKs to
 * @param autoMode Whether operating in "auto-mode" where JPOX can create its own FKs
 * @param clr ClassLoader resolver
 * @param embeddedMapping The embedded PC mapping
 */
private void addExpectedForeignKeysForEmbeddedPCField(List foreignKeys, boolean autoMode, ClassLoaderResolver clr, EmbeddedPCMapping embeddedMapping) {
    for (int i = 0; i < embeddedMapping.getNumberOfJavaTypeMappings(); i++) {
        JavaTypeMapping embFieldMapping = embeddedMapping.getJavaTypeMapping(i);
        if (embFieldMapping instanceof EmbeddedPCMapping) {
            // Nested embedded PC so add the FKs for that
            addExpectedForeignKeysForEmbeddedPCField(foreignKeys, autoMode, clr, (EmbeddedPCMapping) embFieldMapping);
        } else {
            AbstractMemberMetaData embFmd = embFieldMapping.getMemberMetaData();
            if (ClassUtils.isReferenceType(embFmd.getType()) && embFieldMapping instanceof ReferenceMapping) {
                // Field is a reference type, so add a FK to the table of the PC for each PC implementation
                Collection fks = TableUtils.getForeignKeysForReferenceField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
                foreignKeys.addAll(fks);
            } else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(embFmd.getType(), clr) != null && embFieldMapping.getNumberOfDatastoreMappings() > 0 && embFieldMapping instanceof PersistableMapping) {
                // Field is for a PC class with the FK at this side, so add a FK to the table of this PC
                ForeignKey fk = TableUtils.getForeignKeyForPCField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
                if (fk != null) {
                    foreignKeys.add(fk);
                }
            }
        }
    }
}
Also used : PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) EmbeddedPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedPCMapping) Collection(java.util.Collection) ForeignKey(org.datanucleus.store.rdbms.key.ForeignKey) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 22 with PersistableMapping

use of org.datanucleus.store.rdbms.mapping.java.PersistableMapping in project datanucleus-rdbms by datanucleus.

the class CollectionTable method initialize.

/**
 * Method to initialise the table definition.
 * @param clr The ClassLoaderResolver
 */
public void initialize(ClassLoaderResolver clr) {
    super.initialize(clr);
    // Add column(s) for element
    boolean elementPC = (mmd.hasCollection() && mmd.getCollection().elementIsPersistent());
    Class elementClass = clr.classForName(getElementType());
    if (isSerialisedElement() || isEmbeddedElementPC() || (isEmbeddedElement() && !elementPC) || ClassUtils.isReferenceType(elementClass)) {
        // Element = PC(embedded), PC(serialised), Non-PC(serialised), Non-PC(embedded), Reference
        // Join table has : ownerMapping (PK), elementMapping, orderMapping (PK)
        elementMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_COLLECTION_ELEMENT);
        if (NucleusLogger.DATASTORE.isDebugEnabled()) {
            logMapping(mmd.getFullFieldName() + ".[ELEMENT]", elementMapping);
        }
    } else {
        // Element = PC
        // Join table has : ownerMapping (PK), elementMapping, orderMapping (optional)
        ColumnMetaData[] elemColmd = null;
        AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
        ElementMetaData elemmd = mmd.getElementMetaData();
        if (elemmd != null && elemmd.getColumnMetaData() != null && elemmd.getColumnMetaData().length > 0) {
            // Column mappings defined at this side (1-N, M-N)
            elemColmd = elemmd.getColumnMetaData();
        } else if (relatedMmds != null && relatedMmds[0].getJoinMetaData() != null && relatedMmds[0].getJoinMetaData().getColumnMetaData() != null && relatedMmds[0].getJoinMetaData().getColumnMetaData().length > 0) {
            // Column mappings defined at other side (M-N) on <join>
            elemColmd = relatedMmds[0].getJoinMetaData().getColumnMetaData();
        }
        elementMapping = ColumnCreator.createColumnsForJoinTables(elementClass, mmd, elemColmd, storeMgr, this, false, false, FieldRole.ROLE_COLLECTION_ELEMENT, clr, null);
        RelationType relationType = mmd.getRelationType(clr);
        if (Boolean.TRUE.equals(mmd.getContainer().allowNulls()) && relationType != RelationType.MANY_TO_MANY_BI) {
            // 1-N : Make all element col(s) nullable so we can store null elements
            for (int i = 0; i < elementMapping.getNumberOfDatastoreMappings(); i++) {
                Column elementCol = elementMapping.getDatastoreMapping(i).getColumn();
                elementCol.setNullable(true);
            }
        }
        if (NucleusLogger.DATASTORE.isDebugEnabled()) {
            logMapping(mmd.getFullFieldName() + ".[ELEMENT]", elementMapping);
        }
    }
    PrimaryKeyMetaData pkmd = (mmd.getJoinMetaData() != null ? mmd.getJoinMetaData().getPrimaryKeyMetaData() : null);
    boolean pkColsSpecified = (pkmd != null ? pkmd.getColumnMetaData() != null : false);
    boolean pkRequired = requiresPrimaryKey();
    // Add order mapping if required
    boolean orderRequired = false;
    if (mmd.getOrderMetaData() != null) {
        if (mmd.getOrderMetaData().isIndexedList()) {
            // Indexed Collection with <order>, so add index mapping
            orderRequired = true;
            RelationType relType = mmd.getRelationType(clr);
            if (relType == RelationType.MANY_TO_MANY_BI) {
                // Don't support M-N using indexed List
                throw new NucleusUserException(Localiser.msg("020002", mmd.getFullFieldName())).setFatal();
            }
        }
    } else if (List.class.isAssignableFrom(mmd.getType())) {
        // Indexed List with no <order>, so has index mapping
        orderRequired = true;
    } else if (pkRequired && !pkColsSpecified) {
        // PK is required so maybe need to add an index to form the PK
        if (isEmbeddedElementPC()) {
            if (mmd.getCollection().getElementClassMetaData(clr).getIdentityType() != IdentityType.APPLICATION) {
                // Embedded PC with datastore id so we need an index to form the PK
                orderRequired = true;
            }
        } else if (isSerialisedElement()) {
            // Serialised element, so need an index to form the PK
            orderRequired = true;
        } else if (elementMapping instanceof ReferenceMapping) {
            // ReferenceMapping, so have order if more than 1 implementation
            ReferenceMapping refMapping = (ReferenceMapping) elementMapping;
            if (refMapping.getJavaTypeMapping().length > 1) {
                orderRequired = true;
            }
        } else if (!(elementMapping instanceof PersistableMapping)) {
            // Non-PC, so depends if the element column can be used as part of a PK
            // TODO This assumes the elementMapping has a single column but what if it is Color with 4 cols?
            Column elementCol = elementMapping.getDatastoreMapping(0).getColumn();
            if (!storeMgr.getDatastoreAdapter().isValidPrimaryKeyType(elementCol.getJdbcType())) {
                // Not possible to use this Non-PC type as part of the PK
                orderRequired = true;
            }
        }
    }
    if (orderRequired) {
        // Order (index) column is required (integer based)
        ColumnMetaData orderColmd = null;
        if (mmd.getOrderMetaData() != null && mmd.getOrderMetaData().getColumnMetaData() != null && mmd.getOrderMetaData().getColumnMetaData().length > 0) {
            // Specified "order" column info
            orderColmd = mmd.getOrderMetaData().getColumnMetaData()[0];
            if (orderColmd.getName() == null) {
                // No column name so use default
                orderColmd = new ColumnMetaData(orderColmd);
                DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
                orderColmd.setName(id.getName());
            }
        } else {
            // No column name so use default
            DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
            orderColmd = new ColumnMetaData();
            orderColmd.setName(id.getName());
        }
        // JDO2 spec [18.5] order column is assumed to be "int"
        orderMapping = storeMgr.getMappingManager().getMapping(int.class);
        ColumnCreator.createIndexColumn(orderMapping, storeMgr, clr, this, orderColmd, pkRequired && !pkColsSpecified);
        if (NucleusLogger.DATASTORE.isDebugEnabled()) {
            logMapping(mmd.getFullFieldName() + ".[ORDER]", orderMapping);
        }
    }
    // Define primary key of the join table (if any)
    if (pkRequired) {
        if (pkColsSpecified) {
            // Apply the users PK specification
            applyUserPrimaryKeySpecification(pkmd);
        } else {
            // Define PK
            for (int i = 0; i < ownerMapping.getNumberOfDatastoreMappings(); i++) {
                ownerMapping.getDatastoreMapping(i).getColumn().setPrimaryKey();
            }
            if (orderRequired) {
                // Order column specified so owner+order are the PK
                orderMapping.getDatastoreMapping(0).getColumn().setPrimaryKey();
            } else {
                // No order column specified so owner+element are the PK
                for (int i = 0; i < elementMapping.getNumberOfDatastoreMappings(); i++) {
                    elementMapping.getDatastoreMapping(i).getColumn().setPrimaryKey();
                }
            }
        }
    }
    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
        NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057023", this));
    }
    storeMgr.registerTableInitialized(this);
    state = TABLE_STATE_INITIALIZED;
}
Also used : ElementMetaData(org.datanucleus.metadata.ElementMetaData) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) PrimaryKeyMetaData(org.datanucleus.metadata.PrimaryKeyMetaData) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) RelationType(org.datanucleus.metadata.RelationType) List(java.util.List) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 23 with PersistableMapping

use of org.datanucleus.store.rdbms.mapping.java.PersistableMapping in project datanucleus-rdbms by datanucleus.

the class ColumnCreator method createColumnsForField.

/**
 * Method to create the column(s) for a field in either a join table or for a reference field.
 * @param javaType The java type of the field being stored
 * @param mapping The JavaTypeMapping (if existing, otherwise created and returned by this method)
 * @param table The table to insert the columns into (join table, or primary table (if ref field))
 * @param storeMgr Manager for the store
 * @param mmd MetaData for the field (or null if a collection field)
 * @param isPrimaryKey Whether to create the columns as part of the PK
 * @param isNullable Whether the columns should be nullable
 * @param serialised Whether the field is serialised
 * @param embedded Whether the field is embedded
 * @param fieldRole The role of the field (when part of a join table)
 * @param columnMetaData MetaData for the column(s)
 * @param clr ClassLoader resolver
 * @param isReferenceField Whether this field is part of a reference field
 * @param ownerTable Table of the owner of this member (optional, for when the member is embedded)
 * @return The JavaTypeMapping for the table
 */
public static JavaTypeMapping createColumnsForField(Class javaType, JavaTypeMapping mapping, Table table, RDBMSStoreManager storeMgr, AbstractMemberMetaData mmd, boolean isPrimaryKey, boolean isNullable, boolean serialised, boolean embedded, FieldRole fieldRole, ColumnMetaData[] columnMetaData, ClassLoaderResolver clr, boolean isReferenceField, Table ownerTable) {
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    if (mapping instanceof ReferenceMapping || mapping instanceof PersistableMapping) {
        // PC/interface/Object mapping
        JavaTypeMapping container = mapping;
        if (mapping instanceof ReferenceMapping) {
            // Interface/Object has child mappings for each implementation
            container = storeMgr.getMappingManager().getMapping(javaType, serialised, embedded, mmd != null ? mmd.getFullFieldName() : null);
            ((ReferenceMapping) mapping).addJavaTypeMapping(container);
        }
        // Get the table that we want our column to be a FK to. This could be the owner table, element table, key table, value table etc
        DatastoreClass destinationTable = null;
        try {
            destinationTable = storeMgr.getDatastoreClass(javaType.getName(), clr);
        } catch (NoTableManagedException ntme) {
            if (ownerTable != null && ownerTable instanceof DatastoreClass) {
                destinationTable = (DatastoreClass) ownerTable;
            } else {
                throw ntme;
            }
        }
        if (destinationTable == null) {
            // Maybe the owner hasn't got its own table (e.g "subclass-table" or "complete-table"+abstract)
            // Alternate is when we have an embedded type which itself has an embedded collection - not catered for at all currently
            AbstractClassMetaData ownerCmd = storeMgr.getMetaDataManager().getMetaDataForClass(javaType, clr);
            if (ownerCmd.getBaseAbstractClassMetaData().getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE) {
                // COMPLETE-TABLE but abstract root, so find one of the subclasses with a table and use that for now
                Collection<String> ownerSubclassNames = storeMgr.getSubClassesForClass(javaType.getName(), true, clr);
                if (ownerSubclassNames != null && ownerSubclassNames.size() > 0) {
                    for (String ownerSubclassName : ownerSubclassNames) {
                        ownerCmd = storeMgr.getMetaDataManager().getMetaDataForClass(ownerSubclassName, clr);
                        try {
                            destinationTable = storeMgr.getDatastoreClass(ownerSubclassName, clr);
                        } catch (NoTableManagedException ntme) {
                        }
                        if (destinationTable != null) {
                            break;
                        }
                    }
                }
            } else {
                AbstractClassMetaData[] ownerCmds = storeMgr.getClassesManagingTableForClass(ownerCmd, clr);
                if (ownerCmds == null || ownerCmds.length == 0) {
                    throw new NucleusUserException(Localiser.msg("057023", javaType.getName())).setFatal();
                }
                // Use the first one since they should all have the same id column(s)
                destinationTable = storeMgr.getDatastoreClass(ownerCmds[0].getFullClassName(), clr);
            }
        }
        if (destinationTable != null) {
            // Foreign-Key to the destination table ID mapping
            JavaTypeMapping m = destinationTable.getIdMapping();
            // For each column in the destination mapping, add a column here
            ColumnMetaDataContainer columnContainer = null;
            if (columnMetaData != null && columnMetaData.length > 0) {
                columnContainer = (ColumnMetaDataContainer) columnMetaData[0].getParent();
            }
            CorrespondentColumnsMapper correspondentColumnsMapping = new CorrespondentColumnsMapper(columnContainer, table, columnMetaData, m, true);
            for (int i = 0; i < m.getNumberOfDatastoreMappings(); i++) {
                JavaTypeMapping refDatastoreMapping = storeMgr.getMappingManager().getMapping(m.getDatastoreMapping(i).getJavaTypeMapping().getJavaType());
                ColumnMetaData colmd = correspondentColumnsMapping.getColumnMetaDataByIdentifier(m.getDatastoreMapping(i).getColumn().getIdentifier());
                try {
                    DatastoreIdentifier identifier = null;
                    if (fieldRole == FieldRole.ROLE_MAP_KEY && columnContainer == null) {
                        // Map KEY field and no metadata defined
                        if (isReferenceField) {
                            // Create reference identifier
                            identifier = idFactory.newReferenceFieldIdentifier(mmd, storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(javaType, clr), m.getDatastoreMapping(i).getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
                        } else {
                            // Create join table identifier
                            AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
                            // TODO If the mmd is an "embedded" type this can create invalid identifiers
                            // TODO Cater for more than 1 related field
                            identifier = idFactory.newJoinTableFieldIdentifier(mmd, relatedMmds != null ? relatedMmds[0] : null, m.getDatastoreMapping(i).getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
                        }
                    } else {
                        if (colmd.getName() == null) {
                            // User hasn't provided a name, so we use default naming
                            if (isReferenceField) {
                                // Create reference identifier
                                identifier = idFactory.newReferenceFieldIdentifier(mmd, storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(javaType, clr), m.getDatastoreMapping(i).getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
                            } else {
                                // Create join table identifier
                                AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
                                // TODO If the mmd is an "embedded" type this can create invalid identifiers
                                // TODO Cater for more than 1 related field
                                identifier = idFactory.newJoinTableFieldIdentifier(mmd, relatedMmds != null ? relatedMmds[0] : null, m.getDatastoreMapping(i).getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
                            }
                        } else {
                            // User defined name, so we use that.
                            identifier = idFactory.newColumnIdentifier(colmd.getName());
                        }
                    }
                    // Only add the column if not currently present
                    Column column = table.addColumn(javaType.getName(), identifier, refDatastoreMapping, colmd);
                    m.getDatastoreMapping(i).getColumn().copyConfigurationTo(column);
                    if (isPrimaryKey) {
                        column.setPrimaryKey();
                    }
                    if (isNullable) {
                        column.setNullable(true);
                    }
                    storeMgr.getMappingManager().createDatastoreMapping(refDatastoreMapping, column, m.getDatastoreMapping(i).getJavaTypeMapping().getJavaTypeForDatastoreMapping(i));
                } catch (DuplicateColumnException ex) {
                    throw new NucleusUserException("Cannot create column for field " + mmd.getFullFieldName() + " column metadata " + colmd, ex);
                }
                try {
                    ((PersistableMapping) container).addJavaTypeMapping(refDatastoreMapping);
                } catch (ClassCastException e) {
                    throw new NucleusUserException("Failed to create column for field " + mmd.getFullFieldName() + ". Cannot cast mapping to PersistableMapping.", e);
                }
            }
        }
    } else {
        // Non-PC mapping
        // Add column for the field
        Column column = null;
        ColumnMetaData colmd = null;
        if (columnMetaData != null && columnMetaData.length > 0) {
            colmd = columnMetaData[0];
        }
        DatastoreIdentifier identifier = null;
        if (colmd != null && colmd.getName() != null) {
            // User specified name
            identifier = idFactory.newColumnIdentifier(colmd.getName());
        } else {
            // No user-supplied name so generate one
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
        }
        column = table.addColumn(javaType.getName(), identifier, mapping, colmd);
        storeMgr.getMappingManager().createDatastoreMapping(mapping, column, mapping.getJavaTypeForDatastoreMapping(0));
        if (isNullable) {
            column.setNullable(true);
        }
    }
    return mapping;
}
Also used : DuplicateColumnException(org.datanucleus.store.rdbms.exceptions.DuplicateColumnException) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) ColumnMetaDataContainer(org.datanucleus.metadata.ColumnMetaDataContainer) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) CorrespondentColumnsMapper(org.datanucleus.store.rdbms.mapping.CorrespondentColumnsMapper)

Example 24 with PersistableMapping

use of org.datanucleus.store.rdbms.mapping.java.PersistableMapping in project datanucleus-rdbms by datanucleus.

the class ElementContainerTable method getExpectedIndices.

/**
 * 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).
 * @param clr The ClassLoaderResolver
 * @return The indices
 */
protected Set getExpectedIndices(ClassLoaderResolver clr) {
    assertIsInitialized();
    Set<Index> indices = new HashSet();
    // Index for FK back to owner
    if (mmd.getIndexMetaData() != null) {
        Index index = TableUtils.getIndexForField(this, mmd.getIndexMetaData(), ownerMapping);
        if (index != null) {
            indices.add(index);
        }
    } else if (mmd.getJoinMetaData() != null && mmd.getJoinMetaData().getIndexMetaData() != null) {
        Index index = TableUtils.getIndexForField(this, mmd.getJoinMetaData().getIndexMetaData(), ownerMapping);
        if (index != null) {
            indices.add(index);
        }
    } else {
        // Fallback to an index for the foreign-key to the owner
        Index index = TableUtils.getIndexForField(this, null, ownerMapping);
        if (index != null) {
            indices.add(index);
        }
    }
    // Index for FK to element (if required)
    if (elementMapping instanceof EmbeddedElementPCMapping) {
        // Add all indices required by fields of the embedded element
        EmbeddedElementPCMapping embMapping = (EmbeddedElementPCMapping) elementMapping;
        for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
            // Add indexes for fields of this embedded PC object
            JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
            IndexMetaData imd = embFieldMapping.getMemberMetaData().getIndexMetaData();
            if (imd != null) {
                Index index = TableUtils.getIndexForField(this, imd, embFieldMapping);
                if (index != null) {
                    indices.add(index);
                }
            }
        }
    } else {
        ElementMetaData elemmd = mmd.getElementMetaData();
        if (elemmd != null && elemmd.getIndexMetaData() != null) {
            Index index = TableUtils.getIndexForField(this, elemmd.getIndexMetaData(), elementMapping);
            if (index != null) {
                indices.add(index);
            }
        } else {
            // Fallback to an index for any foreign-key to the element
            if (elementMapping instanceof PersistableMapping) {
                Index index = TableUtils.getIndexForField(this, null, elementMapping);
                if (index != null) {
                    indices.add(index);
                }
            }
        }
    }
    if (orderMapping != null) {
        // Index for ordering?
        if (mmd.getOrderMetaData() != null && mmd.getOrderMetaData().getIndexMetaData() != null) {
            Index index = TableUtils.getIndexForField(this, mmd.getOrderMetaData().getIndexMetaData(), orderMapping);
            if (index != null) {
                indices.add(index);
            }
        }
    }
    return indices;
}
Also used : PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ElementMetaData(org.datanucleus.metadata.ElementMetaData) Index(org.datanucleus.store.rdbms.key.Index) EmbeddedElementPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping) HashSet(java.util.HashSet) IndexMetaData(org.datanucleus.metadata.IndexMetaData)

Example 25 with PersistableMapping

use of org.datanucleus.store.rdbms.mapping.java.PersistableMapping in project datanucleus-rdbms by datanucleus.

the class MapTable method getExpectedForeignKeys.

/**
 * Accessor for the expected foreign keys for this table.
 * @param clr The ClassLoaderResolver
 * @return The expected foreign keys.
 */
public List getExpectedForeignKeys(ClassLoaderResolver clr) {
    assertIsInitialized();
    boolean autoMode = false;
    if (storeMgr.getStringProperty(RDBMSPropertyNames.PROPERTY_RDBMS_CONSTRAINT_CREATE_MODE).equals("DataNucleus")) {
        autoMode = true;
    }
    ArrayList foreignKeys = new ArrayList();
    try {
        // FK from join table to owner table
        DatastoreClass referencedTable = storeMgr.getDatastoreClass(ownerType, clr);
        if (referencedTable != null) {
            // Take <foreign-key> from <join>
            ForeignKeyMetaData fkmd = null;
            if (mmd.getJoinMetaData() != null) {
                fkmd = mmd.getJoinMetaData().getForeignKeyMetaData();
            }
            if (fkmd != null || autoMode) {
                ForeignKey fk = new ForeignKey(ownerMapping, dba, referencedTable, true);
                fk.setForMetaData(fkmd);
                foreignKeys.add(fk);
            }
        }
        if (!isSerialisedValuePC()) {
            if (isEmbeddedValuePC()) {
                // Add any FKs for the fields of the (embedded) value
                EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping) valueMapping;
                for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
                    JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                    AbstractMemberMetaData embFmd = embFieldMapping.getMemberMetaData();
                    if (ClassUtils.isReferenceType(embFmd.getType()) && embFieldMapping instanceof ReferenceMapping) {
                        // Field is a reference type, so add a FK to the table of the PC for each PC implementation
                        Collection fks = TableUtils.getForeignKeysForReferenceField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
                        foreignKeys.addAll(fks);
                    } else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(embFmd.getType(), clr) != null && embFieldMapping.getNumberOfDatastoreMappings() > 0 && embFieldMapping instanceof PersistableMapping) {
                        // Field is for a PC class with the FK at this side, so add a FK to the table of this PC
                        ForeignKey fk = TableUtils.getForeignKeyForPCField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
                        if (fk != null) {
                            foreignKeys.add(fk);
                        }
                    }
                }
            } else if (mmd.getMap().valueIsPersistent()) {
                // FK from join table to value table
                referencedTable = storeMgr.getDatastoreClass(mmd.getMap().getValueType(), clr);
                if (referencedTable != null) {
                    // Take <foreign-key> from <value>
                    ForeignKeyMetaData fkmd = null;
                    if (mmd.getValueMetaData() != null) {
                        fkmd = mmd.getValueMetaData().getForeignKeyMetaData();
                    }
                    if (fkmd != null || autoMode) {
                        ForeignKey fk = new ForeignKey(valueMapping, dba, referencedTable, true);
                        fk.setForMetaData(fkmd);
                        foreignKeys.add(fk);
                    }
                }
            }
        }
        if (!isSerialisedKeyPC()) {
            if (isEmbeddedKeyPC()) {
                // Add any FKs for the fields of the (embedded) key
                EmbeddedKeyPCMapping embMapping = (EmbeddedKeyPCMapping) keyMapping;
                for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
                    JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                    AbstractMemberMetaData embFmd = embFieldMapping.getMemberMetaData();
                    if (ClassUtils.isReferenceType(embFmd.getType()) && embFieldMapping instanceof ReferenceMapping) {
                        // Field is a reference type, so add a FK to the table of the PC for each PC implementation
                        Collection fks = TableUtils.getForeignKeysForReferenceField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
                        foreignKeys.addAll(fks);
                    } else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(embFmd.getType(), clr) != null && embFieldMapping.getNumberOfDatastoreMappings() > 0 && embFieldMapping instanceof PersistableMapping) {
                        // Field is for a PC class with the FK at this side, so add a FK to the table of this PC
                        ForeignKey fk = TableUtils.getForeignKeyForPCField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
                        if (fk != null) {
                            foreignKeys.add(fk);
                        }
                    }
                }
            } else if (mmd.getMap().keyIsPersistent()) {
                // FK from join table to key table
                referencedTable = storeMgr.getDatastoreClass(mmd.getMap().getKeyType(), clr);
                if (referencedTable != null) {
                    // Take <foreign-key> from <key>
                    ForeignKeyMetaData fkmd = null;
                    if (mmd.getKeyMetaData() != null) {
                        fkmd = mmd.getKeyMetaData().getForeignKeyMetaData();
                    }
                    if (fkmd != null || autoMode) {
                        ForeignKey fk = new ForeignKey(keyMapping, dba, referencedTable, true);
                        fk.setForMetaData(fkmd);
                        foreignKeys.add(fk);
                    }
                }
            }
        }
    } catch (NoTableManagedException e) {
    // expected when no table exists
    }
    return foreignKeys;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) EmbeddedValuePCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedValuePCMapping) ForeignKey(org.datanucleus.store.rdbms.key.ForeignKey) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) Collection(java.util.Collection) ForeignKeyMetaData(org.datanucleus.metadata.ForeignKeyMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)

Aggregations

PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)33 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)27 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)17 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)17 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)16 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)15 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)13 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)10 Collection (java.util.Collection)7 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)6 HashMap (java.util.HashMap)5 Iterator (java.util.Iterator)5 List (java.util.List)5 Map (java.util.Map)5 NucleusException (org.datanucleus.exceptions.NucleusException)5 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)5 FetchPlanForClass (org.datanucleus.FetchPlanForClass)4