Package org.jpox.store.mapped.mapping

Examples of org.jpox.store.mapped.mapping.JavaTypeMapping


        {
            for (int i=0;i<fmds.length;i++)
            {
                // Find the metadata for the actual field with the same name as this "index" field
                AbstractMemberMetaData realFmd = getFieldMetaData(fmds[i].getName());
                JavaTypeMapping fieldMapping = (JavaTypeMapping)fieldMappingsMap.get(realFmd);
                int countFields = fieldMapping.getNumberOfDatastoreFields();
                for (int j=0; j<countFields; j++)
                {
                    index.addDatastoreField(fieldMapping.getDataStoreMapping(j).getDatastoreField());
                }
            }
        }
        else
        {
View Full Code Here


        Set fieldNumbersSet = fieldMappingsMap.keySet();
        Iterator iter = fieldNumbersSet.iterator();
        while (iter.hasNext())
        {
            AbstractMemberMetaData fmd = (AbstractMemberMetaData) iter.next();
            JavaTypeMapping fieldMapping = (JavaTypeMapping)fieldMappingsMap.get(fmd);

            if (fmd.getEmbeddedMetaData() != null && fieldMapping instanceof EmbeddedPCMapping)
            {
                EmbeddedPCMapping embMapping = (EmbeddedPCMapping)fieldMapping;
                addExpectedForeignKeysForEmbeddedPCField(foreignKeys, autoMode, clr, embMapping);
            }
            else
            {
                if (storeMgr.getOMFContext().getTypeManager().isReferenceType(fmd.getType()) && fieldMapping 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(fieldMapping, fmd, autoMode, storeMgr, clr);
                    foreignKeys.addAll(fks);
                }
                else if (storeMgr.getOMFContext().getMetaDataManager().getMetaDataForClass(fmd.getType(), clr) != null &&
                        fieldMapping.getNumberOfDatastoreFields() > 0 &&
                        fieldMapping instanceof PersistenceCapableMapping)
                {
                    // 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(fieldMapping, fmd, 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 cmdIter = managedClassMetaData.iterator();
        while (cmdIter.hasNext())
        {
            ClassMetaData thisCmd = (ClassMetaData)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.keySet();
            Iterator externalFkKeysIter = externalFkKeys.iterator();
            while (externalFkKeysIter.hasNext())
            {
                AbstractMemberMetaData fmd = (AbstractMemberMetaData)externalFkKeysIter.next();
                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 = (JavaTypeMapping)externalFks.get(fmd);
                        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)
View Full Code Here

    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.getFieldMetaData();
                if (storeMgr.getOMFContext().getTypeManager().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.getOMFContext().getMetaDataManager().getMetaDataForClass(embFmd.getType(), clr) != null &&
                        embFieldMapping.getNumberOfDatastoreFields() > 0 &&
                        embFieldMapping instanceof PersistenceCapableMapping)
                {
                    // 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)
View Full Code Here

            // TODO Why use the default table alias ?, and why the default table expression below?
            // This uses the assumption that if this is a subquery then we find the right outer query candidate!
            stmt.newTableExpression(candidateTable, stmt.getMainTableAlias());

            // Inner Join from the ID of the value to the value mapping of the join table
            JavaTypeMapping valueTableID = candidateTable.getIDMapping();
            ScalarExpression valueMapExpr = valueMapping.newScalarExpression(stmt,
                stmt.getTableExpression(mapTableAlias));
            ScalarExpression valueExpr = valueTableID.newScalarExpression(stmt,
                stmt.getMainTableExpression());
            stmt.innerJoin(valueExpr, valueMapExpr, stmt.getMainTableExpression(), true, true);

            // Select the ID of the value table
            stmt.select(valueTableID);
View Full Code Here

        else
        {
            // Key = PC
            // Join the key table on the key ID column
            DatastoreClass keyTable = storeMgr.getDatastoreClass(filteredKeyType.getName(), stmt.getClassLoaderResolver());
            JavaTypeMapping keyTableID = keyTable.getIDMapping();

            LogicSetExpression keyTblExpr = stmt.getTableExpression(keyTableAlias);
            if (keyTblExpr==null)
            {
                keyTblExpr = stmt.newTableExpression(keyTable,keyTableAlias);
            }
            ScalarExpression keyMapExpr = keyMapping.newScalarExpression(stmt,stmt.getTableExpression(mapTableAlias));
            if (!parentStmt.hasCrossJoin(keyTblExpr))
            {
                stmt.crossJoin(keyTblExpr, true);
            }
            if( keyExpr == null )
            {
                keyExpr = keyTableID.newScalarExpression(stmt, stmt.getTableExpression(keyTableAlias));
            }
            if( keyExpr.getLogicSetExpression()!= null && !keyTable.equals(keyExpr.getLogicSetExpression().getMainTable()) )
            {
                //keyExpr might express a FK in another to the KEY table
                stmt.andCondition(keyMapExpr.eq(keyExpr),true);
                return this.keyMapping.newScalarExpression(stmt,stmt.getTableExpression(mapTableAlias));
            }
            else
            {
                //keyExpr might be a PK of the KEY table
                ScalarExpression kExpr = keyTableID.newScalarExpression(stmt, stmt.getTableExpression(keyTableAlias));
                stmt.andCondition(keyMapExpr.eq(kExpr),true);
                return kExpr;
            }
        }
    }
View Full Code Here

            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 = getFieldMetaData(fmds[i].getName());
                JavaTypeMapping fieldMapping = (JavaTypeMapping)fieldMappingsMap.get(realFmd);
                int countDatastoreFields = fieldMapping.getNumberOfDatastoreFields();
                for (int j=0; j<countDatastoreFields; j++)
                {
                    // Add each column of this field to the FK definition
                    sourceCols.add(fieldMapping.getDataStoreMapping(i).getDatastoreField());
                }
            }
        }

        if (sourceCols.size() != targetCols.size())
View Full Code Here

        else
        {
            // Value = PC
            // Join the value table on the value ID column
            DatastoreClass valueTable=storeMgr.getDatastoreClass(filteredValueType.getName(), stmt.getClassLoaderResolver());
            JavaTypeMapping valueTableID = valueTable.getIDMapping();
            LogicSetExpression valueTblExpr = stmt.getTableExpression(valueTableAlias);
            if (valueTblExpr == null)
            {
                valueTblExpr = stmt.newTableExpression(valueTable,valueTableAlias);
            }
            ScalarExpression valueMapExpr = valueMapping.newScalarExpression(stmt,stmt.getTableExpression(mapTableAlias));
            if (!parentStmt.hasCrossJoin(valueTblExpr))
            {
                stmt.crossJoin(valueTblExpr, true);
            }
            if (valExpr == null)
            {
                valExpr = valueTableID.newScalarExpression(stmt, stmt.getTableExpression(valueTableAlias));
            }
            if (valExpr.getLogicSetExpression() != null &&
                !valueTable.equals(valExpr.getLogicSetExpression().getMainTable()))
            {
                //valExpr might express a FK in another to the VALUE table
                stmt.andCondition(valueMapExpr.eq(valExpr),true);
                return this.valueMapping.newScalarExpression(stmt,stmt.getTableExpression(mapTableAlias));
            }
            else
            {
                //valExpr might be a PK of the VALUE table
                ScalarExpression valueExpr = valueTableID.newScalarExpression(stmt, stmt.getTableExpression(valueTableAlias));
                stmt.andCondition(valueMapExpr.eq(valueExpr),true);
                return valueExpr;
            }
        }
    }
View Full Code Here

        Set fieldNumbersSet = fieldMappingsMap.keySet();
        Iterator iter = fieldNumbersSet.iterator();
        while (iter.hasNext())
        {
            AbstractMemberMetaData fmd=(AbstractMemberMetaData) iter.next();
            JavaTypeMapping fieldMapping = (JavaTypeMapping)fieldMappingsMap.get(fmd);
            if (fieldMapping instanceof EmbeddedPCMapping)
            {
                // Add indexes for fields of this embedded PC object
                EmbeddedPCMapping embMapping = (EmbeddedPCMapping)fieldMapping;
                for (int i=0;i<embMapping.getNumberOfJavaTypeMappings();i++)
                {
                    JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                    UniqueMetaData umd = embFieldMapping.getFieldMetaData().getUniqueMetaData();
                    if (umd != null)
                    {
                        CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                        if (ck != null)
                        {
View Full Code Here

        {
            for (int i=0;i<fmds.length;i++)
            {
                // Find the metadata for the actual field with the same name as this "unique" field
                AbstractMemberMetaData realFmd = getFieldMetaData(fmds[i].getName());
                JavaTypeMapping fieldMapping = (JavaTypeMapping)fieldMappingsMap.get(realFmd);
                int countFields = fieldMapping.getNumberOfDatastoreFields();
                for (int j=0; j<countFields; j++)
                {
                    ck.addDatastoreField(fieldMapping.getDataStoreMapping(j).getDatastoreField());
                }
            }
        }
        else
        {
View Full Code Here

                        {
                            throw new ClassDefinitionException(LOCALISER.msg("057007",
                                mfmd.getFullFieldName(), key_field_name));
                        }

                        JavaTypeMapping ownerMapping = getFieldMapping(map_field_name);
                        JavaTypeMapping keyMapping = getFieldMapping(kmd.getName());

                        if (dba.supportsNullsInCandidateKeys() || (!ownerMapping.isNullable() && !keyMapping.isNullable()))
                        {
                            // If the owner and key fields are represented in this table then we can impose
                            // a unique constraint on them. If the key field is in a superclass then we
                            // cannot do this so just omit it.
                            if (keyMapping.getDatastoreContainer() == this &&
                                ownerMapping.getDatastoreContainer() == this)
                            {
                                CandidateKey ck = new CandidateKey(this);

                                // This HashSet is to avoid duplicate adding of columns.
                                HashSet addedColumns = new HashSet();

                                // Add columns for the owner field
                                int countOwnerFields = ownerMapping.getNumberOfDatastoreFields();
                                for (int i = 0; i < countOwnerFields; i++)
                                {
                                    Column col = (Column) ownerMapping.getDataStoreMapping(i).getDatastoreField();
                                    addedColumns.add(col);
                                    ck.addDatastoreField(col);
                                }

                                // Add columns for the key field
                                int countKeyFields = keyMapping.getNumberOfDatastoreFields();
                                for (int i = 0; i < countKeyFields; i++)
                                {
                                    Column col = (Column) keyMapping.getDataStoreMapping(i).getDatastoreField();
                                    if (!addedColumns.contains(col))
                                    {
                                        addedColumns.add(col);
                                        ck.addDatastoreField(col);
                                    }
                                    else
                                    {
                                        JPOXLogger.DATASTORE.warn(LOCALISER.msg("057041",
                                            ownerfmd.getName()));
                                    }
                                }

                                if (candidateKeysByMapField.put(mfmd, ck) != null)
                                {
                                    // We have multiple "mapped-by" coming to this field so give a warning that this may potentially
                                    // cause problems. For example if they have the key field defined here for 2 different relations
                                    // so you may get keys/values appearing in the other relation that shouldn't be.
                                    // Logged as a WARNING for now.
                                    // If there is a situation where this should throw an exception, please update this AND COMMENT WHY.
                                    JPOXLogger.DATASTORE.warn(LOCALISER.msg("057012",
                                        mfmd.getFullFieldName(), ownerfmd.getFullFieldName()));
                                }
                            }
                        }
                    }
                    else if (ownerfmd.getValueMetaData() != null && ownerfmd.getValueMetaData().getMappedBy() != null)
                    {
                        // Value field is stored in the key table
                        AbstractMemberMetaData vmd = null;
                        String value_field_name = ownerfmd.getValueMetaData().getMappedBy();
                        if (value_field_name != null)
                        {
                            vmd = cmd.getMetaDataForMember(value_field_name);
                        }
                        if (vmd == null)
                        {
                            throw new ClassDefinitionException(LOCALISER.msg("057008", mfmd));
                        }

                        JavaTypeMapping ownerMapping = getFieldMapping(map_field_name);
                        JavaTypeMapping valueMapping = getFieldMapping(vmd.getName());

                        if (dba.supportsNullsInCandidateKeys() || (!ownerMapping.isNullable() && !valueMapping.isNullable()))
                        {
                            // If the owner and value fields are represented in this table then we can impose
                            // a unique constraint on them. If the value field is in a superclass then we
                            // cannot do this so just omit it.
                            if (valueMapping.getDatastoreContainer() == this &&
                                ownerMapping.getDatastoreContainer() == this)
                            {
                                CandidateKey ck = new CandidateKey(this);

                                // This HashSet is to avoid duplicate adding of columns.
                                HashSet addedColumns = new HashSet();

                                // Add columns for the owner field
                                int countOwnerFields = ownerMapping.getNumberOfDatastoreFields();
                                for (int i = 0; i < countOwnerFields; i++)
                                {
                                    Column col = (Column) ownerMapping.getDataStoreMapping(i).getDatastoreField();
                                    addedColumns.add(col);
                                    ck.addDatastoreField(col);
                                }

                                // Add columns for the value field
                                int countValueFields = valueMapping.getNumberOfDatastoreFields();
                                for (int i = 0; i < countValueFields; i++)
                                {
                                    Column col = (Column) valueMapping.getDataStoreMapping(i).getDatastoreField();
                                    if (!addedColumns.contains(col))
                                    {
                                        addedColumns.add(col);
                                        ck.addDatastoreField(col);
                                    }
View Full Code Here

TOP

Related Classes of org.jpox.store.mapped.mapping.JavaTypeMapping

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.