Package org.datanucleus.metadata

Examples of org.datanucleus.metadata.AbstractMemberMetaData


     * @param ownerMmd metadata for the field/property with the map in the owner class
     */
    private void initializeFKMapUniqueConstraints(AbstractMemberMetaData ownerMmd)
    {
        // Load "mapped-by"
        AbstractMemberMetaData mfmd = null;
        String map_field_name = ownerMmd.getMappedBy(); // Field in our class that maps back to the owner class
        if (map_field_name != null)
        {
            // Bidirectional
            mfmd = cmd.getMetaDataForMember(map_field_name);
            if (mfmd == null)
            {
                // Field not in primary class so may be in subclass so check all managed classes
                Iterator<AbstractClassMetaData> cmdIter = managedClassMetaData.iterator();
                while (cmdIter.hasNext())
                {
                    AbstractClassMetaData managedCmd = cmdIter.next();
                    mfmd = managedCmd.getMetaDataForMember(map_field_name);
                    if (mfmd != null)
                    {
                        break;
                    }
                }
            }
            if (mfmd == null)
            {
                // "mapped-by" refers to a field in our class that doesnt exist!
                throw new NucleusUserException(LOCALISER.msg("057036",
                    map_field_name, cmd.getFullClassName(), ownerMmd.getFullFieldName()));                      
            }

            if (mfmd != null)
            {
                if (ownerMmd.getJoinMetaData() == null)
                {
                    // Load field of key in value
                    if (ownerMmd.getKeyMetaData() != null && ownerMmd.getKeyMetaData().getMappedBy() != null)
                    {
                        // Key field is stored in the value table
                        AbstractMemberMetaData kmd = null;
                        String key_field_name = ownerMmd.getKeyMetaData().getMappedBy();
                        if (key_field_name != null)
                        {
                            kmd = cmd.getMetaDataForMember(key_field_name);
                        }
                        if (kmd == null)
                        {
                            // Field not in primary class so may be in subclass so check all managed classes
                            Iterator<AbstractClassMetaData> cmdIter = managedClassMetaData.iterator();
                            while (cmdIter.hasNext())
                            {
                                AbstractClassMetaData managedCmd = cmdIter.next();
                                kmd = managedCmd.getMetaDataForMember(key_field_name);
                                if (kmd != null)
                                {
                                    break;
                                }
                            }
                        }
                        if (kmd == null)
                        {
                            throw new ClassDefinitionException(LOCALISER.msg("057007",
                                mfmd.getFullFieldName(), key_field_name));
                        }

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

                        if (dba.supportsOption(RDBMSAdapter.NULLS_IN_CANDIDATE_KEYS) ||
                            (!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.getNumberOfDatastoreMappings();
                                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.getNumberOfDatastoreMappings();
                                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
                                    {
                                        NucleusLogger.DATASTORE.warn(LOCALISER.msg("057041",
                                            ownerMmd.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.
                                    NucleusLogger.DATASTORE.warn(LOCALISER.msg("057012",
                                        mfmd.getFullFieldName(), ownerMmd.getFullFieldName()));
                                }
                            }
                        }
                    }
                    else if (ownerMmd.getValueMetaData() != null && ownerMmd.getValueMetaData().getMappedBy() != null)
                    {
                        // Value field is stored in the key table
                        AbstractMemberMetaData vmd = null;
                        String value_field_name = ownerMmd.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 = getMemberMapping(map_field_name);
                        JavaTypeMapping valueMapping = getMemberMapping(vmd.getName());

                        if (dba.supportsOption(RDBMSAdapter.NULLS_IN_CANDIDATE_KEYS) ||
                            (!ownerMapping.isNullable() && !valueMapping.isNullable()))
                        {
                            // If the owner and value fields are represented in this table then we can impose
View Full Code Here


     * @throws NoSuchPersistentFieldException Thrown when the field/property is not found
     */
    public JavaTypeMapping getMemberMapping(String memberName)
    {
        assertIsInitialized();
        AbstractMemberMetaData mmd = getMetaDataForMember(memberName);
        JavaTypeMapping m = getMemberMapping(mmd);
        if (m == null)
        {
            throw new NoSuchPersistentFieldException(cmd.getFullClassName(), memberName);
        }
View Full Code Here

     * @return metadata for the member
     */
    AbstractMemberMetaData getMetaDataForMember(String memberName)
    {
        // TODO Dont search "cmd" since it is included in "managedClassMetaData"
        AbstractMemberMetaData mmd = cmd.getMetaDataForMember(memberName);
        if (mmd == null)
        {
            Iterator<AbstractClassMetaData> iter = managedClassMetaData.iterator();
            while (iter.hasNext())
            {
                AbstractClassMetaData theCmd = iter.next();
                final AbstractMemberMetaData foundMmd = theCmd.getMetaDataForMember(memberName);
                if (foundMmd != null)
                {
                    if (mmd != null && (!mmd.toString().equalsIgnoreCase(foundMmd.toString()) || mmd.getType() != foundMmd.getType()))
                    {
                        final String errMsg = "Table " + getIdentifier() +
                        " manages at least 2 subclasses that both define a field \"" + memberName + "\", " +
                        "and the fields' metadata is different or they have different type! That means you can get e.g. wrong fetch results.";
                        NucleusLogger.DATASTORE.error(errMsg);
View Full Code Here

            // Application identity
            int[] primaryKeyFieldNumbers = cmd.getPKMemberPositions();
            for (int i=0;i<pkMappings.length;i++)
            {
                // Make the assumption that the pkMappings are in the same order as the absolute field numbers
                AbstractMemberMetaData fmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(primaryKeyFieldNumbers[i]);
                consumer.consumeMapping(pkMappings[i], fmd);
            }
        }
        else
        {
            // Datastore identity
            int[] primaryKeyFieldNumbers = cmd.getPKMemberPositions();
            int countPkFields = cmd.getNoOfPrimaryKeyMembers();
            for (int i = 0; i < countPkFields; i++)
            {
                AbstractMemberMetaData pkfmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(primaryKeyFieldNumbers[i]);
                consumer.consumeMapping(getMemberMapping(pkfmd), pkfmd);
            }
        }
    }
View Full Code Here

            consumer.preConsumeMapping(highestMemberNumber + 1);

            Iterator<AbstractMemberMetaData> iter = externalFkMappings.keySet().iterator();
            while (iter.hasNext())
            {
                AbstractMemberMetaData fmd = iter.next();
                JavaTypeMapping fieldMapping = externalFkMappings.get(fmd);
                if (fieldMapping != null)
                {
                    consumer.consumeMapping(fieldMapping, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
                }
            }
        }
        else if (mappingType == MappingConsumer.MAPPING_TYPE_EXTERNAL_FK_DISCRIM && externalFkDiscriminatorMappings != null)
        {
            consumer.preConsumeMapping(highestMemberNumber + 1);

            Iterator<AbstractMemberMetaData> iter = externalFkDiscriminatorMappings.keySet().iterator();
            while (iter.hasNext())
            {
                AbstractMemberMetaData fmd = iter.next();
                JavaTypeMapping fieldMapping = externalFkDiscriminatorMappings.get(fmd);
                if (fieldMapping != null)
                {
                    consumer.consumeMapping(fieldMapping, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK_DISCRIM);
                }
            }
        }
        else if (mappingType == MappingConsumer.MAPPING_TYPE_EXTERNAL_INDEX && externalOrderMappings != null)
        {
            consumer.preConsumeMapping(highestMemberNumber + 1);

            Iterator<AbstractMemberMetaData> iter = externalOrderMappings.keySet().iterator();
            while (iter.hasNext())
            {
                AbstractMemberMetaData fmd = iter.next();
                JavaTypeMapping fieldMapping = externalOrderMappings.get(fmd);
                if (fieldMapping != null)
                {
                    consumer.consumeMapping(fieldMapping, MappingConsumer.MAPPING_TYPE_EXTERNAL_INDEX);
                }
View Full Code Here

                    // 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,
View Full Code Here

    {
        // Go through the fields for this class and add columns for them
        for (int fieldNumber=0; fieldNumber<mmds.length; fieldNumber++)
        {
            // Primary key fields are added by the initialisePK method
            AbstractMemberMetaData mmd = mmds[fieldNumber];
            if (!mmd.isPrimaryKey())
            {
                if (managesMember(mmd.getFullFieldName()))
                {
                    if (!mmd.getClassName(true).equals(theCmd.getFullClassName()))
                    {
                        // Field already managed by this table so maybe we are overriding a superclass
                        JavaTypeMapping fieldMapping = getMappingForMemberName(mmd.getFullFieldName());
                        ColumnMetaData[] colmds = mmd.getColumnMetaData();
                        if (colmds != null && colmds.length > 0)
                        {
                            // Apply this set of ColumnMetaData to the existing mapping
                            int colnum = 0;
                            IdentifierFactory idFactory = getStoreManager().getIdentifierFactory();
                            for (int i=0;i<fieldMapping.getNumberOfDatastoreMappings();i++)
                            {
                                Column col = (Column)fieldMapping.getDatastoreMapping(i).getDatastoreField();
                                col.setIdentifier(idFactory.newDatastoreFieldIdentifier(colmds[colnum].getName()));
                                col.setColumnMetaData(colmds[colnum]);

                                colnum++;
                                if (colnum == colmds.length)
                                {
                                    // Reached end of specified metadata
                                    break;
                                }
                            }
                            if (NucleusLogger.DATASTORE.isDebugEnabled())
                            {
                                // TODO Change this to reflect that we have updated the previous mapping
                                // Provide field->column mapping debug message
                                StringBuffer columnsStr = new StringBuffer();
                                for (int i=0;i<fieldMapping.getNumberOfDatastoreMappings();i++)
                                {
                                    if (i > 0)
                                    {
                                        columnsStr.append(",");
                                    }
                                    columnsStr.append(fieldMapping.getDatastoreMapping(i).getDatastoreField());
                                }
                                if (fieldMapping.getNumberOfDatastoreMappings() == 0)
                                {
                                    columnsStr.append("[none]");
                                }
                                StringBuffer datastoreMappingTypes = new StringBuffer();
                                for (int i=0;i<fieldMapping.getNumberOfDatastoreMappings();i++)
                                {
                                    if (i > 0)
                                    {
                                        datastoreMappingTypes.append(',');
                                    }
                                    datastoreMappingTypes.append(fieldMapping.getDatastoreMapping(i).getClass().getName());
                                }
                                NucleusLogger.DATASTORE.debug(LOCALISER.msg("057010",
                                    mmd.getFullFieldName(), columnsStr.toString(), fieldMapping.getClass().getName(), datastoreMappingTypes.toString()));
                            }
                        }
                    }
                }
                else
                {
                    // Manage the field if not already managed (may already exist if overriding a superclass field)
                    if (mmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT)
                    {
                        boolean isPrimary = true;
                        if (mmd.getTable() != null && mmd.getJoinMetaData() == null)
                        {
                            // Field has a table specified and is not a 1-N with join table
                            // so is mapped to a secondary table
                            isPrimary = false;
                        }

                        if (isPrimary)
                        {
                            // Add the field to this table
                            JavaTypeMapping fieldMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_FIELD);
                            if (theCmd != cmd &&
                                theCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUPERCLASS_TABLE &&
                                fieldMapping.getNumberOfDatastoreMappings() > 0)
                            {
                                // Field is for a subclass and so column(s) has to either allow nulls, or have default
                                int numCols = fieldMapping.getNumberOfDatastoreMappings();
                                for (int colNum = 0;colNum < numCols; colNum++)
                                {
                                    Column col = (Column) fieldMapping.getDatastoreMapping(colNum).getDatastoreField();
                                    if (col.getDefaultValue() == null && !col.isNullable())
                                    {
                                        // Column needs to be nullable
                                        NucleusLogger.DATASTORE_SCHEMA.debug("Member " + mmd.getFullFieldName() +
                                            " uses superclass-table yet the field is not marked as nullable " +
                                            " nor does it have a default value, so setting the column as nullable");
                                        col.setNullable();
                                    }
                                }
                            }

                            addMemberMapping(fieldMapping);
                        }
                        else
                        {
                            // Add the field to the appropriate secondary table
                            if (secondaryTables == null)
                            {
                                secondaryTables = new HashMap();
                            }
                            SecondaryTable secTable = secondaryTables.get(mmd.getTable());
                            if (secTable == null)
                            {
                                // Secondary table doesnt exist yet so create it to users specifications.
                                JoinMetaData[] joinmds = theCmd.getJoinMetaData();
                                JoinMetaData joinmd = null;
                                if (joinmds != null)
                                {
                                    for (int j=0;j<joinmds.length;j++)
                                    {
                                        if (joinmds[j].getTable().equalsIgnoreCase(mmd.getTable()) &&
                                            (joinmds[j].getCatalog() == null || (joinmds[j].getCatalog() != null && joinmds[j].getCatalog().equalsIgnoreCase(mmd.getCatalog()))) &&
                                            (joinmds[j].getSchema() == null || (joinmds[j].getSchema() != null && joinmds[j].getSchema().equalsIgnoreCase(mmd.getSchema()))))
                                        {
                                            joinmd = joinmds[j];
                                            break;
                                        }
                                    }
                                }

                                DatastoreIdentifier secTableIdentifier =
                                    storeMgr.getIdentifierFactory().newDatastoreContainerIdentifier(mmd.getTable());
                                // Use specified catalog, else take catalog of the owning table
                                String catalogName = mmd.getCatalog();
                                if (catalogName == null)
                                {
                                    catalogName = getCatalogName();
                                }
                                // Use specified schema, else take schema of the owning table
                                String schemaName = mmd.getSchema();
                                if (schemaName == null)
                                {
                                    schemaName = getSchemaName();
                                }
                                secTableIdentifier.setCatalogName(catalogName);
                                secTableIdentifier.setSchemaName(schemaName);

                                secTable = new SecondaryTable(secTableIdentifier, storeMgr, this, joinmd, clr);
                                secTable.preInitialize(clr);
                                secTable.initialize(clr);
                                secTable.postInitialize(clr);
                                secondaryTables.put(mmd.getTable(), secTable);
                            }
                            secTable.addMemberMapping(storeMgr.getMappingManager().getMapping(secTable, mmd,
                                clr, FieldRole.ROLE_FIELD));
                        }
                    }
                    else if (mmd.getPersistenceModifier() != FieldPersistenceModifier.TRANSACTIONAL)
                    {
                        throw new NucleusException(LOCALISER.msg("057006",mmd.getName())).setFatal();
                    }

                    // Calculate if we need a FK adding due to a 1-N (FK) relationship
                    boolean needsFKToContainerOwner = false;
                    int relationType = mmd.getRelationType(clr);
                    if (relationType == Relation.ONE_TO_MANY_BI)
                    {
                        AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
                        if (mmd.getJoinMetaData() == null && relatedMmds[0].getJoinMetaData() == null)
                        {
                            needsFKToContainerOwner = true;
                        }
                    }
                    else if (relationType == Relation.ONE_TO_MANY_UNI)
                    {
                        if (mmd.getJoinMetaData() == null)
                        {
                            needsFKToContainerOwner = true;
                        }
                    }

                    if (needsFKToContainerOwner)
                    {
                        // 1-N uni/bidirectional using FK, so update the element side with a FK
                        if ((mmd.getCollection() != null && !SCOUtils.collectionHasSerialisedElements(mmd)) ||
                            (mmd.getArray() != null && !SCOUtils.arrayIsStoredInSingleColumn(mmd, storeMgr.getMetaDataManager())))
                        {
                            // 1-N ForeignKey collection/array, so add FK to element table
                            AbstractClassMetaData elementCmd = null;
                            if (mmd.hasCollection())
                            {
                                // Collection
                                elementCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getCollection().getElementType(), clr);
                            }
                            else
                            {
                                // Array
                                elementCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getType().getComponentType(), clr);
                            }
                            if (elementCmd == null)
                            {
                                // Elements that are reference types or non-PC will come through here
                                if (mmd.hasCollection())
                                {
                                    NucleusLogger.METADATA.warn(LOCALISER.msg("057016",
                                        theCmd.getFullClassName(), mmd.getCollection().getElementType()));
                                }
                                else
                                {
                                    NucleusLogger.METADATA.warn(LOCALISER.msg("057014",
                                        theCmd.getFullClassName(), mmd.getType().getComponentType().getName()));
                                }
                            }
                            else
                            {
                                AbstractClassMetaData[] elementCmds = null;
                                // TODO : Cater for interface elements, and get the metadata for the implementation classes here
                                if (elementCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE)
                                {
                                    elementCmds = storeMgr.getClassesManagingTableForClass(elementCmd, clr);
                                }
                                else
                                {
                                    elementCmds = new ClassMetaData[1];
                                    elementCmds[0] = elementCmd;
                                }

                                // Run callbacks for each of the element classes.
                                for (int i=0;i<elementCmds.length;i++)
                                {
                                    storeMgr.addSchemaCallback(elementCmds[i].getFullClassName(), mmd);
                                    DatastoreClass dc = storeMgr.getDatastoreClass(elementCmds[i].getFullClassName(), clr);
                                    if (dc == null)
                                    {
                                        throw new NucleusException("Unable to add foreign-key to " +
                                            elementCmds[i].getFullClassName() + " to " + this + " since element has no table!");
                                    }
                                    ClassTable ct = (ClassTable) dc;
                                    if (ct.isInitialized())
                                    {
                                        // if the target table is already initialized, run the callbacks
                                        ct.runCallBacks(clr);
                                    }
                                }
                            }
                        }
                        else if (mmd.getMap() != null && !SCOUtils.mapHasSerialisedKeysAndValues(mmd))
                        {
                            // 1-N ForeignKey map, so add FK to value table
                            if (mmd.getKeyMetaData() != null && mmd.getKeyMetaData().getMappedBy() != null)
                            {
                                // Key is stored in the value table so add the FK to the value table
                                AbstractClassMetaData valueCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getMap().getValueType(), clr);
                                if (valueCmd == null)
                                {
                                    // Interface elements will come through here and java.lang.String and others as well
                                    NucleusLogger.METADATA.warn(LOCALISER.msg("057018",
                                        theCmd.getFullClassName(), mmd.getMap().getValueType()));
                                }
                                else
                                {
                                    AbstractClassMetaData[] valueCmds = null;
                                    // TODO : Cater for interface values, and get the metadata for the implementation classes here
                                    if (valueCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE)
                                    {
                                        valueCmds = storeMgr.getClassesManagingTableForClass(valueCmd, clr);
                                    }
                                    else
                                    {
                                        valueCmds = new ClassMetaData[1];
                                        valueCmds[0] = valueCmd;
                                    }
                                   
                                    // Run callbacks for each of the value classes.
                                    for (int i=0;i<valueCmds.length;i++)
                                    {
                                        storeMgr.addSchemaCallback(valueCmds[i].getFullClassName(), mmd);
                                        DatastoreClass dc = storeMgr.getDatastoreClass(valueCmds[i].getFullClassName(), clr);
                                        ClassTable ct = (ClassTable) dc;
                                        if (ct.isInitialized())
                                        {
                                            // if the target table is already initialized, run the callbacks
                                            ct.runCallBacks(clr);
                                        }
                                    }
                                }
                            }
                            else if (mmd.getValueMetaData() != null && mmd.getValueMetaData().getMappedBy() != null)
                            {
                                // Value is stored in the key table so add the FK to the key table
                                AbstractClassMetaData keyCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getMap().getKeyType(), clr);
                                if (keyCmd == null)
                                {
                                    // Interface elements will come through here and java.lang.String and others as well
                                    NucleusLogger.METADATA.warn(LOCALISER.msg("057019",
                                        theCmd.getFullClassName(), mmd.getMap().getKeyType()));
                                }
                                else
                                {
                                    AbstractClassMetaData[] keyCmds = null;
                                    // TODO : Cater for interface keys, and get the metadata for the implementation classes here
View Full Code Here

                // TODO Does this allow for overridden PK field info ?
                AbstractClassMetaData baseCmd = cmd.getBaseAbstractClassMetaData();
                fieldCount = baseCmd.getNoOfManagedMembers();
                for (int relFieldNum = 0; relFieldNum < fieldCount; ++relFieldNum)
                {
                    AbstractMemberMetaData mmd = baseCmd.getMetaDataForManagedMemberAtPosition(relFieldNum);
                    if (mmd.isPrimaryKey())
                    {
                        if (mmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT)
                        {
                            membersToAdd[pkFieldNum++] = mmd;
                            hasPrimaryKeyInThisClass = true;
                        }
                        else if (mmd.getPersistenceModifier() != FieldPersistenceModifier.TRANSACTIONAL)
                        {
                            throw new NucleusException(LOCALISER.msg("057006", mmd.getName())).setFatal();
                        }

                        // Check if auto-increment and that it is supported by this RDBMS
                        if ((mmd.getValueStrategy() == IdentityStrategy.IDENTITY) &&
                            !dba.supportsOption(DatastoreAdapter.IDENTITY_COLUMNS))
                        {
                            throw new NucleusException(LOCALISER.msg("057020",
                                cmd.getFullClassName(), mmd.getName())).setFatal();
                        }
                    }
                }
            }
            else
            {
                for (int relFieldNum=0; relFieldNum<fieldCount; ++relFieldNum)
                {
                    AbstractMemberMetaData fmd = cmd.getMetaDataForManagedMemberAtPosition(relFieldNum);
                    if (fmd.isPrimaryKey())
                    {
                        if (fmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT)
                        {
                            membersToAdd[pkFieldNum++] = fmd;
                            hasPrimaryKeyInThisClass = true;
                        }
                        else if (fmd.getPersistenceModifier() != FieldPersistenceModifier.TRANSACTIONAL)
                        {
                            throw new NucleusException(LOCALISER.msg("057006",fmd.getName())).setFatal();
                        }

                        // Check if auto-increment and that it is supported by this RDBMS
                        if ((fmd.getValueStrategy() == IdentityStrategy.IDENTITY) &&
                            !dba.supportsOption(DatastoreAdapter.IDENTITY_COLUMNS))
                        {
                            throw new NucleusException(LOCALISER.msg("057020",
                                cmd.getFullClassName(), fmd.getName())).setFatal();
                        }
                    }
                }
            }
        }

        // No Primary Key defined, so search for superclass or handle datastore id
        if (!hasPrimaryKeyInThisClass)
        {
            if (cmd.getIdentityType() == IdentityType.APPLICATION)
            {
                // application-identity
                DatastoreClass elementCT = storeMgr.getDatastoreClass(cmd.getPersistenceCapableSuperclass(), clr);
                if (elementCT != null)
                {
                    // Superclass has a table so copy its PK mappings
                    ColumnMetaDataContainer colContainer = null;
                    if (cmd.getInheritanceMetaData() != null)
                    {
                        // Try via <inheritance><join>...</join></inheritance>
                        colContainer = cmd.getInheritanceMetaData().getJoinMetaData();
                    }
                    if (colContainer == null)
                    {
                        // Try via <primary-key>...</primary-key>
                        colContainer = cmd.getPrimaryKeyMetaData();
                    }

                    addApplicationIdUsingClassTableId(colContainer, elementCT, clr, cmd);
                }
                else
                {
                    // Superclass has no table so create new mappings and columns
                    AbstractClassMetaData pkCmd = storeMgr.getClassWithPrimaryKeyForClass(cmd.getSuperAbstractClassMetaData(), clr);
                    if (pkCmd != null)
                    {
                        pkMappings = new JavaTypeMapping[pkCmd.getNoOfPrimaryKeyMembers()];
                        pkFieldNum = 0;
                        fieldCount = pkCmd.getNoOfInheritedManagedMembers() + pkCmd.getNoOfManagedMembers();
                        for (int absFieldNum = 0; absFieldNum < fieldCount; ++absFieldNum)
                        {
                            AbstractMemberMetaData fmd = pkCmd.getMetaDataForManagedMemberAtAbsolutePosition(absFieldNum);
                            if (fmd.isPrimaryKey())
                            {
                                AbstractMemberMetaData overriddenFmd = cmd.getOverriddenMember(fmd.getName());
                                if (overriddenFmd != null)
                                {
                                    // PK field is overridden so use the overriding definition
                                    fmd = overriddenFmd;
                                }
View Full Code Here

                    processedCallbacks = new HashSet();
                    callbacksAppliedForManagedClass.put(managedCmd.getFullClassName(), processedCallbacks);
                }
                for (Iterator it = c.iterator(); it.hasNext();)
                {
                    AbstractMemberMetaData callbackMmd = (AbstractMemberMetaData) it.next();
                    if (processedCallbacks.contains(callbackMmd))
                    {
                        continue;
                    }
                    else
                    {
                        processedCallbacks.add(callbackMmd);
                    }

                    if (callbackMmd.getJoinMetaData() == null)
                    {
                        // 1-N FK relationship
                        AbstractMemberMetaData ownerFmd = callbackMmd;
                        if (ownerFmd.getMappedBy() != null)
                        {
                            // Bidirectional (element has a PC mapping to the owner)
                            // Check that the "mapped-by" field in the other class actually exists
                            AbstractMemberMetaData fmd = managedCmd.getMetaDataForMember(ownerFmd.getMappedBy());
                            if (fmd == null)
                            {
                                throw new NucleusUserException(LOCALISER.msg("057036",
                                    ownerFmd.getMappedBy(), managedCmd.getFullClassName(), ownerFmd.getFullFieldName()));
                            }
View Full Code Here

                            found=true;
                        }
                    }
                    for (int k=0; k<acmd.getNoOfManagedMembers()+acmd.getNoOfInheritedManagedMembers() && !found; k++)
                    {
                        AbstractMemberMetaData apmd = acmd.getMetaDataForManagedMemberAtAbsolutePosition(k);
                        if (persistentTypes[i].getColumnForField(apmd.getName()) != null)
                        {
                            if (persistentTypes[i].getColumnForField(apmd.getName()).equals(columnNames[j]))
                            {
                                fieldColumns.put(columnNames[j], apmd);
                                columnsInThisType.add(columnNames[j]);
                                fmds[j] = apmd;
                                found = true;
                            }
                        }
                        else
                        {
                            JavaTypeMapping mapping = dc.getMemberMapping(apmd);
                            for(int l=0; l<mapping.getDatastoreMappings().length && !found; l++)
                            {
                                DatastoreField df = mapping.getDatastoreMapping(l).getDatastoreField();
                                if (df.getIdentifier().getIdentifierName().equals(columnNames[j]))
                                {
                                    fieldColumns.put(columnNames[j], apmd);
                                    columnsInThisType.add(columnNames[j]);
                                    fmds[j] = apmd;
                                    found = true;
                                }
                            }
                        }
                    }
                    if (!columnsInThisType.contains(columnNames[j]))
                    {
                        //column not found in this type, so must be another persistent type
                        startColumnIndex = j;
                        break;
                    }
                }

                // Build fields and mappings in the results
                StatementMappingIndex[] stmtMappings =
                    new StatementMappingIndex[acmd.getNoOfManagedMembers() + acmd.getNoOfInheritedManagedMembers()];

                Set fields = new HashSet();
                fields.addAll(fieldColumns.values());
                int[] fieldNumbers = new int[fields.size()];
                Iterator it = fields.iterator();
                int j=0;
                while (it.hasNext())
                {
                    AbstractMemberMetaData apmd = (AbstractMemberMetaData)it.next();
                    StatementMappingIndex stmtMapping = new StatementMappingIndex(dc.getMemberMapping(apmd));

                    fieldNumbers[j] = apmd.getAbsoluteFieldNumber();
                    List indexes = new ArrayList();
                    for (int k=0; k<fmds.length; k++)
                    {
                        if (fmds != null && fmds[k] == apmd)
                        {
View Full Code Here

TOP

Related Classes of org.datanucleus.metadata.AbstractMemberMetaData

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.