Package org.datanucleus.store.rdbms.key

Examples of org.datanucleus.store.rdbms.key.ForeignKey


            // FK from join table to owner table
            DatastoreClass referencedTable = storeMgr.getDatastoreClass(mmd.getClassName(true), clr);
            if (referencedTable != null)
            {
                // Single owner table, so add a single FK to the owner as appropriate
                ForeignKey fk = null;
                if (referencedTable != null)
                {
                    // Take <foreign-key> from <join>
                    ForeignKeyMetaData fkmd = null;
                    if (mmd.getJoinMetaData() != null)
                    {
                        fkmd = mmd.getJoinMetaData().getForeignKeyMetaData();
                    }
                    if (fkmd != null || autoMode)
                    {
                        fk = new ForeignKey(ownerMapping, dba, referencedTable, true);
                        fk.setForMetaData(fkmd);
                    }
                    if (fk != null)
                    {
                        foreignKeys.add(fk);
                    }
                }
            }

            // FK from join table to related table
            referencedTable = storeMgr.getDatastoreClass(mmd.getTypeName(), clr);
            if (referencedTable != null)
            {
                // Take <foreign-key> from <field>
                ForeignKey fk = null;
                ForeignKeyMetaData fkmd = mmd.getForeignKeyMetaData();
                if (fkmd != null || autoMode)
                {
                    fk = new ForeignKey(relatedMapping, dba, referencedTable, true);
                    fk.setForMetaData(fkmd);
                }
                if (fk != null)
                {
                    foreignKeys.add(fk);
                }
View Full Code Here


                else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getType(), clr) != null &&
                        memberMapping.getNumberOfDatastoreMappings() > 0 &&
                        memberMapping 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(memberMapping, mmd, autoMode, storeMgr, clr);
                    if (fk != null)
                    {
                        foreignKeys.add(fk);
                    }
                }
            }
        }

        // FK from id column(s) to id column(s) of superclass, as specified by
        // <inheritance><join><foreign-key ...></join></inheritance>
        ForeignKeyMetaData idFkmd = (cmd.getInheritanceMetaData().getJoinMetaData() != null) ?
                cmd.getInheritanceMetaData().getJoinMetaData().getForeignKeyMetaData() : null;
        if (supertable != null && (autoMode || (idFkmd != null && idFkmd.getDeleteAction() != ForeignKeyAction.NONE)))
        {
            ForeignKey fk = new ForeignKey(getIdMapping(), dba, supertable, false);
            if (idFkmd != null && idFkmd.getName() != null)
            {
                fk.setName(idFkmd.getName());
            }
            foreignKeys.add(0, fk);
        }

        // Add any user-required FKs for the class as a whole
        // <class><foreign-key>...</foreign-key></field>
        Iterator<AbstractClassMetaData> cmdIter = managedClassMetaData.iterator();
        while (cmdIter.hasNext())
        {
            AbstractClassMetaData thisCmd = cmdIter.next();
            ForeignKeyMetaData[] fkmds = thisCmd.getForeignKeyMetaData();
            if (fkmds != null)
            {
                for (int i=0;i<fkmds.length;i++)
                {
                    ForeignKey fk = getForeignKeyForForeignKeyMetaData(fkmds[i]);
                    if (fk != null)
                    {
                        foreignKeys.add(fk);
                    }
                }
            }
        }

        HashMap externalFks = getExternalFkMappings();
        if (!externalFks.isEmpty())
        {
            // 1-N FK relationships - FK to id column(s) of owner table where this is the element table and we have a FK
            Collection externalFkKeys = externalFks.entrySet();
            Iterator<Map.Entry<AbstractMemberMetaData, JavaTypeMapping>> externalFkKeysIter = externalFkKeys.iterator();
            while (externalFkKeysIter.hasNext())
            {
                Map.Entry<AbstractMemberMetaData, JavaTypeMapping> entry = externalFkKeysIter.next();
                AbstractMemberMetaData fmd = entry.getKey();
                DatastoreClass referencedTable = storeMgr.getDatastoreClass(fmd.getAbstractClassMetaData().getFullClassName(), clr);
                if (referencedTable != null)
                {
                    // Take <foreign-key> from either <field> or <element>
                    ForeignKeyMetaData fkmd = fmd.getForeignKeyMetaData();
                    if (fkmd == null && fmd.getElementMetaData() != null)
                    {
                        fkmd = fmd.getElementMetaData().getForeignKeyMetaData();
                    }
                    if ((fkmd != null && fkmd.getDeleteAction() != ForeignKeyAction.NONE) || autoMode)
                    {
                        // Either has been specified by user, or using autoMode, so add FK
                        JavaTypeMapping fkMapping = entry.getValue();
                        ForeignKey fk = new ForeignKey(fkMapping, dba, referencedTable, true);
                        fk.setForMetaData(fkmd); // Does nothing when no FK MetaData
                        if (!foreignKeys.contains(fk))
                        {
                            // Only add when not already present (in the case of shared FKs there can be dups here)
                            foreignKeys.add(fk);
                        }
View Full Code Here

                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);
                    }
                }
View Full Code Here

        {
            return null;
        }

        // Create the ForeignKey base details
        ForeignKey fk = new ForeignKey(fkmd.isDeferred());
        fk.setForMetaData(fkmd);

        // Find the target of the foreign-key
        AbstractClassMetaData acmd = cmd;
        if (fkmd.getTable() == null)
        {
            // Can't create a FK if we don't know where it goes to
            NucleusLogger.DATASTORE_SCHEMA.warn(LOCALISER.msg("058105", acmd.getFullClassName()));
            return null;
        }
        DatastoreIdentifier tableId = storeMgr.getIdentifierFactory().newDatastoreContainerIdentifier(fkmd.getTable());
        ClassTable refTable = (ClassTable)storeMgr.getDatastoreClass(tableId);
        if (refTable == null)
        {
            // TODO Go to the datastore and query for this table to get the columns of the PK
            NucleusLogger.DATASTORE_SCHEMA.warn(LOCALISER.msg("058106", acmd.getFullClassName(),
                fkmd.getTable()));
            return null;
        }
        PrimaryKey pk = refTable.getPrimaryKey();
        List targetCols = pk.getColumns();

        // Generate the columns for the source of the foreign-key
        List sourceCols = new ArrayList();
        ColumnMetaData[] colmds = fkmd.getColumnMetaData();
        AbstractMemberMetaData[] fmds = fkmd.getMemberMetaData();
        if (colmds != null && colmds.length > 0)
        {
            // FK specified via <column>
            for (int i=0;i<colmds.length;i++)
            {
                // Find the column and add to the source columns for the FK
                DatastoreIdentifier colId = storeMgr.getIdentifierFactory().newDatastoreFieldIdentifier(colmds[i].getName());
                Column sourceCol = columnsByName.get(colId);
                if (sourceCol == null)
                {
                    NucleusLogger.DATASTORE_SCHEMA.warn(LOCALISER.msg("058107",
                        acmd.getFullClassName(), fkmd.getTable(), colmds[i].getName(), toString()));
                    return null;
                }
                sourceCols.add(sourceCol);
            }
        }
        else if (fmds != null && fmds.length > 0)
        {
            // FK specified via <field>
            for (int i=0;i<fmds.length;i++)
            {
                // Find the metadata for the actual field with the same name as this "foreign-key" field
                // and add all columns to the source columns for the FK
                AbstractMemberMetaData realFmd = getMetaDataForMember(fmds[i].getName());
                JavaTypeMapping fieldMapping = memberMappingsMap.get(realFmd);
                int countDatastoreFields = fieldMapping.getNumberOfDatastoreMappings();
                for (int j=0; j<countDatastoreFields; j++)
                {
                    // Add each column of this field to the FK definition
                    sourceCols.add(fieldMapping.getDatastoreMapping(j).getDatastoreField());
                }
            }
        }

        if (sourceCols.size() != targetCols.size())
        {
            // Different number of cols in this table and target table
            NucleusLogger.DATASTORE_SCHEMA.warn(LOCALISER.msg("058108",
                acmd.getFullClassName(), fkmd.getTable(), "" + sourceCols.size(), "" + targetCols.size()));
        }

        // Add all column mappings to the ForeignKey
        if (sourceCols.size() > 0)
        {
            for (int i=0;i<sourceCols.size();i++)
            {
                Column source = (Column)sourceCols.get(i);
                String targetColName = colmds[i].getTarget();
                Column target = (Column)targetCols.get(i); // Default to matching via the natural order
                if (targetColName != null)
                {
                    // User has specified the target column for this col so try it in our target list
                    for (int j=0;j<targetCols.size();j++)
                    {
                        Column targetCol = (Column)targetCols.get(j);
                        if (targetCol.getIdentifier().getIdentifierName().equalsIgnoreCase(targetColName))
                        {
                            // Found the required column
                            target = targetCol;
                            break;
                        }
                    }
                }
                fk.addDatastoreField(source, target);
            }
        }

        return fk;
    }
View Full Code Here

                {
                    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);
                        }
                    }
                }
            }
View Full Code Here

                {
                    ForeignKeyMetaData fkmd = mmd.getForeignKeyMetaData();
                    if ((fkmd != null && fkmd.getDeleteAction() != ForeignKeyAction.NONE) || autoMode)
                    {
                        // Either has been specified by user, or using autoMode, so add FK
                        ForeignKey fk = new ForeignKey(implMapping, storeMgr.getDatastoreAdapter(), referencedTable, true);
                        fk.setForMetaData(fkmd); // Does nothing when no FK MetaData
                        fks.add(fk);
                    }
                }
            }
        }
View Full Code Here

        {
            ForeignKeyMetaData fkmd = mmd.getForeignKeyMetaData();
            if ((fkmd != null && fkmd.getDeleteAction() != ForeignKeyAction.NONE) || autoMode)
            {
                // Either has been specified by user, or using autoMode, so add FK
                ForeignKey fk = new ForeignKey(fieldMapping, storeMgr.getDatastoreAdapter(), referencedTable, true);
                fk.setForMetaData(fkmd); // Does nothing when no FK MetaData
                return fk;
            }
        }
        return null;
    }
View Full Code Here

                    {
                        for (int j = 0; j < col.getJavaTypeMapping().getNumberOfDatastoreMappings(); j++)
                        {
                            colsInFKs.add(col.getJavaTypeMapping().getDatastoreMapping(j).getDatastoreField());
                        }
                        ForeignKey fk = new ForeignKey(col.getJavaTypeMapping(), dba, referencedTable, true);
                        foreignKeys.add(fk);
                    }
                }
                catch (NoTableManagedException e)
                {
View Full Code Here

         */
        PrimaryKey pk = getPrimaryKey();
        Iterator i = getExpectedForeignKeys(clr).iterator();
        while (i.hasNext())
        {
            ForeignKey fk = (ForeignKey) i.next();
            if (!pk.getColumnList().equals(fk.getColumnList()))
            {
                indices.add(new Index(fk));
            }
        }

View Full Code Here

                    fkIdentifier = idFactory.newIdentifier(IdentifierType.FOREIGN_KEY, fkName);
                }
   
                short deferrability = ((Short)fkInfo.getProperty("deferrability")).shortValue();
                boolean initiallyDeferred = deferrability == DatabaseMetaData.importedKeyInitiallyDeferred;
                ForeignKey fk = (ForeignKey) foreignKeysByName.get(fkIdentifier);
                if (fk == null)
                {
                    fk = new ForeignKey(initiallyDeferred);
                    fk.setName(fkIdentifier.getIdentifierName());
                    foreignKeysByName.put(fkIdentifier, fk);
                }
   
                String pkTableName = (String)fkInfo.getProperty("pk_table_name");
                AbstractTable refTable = (AbstractTable)storeMgr.getDatastoreClass(
                    idFactory.newDatastoreContainerIdentifier(pkTableName));
                if (refTable != null)
                {
                    String fkColumnName = (String)fkInfo.getProperty("fk_column_name");
                    String pkColumnName = (String)fkInfo.getProperty("pk_column_name");
                    DatastoreIdentifier colName = idFactory.newIdentifier(IdentifierType.COLUMN, fkColumnName);
                    DatastoreIdentifier refColName = idFactory.newIdentifier(IdentifierType.COLUMN, pkColumnName);
                    DatastoreField col = columnsByName.get(colName);
                    DatastoreField refCol = refTable.columnsByName.get(refColName);
                    if (col != null && refCol != null)
                    {
                        fk.addDatastoreField(col, refCol);
                    }
                    else
                    {
                        //TODO throw exception?
                    }
View Full Code Here

TOP

Related Classes of org.datanucleus.store.rdbms.key.ForeignKey

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.