Search in sources :

Example 1 with EmbeddedValuePCMapping

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

the class AbstractMapStore method getUpdateEmbeddedValueStmt.

/**
 * Generate statement for update the field of an embedded value.
 * <PRE>
 * UPDATE MAPTABLE
 * SET EMBEDDEDVALUECOL1 = ?
 * WHERE OWNERCOL=?
 * AND EMBEDDEDVALUECOL1 = ?
 * AND EMBEDDEDVALUECOL2 = ? ...
 * </PRE>
 * @param fieldMapping The mapping for the field to be updated
 * @param ownerMapping The owner mapping
 * @param valueMapping mapping for the value
 * @param mapTable The map table
 * @return Statement for updating an embedded value in the Set
 */
protected String getUpdateEmbeddedValueStmt(JavaTypeMapping fieldMapping, JavaTypeMapping ownerMapping, JavaTypeMapping valueMapping, Table mapTable) {
    StringBuilder stmt = new StringBuilder("UPDATE ");
    stmt.append(mapTable.toString());
    stmt.append(" SET ");
    for (int i = 0; i < fieldMapping.getNumberOfDatastoreMappings(); i++) {
        if (i > 0) {
            stmt.append(",");
        }
        stmt.append(fieldMapping.getDatastoreMapping(i).getColumn().getIdentifier().toString());
        stmt.append(" = ");
        stmt.append(fieldMapping.getDatastoreMapping(i).getUpdateInputParameter());
    }
    stmt.append(" WHERE ");
    BackingStoreHelper.appendWhereClauseForMapping(stmt, ownerMapping, null, true);
    EmbeddedValuePCMapping embeddedMapping = (EmbeddedValuePCMapping) valueMapping;
    for (int i = 0; i < embeddedMapping.getNumberOfJavaTypeMappings(); i++) {
        JavaTypeMapping m = embeddedMapping.getJavaTypeMapping(i);
        if (m != null) {
            for (int j = 0; j < m.getNumberOfDatastoreMappings(); j++) {
                stmt.append(" AND ");
                stmt.append(m.getDatastoreMapping(j).getColumn().getIdentifier().toString());
                stmt.append(" = ");
                stmt.append(m.getDatastoreMapping(j).getUpdateInputParameter());
            }
        }
    }
    return stmt.toString();
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) EmbeddedValuePCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedValuePCMapping)

Example 2 with EmbeddedValuePCMapping

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

the class MapTable 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.
 * @param clr The ClassLoaderResolver
 * @return The indices
 */
protected Set getExpectedIndices(ClassLoaderResolver clr) {
    Set 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 the key FK (if required)
    if (keyMapping instanceof EmbeddedKeyPCMapping) {
        // Add all indices required by fields of the embedded key
        EmbeddedKeyPCMapping embMapping = (EmbeddedKeyPCMapping) keyMapping;
        for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
            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 {
        KeyMetaData keymd = mmd.getKeyMetaData();
        if (keymd != null && keymd.getIndexMetaData() != null) {
            IndexMetaData idxmd = mmd.getKeyMetaData().getIndexMetaData();
            Index index = TableUtils.getIndexForField(this, idxmd, keyMapping);
            if (index != null) {
                indices.add(index);
            }
        } else {
            // Fallback to an index for any foreign-key to the key
            if (keyMapping instanceof PersistableMapping) {
                Index index = TableUtils.getIndexForField(this, null, keyMapping);
                if (index != null) {
                    indices.add(index);
                }
            }
        }
    }
    // Index for the value FK (if required)
    if (valueMapping instanceof EmbeddedValuePCMapping) {
        // Add all indices required by fields of the embedded value
        EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping) valueMapping;
        for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
            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 {
        ValueMetaData valmd = mmd.getValueMetaData();
        if (valmd != null && valmd.getIndexMetaData() != null) {
            IndexMetaData idxmd = mmd.getValueMetaData().getIndexMetaData();
            Index index = TableUtils.getIndexForField(this, idxmd, valueMapping);
            if (index != null) {
                indices.add(index);
            }
        } else {
            // Fallback to an index for any foreign-key to the value
            if (valueMapping instanceof PersistableMapping) {
                Index index = TableUtils.getIndexForField(this, null, valueMapping);
                if (index != null) {
                    indices.add(index);
                }
            }
        }
    }
    return indices;
}
Also used : PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) HashSet(java.util.HashSet) Set(java.util.Set) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) KeyMetaData(org.datanucleus.metadata.KeyMetaData) PrimaryKeyMetaData(org.datanucleus.metadata.PrimaryKeyMetaData) ForeignKeyMetaData(org.datanucleus.metadata.ForeignKeyMetaData) ValueMetaData(org.datanucleus.metadata.ValueMetaData) Index(org.datanucleus.store.rdbms.key.Index) EmbeddedValuePCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedValuePCMapping) HashSet(java.util.HashSet) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping) IndexMetaData(org.datanucleus.metadata.IndexMetaData)

Example 3 with EmbeddedValuePCMapping

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

the class MapTable method initialize.

/**
 * Method to initialise the table definition.
 * @param clr The ClassLoaderResolver
 */
public void initialize(ClassLoaderResolver clr) {
    assertIsUninitialized();
    MapMetaData mapmd = mmd.getMap();
    if (mapmd == null) {
        throw new NucleusUserException(Localiser.msg("057017", mmd));
    }
    PrimaryKeyMetaData pkmd = (mmd.getJoinMetaData() != null ? mmd.getJoinMetaData().getPrimaryKeyMetaData() : null);
    boolean pkColsSpecified = (pkmd != null && pkmd.getColumnMetaData() != null);
    boolean pkRequired = requiresPrimaryKey();
    // Add owner mapping
    ColumnMetaData[] ownerColmd = null;
    if (mmd.getJoinMetaData() != null && mmd.getJoinMetaData().getColumnMetaData() != null && mmd.getJoinMetaData().getColumnMetaData().length > 0) {
        // Column mappings defined at this side (1-N, M-N)
        // When specified at this side they use the <join> tag
        ownerColmd = mmd.getJoinMetaData().getColumnMetaData();
    }
    ownerMapping = ColumnCreator.createColumnsForJoinTables(clr.classForName(ownerType), mmd, ownerColmd, storeMgr, this, pkRequired, false, FieldRole.ROLE_OWNER, clr, null);
    if (NucleusLogger.DATASTORE.isDebugEnabled()) {
        logMapping(mmd.getFullFieldName() + ".[OWNER]", ownerMapping);
    }
    String keyValueFieldName = (mmd.getKeyMetaData() != null ? mmd.getKeyMetaData().getMappedBy() : null);
    String valueKeyFieldName = (mmd.getValueMetaData() != null ? mmd.getValueMetaData().getMappedBy() : null);
    // Add key mapping
    boolean keyPC = (mmd.hasMap() && mmd.getMap().keyIsPersistent());
    Class keyCls = clr.classForName(mapmd.getKeyType());
    if (keyValueFieldName != null && isEmbeddedValuePC()) {
    // Added in value code
    } else if (isSerialisedKey() || isEmbeddedKeyPC() || (isEmbeddedKey() && !keyPC) || ClassUtils.isReferenceType(keyCls)) {
        // Key = PC(embedded), PC(serialised), Non-PC(serialised), Non-PC(embedded), Reference
        keyMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_MAP_KEY);
        if (Boolean.TRUE.equals(mmd.getContainer().allowNulls())) {
            // Make all key col(s) nullable so we can store null elements
            for (int i = 0; i < keyMapping.getNumberOfDatastoreMappings(); i++) {
                Column elementCol = keyMapping.getDatastoreMapping(i).getColumn();
                elementCol.setNullable(true);
            }
        }
        if (NucleusLogger.DATASTORE.isDebugEnabled()) {
            logMapping(mmd.getFullFieldName() + ".[KEY]", keyMapping);
        }
        if (valueKeyFieldName != null && isEmbeddedKeyPC()) {
            // Key (PC) is embedded and value is a field of the key
            EmbeddedKeyPCMapping embMapping = (EmbeddedKeyPCMapping) keyMapping;
            valueMapping = embMapping.getJavaTypeMapping(valueKeyFieldName);
        }
    } else {
        // Key = PC
        ColumnMetaData[] keyColmd = null;
        KeyMetaData keymd = mmd.getKeyMetaData();
        if (keymd != null && keymd.getColumnMetaData() != null && keymd.getColumnMetaData().length > 0) {
            // Column mappings defined at this side (1-N, M-N)
            keyColmd = keymd.getColumnMetaData();
        }
        keyMapping = ColumnCreator.createColumnsForJoinTables(keyCls, mmd, keyColmd, storeMgr, this, false, false, FieldRole.ROLE_MAP_KEY, clr, null);
        if (mmd.getContainer().allowNulls() == Boolean.TRUE) {
            // Make all key col(s) nullable so we can store null elements
            for (int i = 0; i < keyMapping.getNumberOfDatastoreMappings(); i++) {
                Column elementCol = keyMapping.getDatastoreMapping(i).getColumn();
                elementCol.setNullable(true);
            }
        }
        if (NucleusLogger.DATASTORE.isDebugEnabled()) {
            logMapping(mmd.getFullFieldName() + ".[KEY]", keyMapping);
        }
    }
    // Add value mapping
    boolean valuePC = (mmd.hasMap() && mmd.getMap().valueIsPersistent());
    Class valueCls = clr.classForName(mapmd.getValueType());
    if (valueKeyFieldName != null && isEmbeddedKeyPC()) {
    // Added in key code
    } else if (isSerialisedValue() || isEmbeddedValuePC() || (isEmbeddedValue() && !valuePC) || ClassUtils.isReferenceType(valueCls)) {
        // Value = PC(embedded), PC(serialised), Non-PC(serialised), Non-PC(embedded), Reference
        valueMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_MAP_VALUE);
        if (mmd.getContainer().allowNulls() == Boolean.TRUE) {
            // Make all value col(s) nullable so we can store null elements
            for (int i = 0; i < valueMapping.getNumberOfDatastoreMappings(); i++) {
                Column elementCol = valueMapping.getDatastoreMapping(i).getColumn();
                elementCol.setNullable(true);
            }
        }
        if (NucleusLogger.DATASTORE.isDebugEnabled()) {
            logMapping(mmd.getFullFieldName() + ".[VALUE]", valueMapping);
        }
        if (keyValueFieldName != null && isEmbeddedValuePC()) {
            // Value (PC) is embedded and key is a field of the value
            EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping) valueMapping;
            keyMapping = embMapping.getJavaTypeMapping(keyValueFieldName);
        }
    } else {
        // Value = PC
        ColumnMetaData[] valueColmd = null;
        ValueMetaData valuemd = mmd.getValueMetaData();
        if (valuemd != null && valuemd.getColumnMetaData() != null && valuemd.getColumnMetaData().length > 0) {
            // Column mappings defined at this side (1-N, M-N)
            valueColmd = valuemd.getColumnMetaData();
        }
        valueMapping = ColumnCreator.createColumnsForJoinTables(clr.classForName(mapmd.getValueType()), mmd, valueColmd, storeMgr, this, false, true, FieldRole.ROLE_MAP_VALUE, clr, null);
        if (mmd.getContainer().allowNulls() == Boolean.TRUE) {
            // Make all value col(s) nullable so we can store null elements
            for (int i = 0; i < valueMapping.getNumberOfDatastoreMappings(); i++) {
                Column elementCol = valueMapping.getDatastoreMapping(i).getColumn();
                elementCol.setNullable(true);
            }
        }
        if (NucleusLogger.DATASTORE.isDebugEnabled()) {
            logMapping(mmd.getFullFieldName() + ".[VALUE]", valueMapping);
        }
    }
    // Add order mapping if required
    boolean orderRequired = false;
    if (mmd.getOrderMetaData() != null) {
        // User requested order column so add one
        orderRequired = true;
    } else if (requiresPrimaryKey() && !pkColsSpecified) {
        // PK is required so maybe need to add an index to form the PK
        if (isEmbeddedKeyPC()) {
            if (mmd.hasExtension("surrogate-pk-column") && mmd.getValueForExtension("surrogate-pk-column").equalsIgnoreCase("true")) {
                // Allow user to request surrogate pk column be added (for use with JPA)
                orderRequired = true;
            } else if (storeMgr.getApiAdapter().getName().equalsIgnoreCase("JDO") && mmd.getMap().getKeyClassMetaData(clr).getIdentityType() != IdentityType.APPLICATION) {
                // Embedded key PC with datastore id so we need an index to form the PK TODO It is arguable that we can just use all embedded key fields as part of PK here always
                orderRequired = true;
            }
        } else if (isSerialisedKey()) {
            // Serialised key, so need an index to form the PK
            orderRequired = true;
        } else if (keyMapping instanceof ReferenceMapping) {
            // ReferenceMapping, so have order if more than 1 implementation
            ReferenceMapping refMapping = (ReferenceMapping) keyMapping;
            if (refMapping.getJavaTypeMapping().length > 1) {
                orderRequired = true;
            }
        } else if (!(keyMapping instanceof PersistableMapping)) {
            // Non-PC, so depends if the key column can be used as part of a PK
            // TODO This assumes the keyMapping has a single column but what if it is Color with 4 cols?
            Column elementCol = keyMapping.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/Adapter (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 using internal rules
            if (orderRequired) {
                // Order column specified so owner+order are the PK
                orderMapping.getDatastoreMapping(0).getColumn().setPrimaryKey();
            } else {
                // No order column specified so owner+key are the PK
                for (int i = 0; i < keyMapping.getNumberOfDatastoreMappings(); i++) {
                    keyMapping.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 : KeyMetaData(org.datanucleus.metadata.KeyMetaData) PrimaryKeyMetaData(org.datanucleus.metadata.PrimaryKeyMetaData) ForeignKeyMetaData(org.datanucleus.metadata.ForeignKeyMetaData) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) EmbeddedValuePCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedValuePCMapping) MapMetaData(org.datanucleus.metadata.MapMetaData) 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) ValueMetaData(org.datanucleus.metadata.ValueMetaData) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)

Example 4 with EmbeddedValuePCMapping

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

the class MapTable method getExpectedCandidateKeys.

/**
 * Accessor for the candidate keys for this table.
 * @return The indices
 */
protected List getExpectedCandidateKeys() {
    // The indices required by foreign keys (BaseTable)
    List candidateKeys = super.getExpectedCandidateKeys();
    if (keyMapping instanceof EmbeddedKeyPCMapping) {
        // Add all candidate keys required by fields of the embedded key
        EmbeddedKeyPCMapping embMapping = (EmbeddedKeyPCMapping) keyMapping;
        for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
            JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
            UniqueMetaData umd = embFieldMapping.getMemberMetaData().getUniqueMetaData();
            if (umd != null) {
                CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                if (ck != null) {
                    candidateKeys.add(ck);
                }
            }
        }
    } else if (mmd.getKeyMetaData() != null) {
        UniqueMetaData unimd = mmd.getKeyMetaData().getUniqueMetaData();
        if (unimd != null) {
            CandidateKey ck = TableUtils.getCandidateKeyForField(this, unimd, keyMapping);
            if (ck != null) {
                candidateKeys.add(ck);
            }
        }
    }
    if (valueMapping instanceof EmbeddedValuePCMapping) {
        // Add all candidate keys required by fields of the embedded value
        EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping) valueMapping;
        for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
            JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
            UniqueMetaData umd = embFieldMapping.getMemberMetaData().getUniqueMetaData();
            if (umd != null) {
                CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                if (ck != null) {
                    candidateKeys.add(ck);
                }
            }
        }
    } else if (mmd.getValueMetaData() != null) {
        UniqueMetaData unimd = mmd.getValueMetaData().getUniqueMetaData();
        if (unimd != null) {
            CandidateKey ck = TableUtils.getCandidateKeyForField(this, unimd, valueMapping);
            if (ck != null) {
                candidateKeys.add(ck);
            }
        }
    }
    return candidateKeys;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) List(java.util.List) UniqueMetaData(org.datanucleus.metadata.UniqueMetaData) EmbeddedValuePCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedValuePCMapping) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)

Example 5 with EmbeddedValuePCMapping

use of org.datanucleus.store.rdbms.mapping.java.EmbeddedValuePCMapping 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

EmbeddedValuePCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedValuePCMapping)7 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)6 EmbeddedKeyPCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)4 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)3 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)3 ArrayList (java.util.ArrayList)2 KeyMetaData (org.datanucleus.metadata.KeyMetaData)2 PrimaryKeyMetaData (org.datanucleus.metadata.PrimaryKeyMetaData)2 ValueMetaData (org.datanucleus.metadata.ValueMetaData)2 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)1 IndexMetaData (org.datanucleus.metadata.IndexMetaData)1 MapMetaData (org.datanucleus.metadata.MapMetaData)1